Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in Visual C++, Assignment learning objectives: You are supposed to correctly def

ID: 3732239 • Letter: I

Question

in Visual C++,

Assignment learning objectives: You are supposed to correctly define and write neat code that uses classes, functions, dynamic arrays, constructors, destructors and overloaded operators. The assignment problem: You have just started your new on-campus job at the front desk of the Swinney recreation center. Your job is to help students check-out and check-in gym items (towels, basket balls, rackets, bikes, etc). Traditionally, students who work this job record all items that students check out and in manually in two text files (checked out and checked in). At the end of the day, they update a third file (students.txt) that includes information about students and what items are currently checked out (if any). However, you believe that this process is tedious, time consuming and error prone. Therefore, you are thinking to automate this process by writing a C++ program that does all the work for you: because you are a great programmer who took 201L, and you can do it! See the following figure to better understand your program input and output. Items being checked out Log of students and checked out items Updated log of students and checked out itoms Output Your app Item being checked in Figurel. Overview of the assignment's input and output data files know that not all students hit the gym frequently and, therefore, you realize that not all students check items all the time. At the same time, some students check several items simultaneously (e.g. towel, locker is means that you are not sure on how much memory to allocate for this data. Thus, you will gym and a basketball). Th write a Sudent class that has a dynamic array to hold the items that each Student has checked-out.

Explanation / Answer

//main.cpp
#include "Student.h"

using namespace std;

void main()
{
   ifstream in("Students.txt");
   ifstream rin("checkouts.txt");
   ifstream lin("checkins.txt");
   ofstream out("Updated_Students.txt");
   unsigned int studentID;
   string item;
   int totalStudents = 0;

   if (!in)
       cout << "failed to open file.";
   if (!rin)
       cout << "failed to open file.";
   if (!lin)
       cout << "failed to open file.";
   int i = 0;
   const int size = 100;
   Student s1;
   Student* students = new Student[size];
  
   while (in >> s1)
   {
       cout << s1;
   }
   //while (!in.eof())
   //{
   //   in >> students[totalStudents];
   //   ++totalStudents;
   //}
  
  

   //for (int i = 0; i < totalStudents; i++)
   //{
   //   students[i] = s1;
   //}

   in.close();

   while (rin.good())
   {
       rin >> studentID;
       for (int i = 0; i < totalStudents; i++)
       {
           if (students[i].getID() == studentID)
           {
               rin >> item;
               students[i].CheckOut(item);
           }
       }
   }

   while (lin.good())
   {
       lin >> item;
       for (int i = 0; i < totalStudents; i++)
       {
           if (students[i].CheckIn(item))
               break;
       }
   }

   for (int i = 0; i < totalStudents; i++)
       out << students[i];


   delete[] students;
   students = NULL;
   in.close();
   rin.close();
   lin.close();
   out.close();
}
---------------------------------------------------------------------------------------------
//student.cpp
#include "Student.h"

Student::Student()
{
   idNumb = 0;
   firstName = "";
   lastName = "";
   itemName = "";
   aSize = 0;
   numItems = 0;
   pointy = NULL;
}

void Student::setID(unsigned int nID)
{
   if ((nID > 999) && (nID < 100001))
       idNumb = nID;
   else
       cout << "Invalid ID Number.";
}

void Student::setFirst(string nFirst)
{
   firstName = nFirst;
}

void Student::setLast(string nLast)
{
   lastName = nLast;
}

int Student::CheckoutCount()
{
   return numItems;
}

bool Student::CheckOut(const string& item)
{
   if (HasCheckedOut(item))
       return false;

   if (pointy == NULL && aSize == 0)
   {
       string *newPointy = new string[5];
   }

       string *newPointy = new string[aSize];
  
   for (int i = 0; i < aSize; i++)
   {
       newPointy[i] = pointy[i];
   }
   if (pointy != NULL && aSize > 0)
       delete[] pointy;
   pointy = newPointy;

   pointy[numItems] = itemName;
   numItems++;

   return true;
}

bool Student::CheckIn(const string& item)
{
   if (!HasCheckedOut(item))
       return false;
   string *newPointy = new string[aSize];
   int nArraySize = 0;
   for (int i = 0; i <aSize; i++)
   {
       if (pointy[i] != item)
       {
           newPointy[nArraySize] = pointy[i];
           nArraySize++;
       }
   }

   delete[] pointy;
   pointy = newPointy;
   numItems--;
}

bool Student::HasCheckedOut(const string& item)
{
   for (int i = 0; i < aSize; i++)
   {
       if (pointy[i] == item)
           return true;
   }
   return false;
}

void Student::Clear()
{
   idNumb = 0;
   firstName = "";
   lastName = "";

   if((pointy != NULL) && (numItems > 0))
   delete[] pointy;
   pointy = NULL;
   numItems = 0;
}

Student::~Student()
{
   delete[] pointy;
   pointy = NULL;
}

istream& operator>>(istream& in, Student& item)
{  
   item.Clear();
   string itemOut;
   unsigned int id;
   string first, last;
   in >>id >> first >> last >> item.aSize;
   item.setID(id);
   item.setFirst(first);
   item.setLast(last);

       for (int i = 0; i < item.aSize; i++)
   {
       in >> itemOut;
       item.CheckOut(itemOut);
   }
   return in;
}
ostream& operator<<(ostream& out, const Student& item)
{
   out << item.idNumb << " " << item.firstName << " " << item.lastName << endl;
   out << item.numItems << endl;
   for (int i = 0; i < item.numItems; i++)
   {
       out << item.pointy[i] << " ";
   }
   return out;
}

Student Student::operator+(Student &student1)
{
   Student anotherStudent;
   anotherStudent = student1;
   anotherStudent.CheckOut(itemName);
   return anotherStudent;
}
  
void Student:: operator+=(string& itemName)
{
   CheckOut(itemName);

}
bool operator==(Student & s1, Student &s2)
{
   if (s1.getID() == s2.getID())
       return true;
   else
       return false;
}
----------------------------------------------------------
//student.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student
{
protected:
   string firstName, lastName, itemName;
   unsigned int idNumb;
   int numItems, aSize;
   string* pointy = NULL;
public:
   //default constructor
   Student();

   //setters
   void setID(unsigned int nID);
   void setFirst(string nFirst);
   void setLast(string nLast);

   //getters
   int getID() { return idNumb; }
   string getFirst() { return firstName; }
   string getLast() { return lastName; }

   int CheckoutCount();
   //returns number of items student has checked out

   //takes a string parameter to describe an item
   bool CheckOut(const string& item);
   //checks if item is in if it is, checks it out

   //takes a string parameter to describe an item
   bool CheckIn(const string& item);
   //checks if item is checked out and deletes the array if so. a successful check in
   //returns true, if not it returns false.

   //takes an item parameter
   bool HasCheckedOut(const string& item);
   //checks if item is on the students checked out list

   void Clear();
   //clears the objects data

   //destructor
   ~Student();

   //Operator overloads
   friend istream& operator>>(istream& in, Student& item);
   //reads data in from text stream

   friend ostream& operator<<(ostream& out, const Student& item);
   //outputs data into text file
  
   Student operator+(Student &student1);
   //overloads the addition function adds an item to a student and outputs a new user

   void operator+=(string &itemName);
   //another way to add an item to a student and check it out

   bool friend operator==(const Student &item, const Student &item1);
   //checks if two students have the same id number or not

};