Schedule‎ > ‎

Practice Final Solution Code


To run this as is:  Username:  Sharon pw:  abc123

Remember that your code does not have to be exactly like this.  There are several
different ways to write this.

curio.h

#ifndef CURIO_H
#define CURIO_H
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

class Curio {
public:
    /**
        Default constructor for a Curio that sets variables
        to default values
    */
    Curio();

    /**
        Constructs a Curio.

        @param name Name of the Curio.
        @param price Price of the Curio.
        @param qty Quantity of the Curio in stock.
    */
    Curio(string newName, double newPrice, int newQuantity);

    /**
        Returns the name of this Curio.

        @return The name of this Curio.
    */
    string getName() const;

    /**
        Returns the price of this Curio.

        @return The price of this Curio.
    */
    double getPrice() const;

    /**
        Returns the quantity of this Curio in stock.

        @return The quantity of this Curio.
    */
    int getQuantity() const;

    /**
        Returns the dollar value of this Curio in stock.

        @return The dollar value of this Curio.
    */
    double getValue() const;

    /**
        Changes the name of this Curio.

        @param newName New name of this Curio.
    */
    void setName(string newName);

    /**
        Changes the price of this Curio.

        @param newPrice New price of this Curio.
    */
    void setPrice(double newPrice);

    /**
        Changes the quantity of this Curio.

        @param newQuantity New quantity of this Curio.
    */
    void setQuantity(int newQuantity);

    /**
        Loads information about this Curio from the file stream.

        @param fin An input stream with the data to load into this class.
    */
    void read(ifstream& fin);

    /**
        Displays information about this Curio to the screen.
    */
    void print();
private:
    string name;
    double price;
    int quantity;
};
#endif // CURIO_H

curio.cpp

#include <iostream>
#include "curio.h"
using namespace std;

Curio::Curio() {
    price = 0.0;
    quantity = 0;
}

Curio::Curio(string newName, double newPrice, int newQuantity) {
    setName(newName);
    setPrice(newPrice);
    setQuantity(newQuantity);
}

string Curio::getName() const {
    return name;
}

double Curio::getPrice() const {
    return price;
}

int Curio::getQuantity() const {
    return quantity;
}

double Curio::getValue() const {
    return price * quantity;
}

void Curio::setName(string newName) {
    name = newName;
}

void Curio::setPrice(double newPrice) {
    if (newPrice > 0.0) {
        price = newPrice;
    } else {
        price = 0.0;
    }
}

void Curio::setQuantity(int newQuantity) {
    if (newQuantity > 0) {
        quantity = newQuantity;
    } else {
        quantity = 0;
    }
}

// Read the Curio information from a file stream
void Curio::read(ifstream& fin) {
    getline(fin, name);
    fin >> price >> quantity;
    fin.ignore(1000, '\n');
}

void Curio::print() {
    cout.setf(ios::fixed);
    cout << setw(16) << left << name
         << setw(8) << setprecision(2) << right << price
         << setw(6) << setprecision(0) << quantity
         << setw(10) << setprecision(2) << getValue() << endl;
}




curioapp.cpp

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include "curio.h"
using namespace std;

/**
    Asks for the user's login name and password. Tests the entered name and
    password against the parameter values. If the login and password are
    not correct, prints an error message and asks the user to try again.
    When the correct login and password are entered, asks the user for their
    full name and provides a personalized greeting.

    @param user The user name to test against.
    @param password The password to test against.
    @return The name that the person entered.
*/
string login(string user, string password);

/**
    Displays all the Curios by calling print() for each Curio object.

    @param list The vector of Curio objects.
*/
void listData(vector<Curio>& list);

/**
    Loads the curio data from the specified file name into the vector
    of Curio objects.

    @param list The vector of Curio objects.
    @param fileName The name of the file from which to read.
*/
void loadData(vector<Curio>& list, string fileName);

/**
    Writes a report to a file named report.txt.

    @param list The vector of Curio objects.
    @param username The users username.
*/
void writeReport(vector<Curio>& list, string username);

/**
    Displays the string backwards.

    @param str The string to display in reverse.
    @param index An index needed for counting the recursion.
*/
void showBackwards(string str, int index);
int main()
{
    string name = login("Sharon", "abc123");

    Curio pumpkin("Glass Pumpkin", 12.33, 15);
    Curio statue("Hercules Statue", 7.75, 10);
    Curio bowl;
    bowl.setName("Purple Bowl");
    bowl.setPrice(17.23);
    bowl.setQuantity(5);

    vector<Curio> store;
    store.push_back(pumpkin);
    store.push_back(statue);
    store.push_back(bowl);

    int choice = 1;
    while (choice != 0) {
        cout << "\nPlease choose one of the following operations:\n"
             << 0 << ". Exit program\n"
             << 1 << ". Report inventory\n"
             << 2 << ". Load more data\n"
             << 3 << ". Save data to file\n"
             << 4 << ". Call recursive function\n"
             << "Choice (0-4): ";
        cin >> choice;
        if (choice == 1) {
            listData(store);
        } else if (choice == 2) {
            loadData(store, "curio.txt");
        } else if (choice == 3) {
            writeReport(store, "saved.txt");
        } else if (choice == 4) {
            cout << "\nYour name backwards is: ";
            showBackwards(name, 0);
            cout << endl;
        } else if (choice != 0) {
            cout << "\nInvalid choice!\n";
        }
    }
    cout << "\nGoodbye!\n";

    return 0;
}
string login(string user, string password) {
    bool valid = false;
    string uname, pword;
    while (!valid) {
        cout << "Username: ";
        cin >> uname;
        cout << "Password: ";
        cin >> pword;
        if (uname == user && pword == password) {
            valid = true;
        } else {
            cout << "Invalid login!\n";
        }
    }

    cout << "Please enter your full name: ";
    string name;
    cin.ignore(1000, '\n');
    getline(cin, name);
    cout << "Welcome to my virtual store, " << name << "!" << endl;
    return name;
}

void loadData(vector<Curio>& list, string fileName) {
    Curio prod;

    ifstream fin(fileName.c_str());
    if (fin.fail()) {
        cout << "File " << fileName << " failed to open.\n";
        exit(1);
    }

    while(fin.good()) {
        prod.read(fin);
        if (fin.good()) {
            list.push_back(prod);
        }
    }

    fin.close();
}

void listData(vector<Curio>& list) {
    cout << "\nCurios in my store:\n";
    cout << setw(18) << left << "# Name"
         << setw(8) << right << "Price"
         << setw(6) << "Qty"
         << setw(10) << "Value" << endl;
    for (unsigned i = 0; i < list.size(); i++) {
        cout << (i + 1) << " ";
        list[i].print();
    }
}

void writeReport(vector<Curio>& list, string username) {
    ofstream fout("report.txt");
    if (fout.fail()) {
        cout << "File report.txt failed to open.\n";
        exit(1);
    }

    fout << "Report for " << username << ":\n";
    fout << setw(18) << left << "# Name"
         << setw(8) << right << "Price"
         << setw(6) << "Qty"
         << setw(10) << "Value" << endl;
    for (unsigned i = 0; i < list.size(); i++) {
        fout << (i + 1) << " ";
        fout.setf(ios::fixed);
        fout << setw(16) << left << list[i].getName()
             << setw(8) << setprecision(2) << right << list[i].getPrice()
             << setw(6) << setprecision(0) << list[i].getQuantity()
             << setw(10) << setprecision(2) << list[i].getValue() << endl;
    }

    fout.close();
}

void showBackwards(string str, int index) {
    if (index >= (int) str.length()) {
        return;
    } else {
        showBackwards(str, index + 1);
        cout << str.at(index);
    }
}

Makefile



# define target dependencies and files
curioapp: curioapp.o curio.o
g++ -o curioapp curioapp.o curio.o

# define how each object file is to be built
curioapp.o: curioapp.cpp curio.h
g++ -c curioapp.cpp -Wall -Wextra -Wpedantic -std=c++11  

curio.o: curio.cpp curio.h
g++ -c curio.cpp -Wall -Wextra -Wpedantic -std=c++11 

# clean up
clean:
rm -f curioapp.exe *.o

Comments