Question

In: Computer Science

In C++ One of the things we rely on computers to do for us is to...

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:

  • Menus! (10 points)
    • Control the interface
      • Select a list to edit
      • Allow users to add or remove items
      • Return to Main Menu(s)
      • Quit the program
  • Functions! (30 points)
    • Entering items to add to a list
    • Deleting items from a list
    • Menus
  • File Operations! (10 points)
    • Read and use files
      • grocery
      • hardware store
      • chores
  • Loops! (20 points)
    • Add as many items as necessary
    • Delete as many items as necessary
    • Move between lists as often as my little heart desires
  • Vectors! (20 points)
    • Read items from files into vectors
    • Add items to lists - who's length is unknown
    • Delete selected items from lists
    • Display lists in a way that makes sense to a user
  • Good User Design (20 points)
    • Make sure I know what's going on as the user. That means good user prompting, control of the outputs to the screen, clearing where necessary, and keeping the user engaged as they edit those lists!

Solutions

Expert Solution

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:


Related Solutions

We rely on wireless networks everyday and put much trust in the activities that we do...
We rely on wireless networks everyday and put much trust in the activities that we do on them. This makes it extremely important to protect them from threats and take measures to protect ourselves while working on them as well. 1. List at least 5 ways that you can secure a wireless network to make your activities on them less vulnerable to attack. 2. In your own words, write at least ten sentences supporting your list.
In healthcare, what are things that we do or things that happen that make employees feel...
In healthcare, what are things that we do or things that happen that make employees feel unsafe?
1. The US and Germany both produces beef and computers. In one day, the US can...
1. The US and Germany both produces beef and computers. In one day, the US can produce either 500 pounds of beef or 200 computers. Germany on the other hand can produce 250 pounds of beef and 500 computers Daily US Productivity Beef (lbs) Computers 500 0 375 50 250 100 125 150 0 200 Daily Germany productivity Beef (lbs) Computers 250 0 200 100 150 200 100 300 50 400 0 500 1a) what is the pre-trade (autarkic) price...
C or C++ program Create a list of 5 things-to-do when you are bored (in the...
C or C++ program Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading...
What does a confidence interval tell us and how do we calculate one? As part of...
What does a confidence interval tell us and how do we calculate one? As part of your answer, please come up with an example.
althoug there are things that we do not pay (at least directly), it does not mean...
althoug there are things that we do not pay (at least directly), it does not mean that they are free. but why not? why can not we do everything free and open to everyone? (according to the economic aspect) and plis send me an example.
If all we can observe are things inside the observable universe, how do we know that...
If all we can observe are things inside the observable universe, how do we know that anything even exists outside this boundary? I can see four ways of solving this problem. 1) We wait a while, the observable universe should get 'larger', so we should be able to observe more. I don't think this is practical though, since telescopes have only existed for a hundred years or so, whereas the age of the universe is many degrees larger. Also, galaxies...
Interactive Session: People Are We Relying Too Much on Computers to Think for Us? Does our...
Interactive Session: People Are We Relying Too Much on Computers to Think for Us? Does our ever burgeoning dependence on computers foster complacency, suppressing our ability to marshal our mental faculties when required? Although computerization has undoubtedly mitigated malfunctions, work stoppages, and breakdowns, are we concurrently losing our ability to assess alternatives independently and make optimal choices? At least one technology writer is sure this is exactly what is happening. Nicholas Carr’s book, The Glass Cage: Automation and Us, lays...
21. “We should do to others what we would want others to do to us.” This...
21. “We should do to others what we would want others to do to us.” This principle is an example of: Select one: a. descriptive ethics. b. veil of ignorance. c. normative ethics. d. categorical imperative. e. metaethics. 22.Which of the following statements holds true for the concept of “environmental exploitation”? Select one: a. It refers to the organized coercion of unwilling people into following a particular religion. b. It refers to the illegal trade of human beings for the...
One of the things that some businesses do to minimize risk in this area is to...
One of the things that some businesses do to minimize risk in this area is to use wired networking (as opposed to wireless networking). If you were an e-business owner would you consider using 100% wired networking? Why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT