In: Computer Science
In C++
One of the things we rely on computers to do for us is to store information. We keep files, movies and music (all legally obtained of course...), and a host of data to help us keep organized.
This week your challenge (the last one!) is to make a program that keeps a collection of lists. You'll manage a grocery, hardware store, and chore list all in the same program. You'll use files to store those lists so that they're somewhat permanent, and vectors to read those lists in to display for the user.
Your list manager will minimally hit the following benchmarks:
The completed C++ Program is given below:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
//grocery item class
class GroceryItem {
public:
string item_name;
float price;
//default constructor
GroceryItem() {
item_name == "";
price = 0;
}
//parameterized constructor
GroceryItem(string name, float price) {
item_name = name;
price == price;
}
void setData(string name, float price) {
item_name = name;
price == price;
}
bool operator == (const GroceryItem &g) const {
return item_name.compare(g.item_name) == 0 && price == g.price;
}
friend istream& operator >> (istream& in, GroceryItem& g) {
in >> g.item_name >> g.price;
return in;
}
friend ostream& operator << (ostream& out, GroceryItem& g) {
out << g.item_name <<" " << g.price << endl;
return out;
}
};
//hardware item class
class HardwareItem {
public:
string name;
string model;
string make;
float price;
HardwareItem() {
name = "";
model = "";
make = "";
price = 0;
}
HardwareItem(string name, string model, string make, float price) {
this->name = name;
this->model = model;
this->make = make;
this->price = price;
}
void setData(string name, string model, string make, float price) {
this->name = name;
this->model = model;
this->make = make;
this->price = price;
}
bool operator == (const HardwareItem& h) const {
return name == h.name && model == h.model && make == h.make && price == h.price;
}
friend istream& operator >> (istream& in, HardwareItem& h) {
in >> h.name >> h.model >> h.make >> h.price;
return in;
}
friend ostream& operator << (ostream& out, HardwareItem& h) {
out << h.name << " " << h.model << " " << h.make << " " << h.price << endl;
return out;
}
};
//chores class
class chores {
public:
string name;
bool done;
chores() {
name = "";
done = false;
}
chores(string name, bool done) {
this->name = name;
this->done = done;
}
void setData(string name, bool done) {
this->name = name;
this->done = done;
}
bool operator == (const chores& c) const{
return name == c.name;
}
friend istream& operator >> (istream& in, chores& c) {
in >> c.name >> c.done;
return in;
}
friend ostream& operator << (ostream& out, chores& c) {
out << c.name << " " << c.done << endl;
return out;
}
};
//presents the grocery menu
void GroceryMenu(vector<GroceryItem>& groceryList) {
int choice;
do {
cout << "\n*****Grocery Store Menu*****" << endl;
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Return to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1) {
cout << "Enter the name and price of the item: ";
GroceryItem g;
cin >> g;
groceryList.push_back(g);
cout << "Grocery Item Added: " << g;
}
else if(choice == 2) {
cout << "Enter the name and price of the item: ";
GroceryItem g;
cin >> g;
vector<GroceryItem>::iterator it;
it = find(groceryList.begin(),groceryList.end(),g);
if(it != groceryList.end()) {
groceryList.erase(it);
cout << "Item remove successfully from the list" << endl;
}
else {
cout << "Item not found in the list" << endl;
}
}
else if(choice == 3) {
break;
}
else {
cout << "Invalid Choice! Try again." << endl;
}
}while(true);
}
//writes grocery list to file
void writeGroceryToFile(vector<GroceryItem>& groceryList) {
unsigned int size = groceryList.size();
ofstream fs("grocery.txt",ios::out);
for(unsigned int i = 0; i < size; i++){
fs << groceryList[i];
}
fs.close();
}
//reads grocery list from file
void readGroceryFromFile(vector<GroceryItem>& groceryList) {
GroceryItem g;
ifstream fs("grocery.txt",ios::in);
if(fs.is_open())
while(!fs.eof()) {
fs >> g;
groceryList.push_back(g);
}
fs.close();
}
//presents the harware store menu
void HardwareMenu(vector<HardwareItem>& hardwareList) {
int choice;
do {
cout << "\n*****Hardware Store Menu*****" << endl;
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Return to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1) {
cout << "Enter the name, make, model and price of the item: ";
HardwareItem h;
cin >> h;
hardwareList.push_back(h);
cout << "Hardware Item Added: " << h;
}
else if(choice == 2) {
cout << "Enter the name, make, model and price of the item: ";
HardwareItem h;
cin >> h;
const HardwareItem hval(h.name,h.make,h.model,h.price);
vector<HardwareItem>::iterator it;
it = find(hardwareList.begin(),hardwareList.end(),h);
if(it != hardwareList.end()) {
hardwareList.erase(it);
cout << "Itemd remove successfully from the list" << endl;
}
else {
cout << "Item not found in the list" << endl;
}
}
else if(choice == 3) {
break;
}
else {
cout << "Invalid Choice! Try again." << endl;
}
}while(true);
}
//writes harware list to file
void writeHardwareToFile(vector<HardwareItem>& hardwareList) {
unsigned int size = hardwareList.size();
ofstream fs("hardware.txt",ios::out);
for(unsigned int i = 0; i < size; i++){
fs << hardwareList[i];
}
fs.close();
}
//writes hardware list from file
void readHardwareFromFile(vector<HardwareItem>& hardwareList) {
HardwareItem g;
ifstream fs("hardware.txt",ios::in);
if(fs.is_open())
while(!fs.eof()) {
fs >> g;
hardwareList.push_back(g);
}
fs.close();
}
//presents the chores menu
void choreMenu(vector<chores>& choreList) {
int choice;
do {
cout << "\n*****Chore Menu*****" << endl;
cout << "1. Add Item" << endl;
cout << "2. Remove Item" << endl;
cout << "3. Return to Main Menu" << endl;
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1) {
string name;
cout << "Enter the name of the chore: ";
cin >> name;
chores c;
c.setData(name,false);
choreList.push_back(c);
cout << "Chore Added: " << c;
}
else if(choice == 2) {
string name;
cout << "Enter the name of the chore ";
cin >> name;
const chores c(name,false);
vector<chores>::iterator it;
it = find(choreList.begin(),choreList.end(),c);
if(it != choreList.end()) {
choreList.erase(it);
cout << "Itemd remove successfully from the list" << endl;
}
else {
cout << "Item not found in the list" << endl;
}
}
else if(choice == 3) {
break;
}
else {
cout << "Invalid Choice! Try again." << endl;
}
}while(true);
}
//writes chores to file
void writeChoreToFile(vector<chores>& choreList) {
unsigned int size = choreList.size();
ofstream fs("chores.txt",ios::out);
for(unsigned int i = 0; i < size; i++){
fs << choreList[i];
}
fs.close();
}
//reads chores from file
void readChoreFromFile(vector<chores>& choreList) {
chores g;
ifstream fs("chores.txt",ios::in);
if(fs.is_open())
while(!fs.eof()) {
fs >> g;
choreList.push_back(g);
}
fs.close();
}
//displays a vector of type t
template <typename T>
void displayList(vector<T>& list) {
unsigned int size = list.size();
if(size > 0)
for(unsigned int i = 0; i < size; i++)
cout << list[i];
else
cout << "Empty List!" << endl;
}
int main() {
vector<GroceryItem> groceryList;
vector<HardwareItem> hardwareList;
vector<chores> choreList;
//read from files and store the data into vectors before we show the menu to the user
readGroceryFromFile(groceryList);
readHardwareFromFile(hardwareList);
readChoreFromFile(choreList);
bool again = true;
int choice;
while(again) {
cout << "\n****LIST MENU*****" << endl;
cout << "******************" << endl;
cout << "1. Grocery Store Menu" << endl;
cout << "2. Hardware Store Menu" << endl;
cout << "3. Chore List Menu " << endl;
cout << "4. Save all the lists to File" << endl;
cout << "5. Show all lists" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch(choice) {
case 1:
GroceryMenu(groceryList);
break;
case 2:
HardwareMenu(hardwareList);
break;
case 3:
choreMenu(choreList);
break;
case 4:
writeGroceryToFile(groceryList);
writeHardwareToFile(hardwareList);
writeChoreToFile(choreList);
cout << "Lists successfully saved to file";
break;
case 5:
cout << "\nGrocery List" << endl;
displayList(groceryList);
cout<< "\nHardware List" << endl;
displayList(hardwareList);
cout << "\nChores List" << endl;
displayList(choreList);
break;
case 6:
again = false;
break;
default:
cout << "Invalid Choice! Try again" << endl;
}
}
}
Sample Output: