c++ program 1.Create the generic class ITEM. It should include general character
ID: 3877370 • Letter: C
Question
c++ program
1.Create the generic class ITEM. It should include general characteristics for ALL items. such as Item • Name • Location • Price
2. Create A new class is created called WritingObject. It should include the properties from Item class • Name • Location • Price plus • Color propertie.
3.Create the new class for FoodItem. Add a new category called Food. You have to inherit the properties from Item. It varies from WritingObject that expirationdate is added. The food class should include the same properties from Item: • Name • Location • Price plus the • ExpirationDate • Isrefregirated
5. Use the concept of inheritance between Item and WrittingObject and between Item and FoodItem
6. Each inheritance overloads the constructor of Item, depending on the parameters sent.
On your main project (program):
All the inventory items now need to be saved to a file. One file should contain all the data and one file for all the WrittingObjects
Create a function for Print all the data on file. It should receive the name of the file. (in both Classes: Food and Writing Objects)
Create a function to add a new item to the file (inside the classes of Food and Writing Object)
Do not use global variables for storing food and writing objects. Create a variable that counts all records in the file.
Hints:
- the user has to choose between the category foodItem or writingObject to request their properties
- The items have to be recorded in a text file and some that are food.txt and other writingobject.txt the program has to be able to read the files of and how to write
- you have to call fstream to save or read the files
- create a pointer to play with the functions
Explanation / Answer
#include<iostream>
#include<vector>
#include<array>
#include<algorithm>
#include<map>
#include<set>
#include<hash_map>
#include<hash_set>
#include<list>
#include<queue>
#include<cmath>
#include<stack>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<memory>
#include<thread>
#include<stdlib.h>
#include<fstream>
#define ull unsigned long long int
#define mod 1000000007
using namespace std;
class Item{
private:
string name;
string location;
float price;
public:
Item() {}
Item(string name, string location, float price) {
this->name = name;
this->location = location;
this->price = price;
}
virtual void printData() {
cout << "Item Name : " << name << endl;
cout << "Location : " << location << endl;
cout << "Price : " << price << endl;
}
virtual void addNewData(fstream *f) {
(*f) << name << endl<< location<<endl << price<<endl;
}
};
class FoodItem :public Item {
private:
bool isRefregirated;
string expirationDate;
public:
FoodItem(){};
FoodItem(string name, string location, float price, bool isRefregirated, string expirationDate) : Item(name, location, price) {
this->isRefregirated = isRefregirated;
this->expirationDate = expirationDate;
}
void printData() {
Item::printData();
cout << "isRefregirated : " << isRefregirated << endl;
cout << "expirationDate : " << expirationDate << endl;
}
void addNewData(fstream *f) {
Item::addNewData(f);
(*f) << isRefregirated <<endl<< expirationDate<<endl;
}
};
class WritingObject :public Item {
private:
string colorProperty;
public:
WritingObject() {};
WritingObject(string name, string location, float price, string colorProperty) : Item(name, location, price) {
this->colorProperty = colorProperty;
}
virtual void printData() {
Item::printData();
cout << "colorProperty : " << colorProperty << endl;
}
void addNewData(fstream *f) {
Item::addNewData(f);
(*f) << colorProperty<<endl;
}
};
int countFoodItem = 0, countWriteObject = 0;
void printData(string fileName) {
fstream fp;
string name;
string location;
float price;
string colorProperty;
bool isRefregirated;
string expirationDate;
if (fileName == "food.txt") {
countFoodItem = 0;
fp.open("food.txt",std::fstream::in);
while (!fp.eof() && !fp.bad() &&!fp.fail()) {
fp >> name;
fp >> location;
fp >> price;
fp >> isRefregirated;
fp >> expirationDate;
FoodItem fi(name, location, price, isRefregirated, expirationDate);
fi.printData();
countFoodItem++;
}
fp.close();
}
else{
fp.open("writingobject.txt");
while (!fp.eof()) {
countWriteObject = 0;
fp >> name;
fp >> location;
fp >> price;
fp >> colorProperty;
WritingObject wo(name, location, price, colorProperty);
wo.printData();
countWriteObject++;
}
fp.close();
}
}
int main() {
fstream fs;
bool flag = true;
int choice;
string name;
string location;
float price;
string colorProperty;
bool isRefregirated;
string expirationDate;
FoodItem *fi;
WritingObject *wo;
while (flag) {
cout << " 1. Display Food Item";
cout << " 2. Display Writing Object";
cout << " 3. Enter new Food Item";
cout << " 4. Enter new Writing Object";
cout << " 5. Display Food Item Count";
cout << " 6. Display Writing Object Count";
cout << " 7. Quit" << endl;
cin >> choice;
switch (choice)
{
case 1: printData("food.txt"); break;
case 2: printData("writingobject.txt"); break;
case 3: cin >> name >> location >> price >> isRefregirated >> expirationDate;
fi = new FoodItem(name, location, price, isRefregirated, expirationDate);
fs.open("food.txt", std::fstream::app);
fi->addNewData(&fs);
delete fi;
fs.close();
countFoodItem++;
break;
case 4: cin >> name >> location >> price >> colorProperty;
wo = new WritingObject(name, location, price, colorProperty);
fs.open("writingobject.txt", std::fstream::app);
wo->addNewData(&fs);
delete wo;
fs.close();
countWriteObject++;
break;
case 5: cout << countFoodItem << endl; break;
case 6: cout << countWriteObject << endl; break;
case 7: flag = false; break;
default:
flag = false;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.