Introduction to Programming C++ Project

profileaujam253
project_7_cpp_oop_2016.doc

Introduction to Programming C++ Project

Consider a program to enter codes of one to eight characters along with an associated item number and associated notes. A code can represent an item, package, or product’s name.

By using such a program, we could enter product quantities and additional notes

(i.e. storage considerations) before associating them with some brief code (i.e. string), which would indicate the product’s ID. We could then retrieve a product’s information by entering the product’s ID. This might be a useful application for a small store’s inventory and stock management.

Codes are entered as 1 to 8 characters.

Use "e" for enter, "f" for find, "l" for list, "q" for quit.

Command: e Soda

Enter quantity: 20

Enter notes:

Command: e Milk

Enter quantity: 10

Enter notes: Chilled storage

Command: e Chips

Enter quantity: 25

Enter notes: Easily crushed

Command: f Milk

-- Milk

-- 10

-- Chilled storage

Command: f Popcorn

** No entry with code Popcorn

Command: . . .

There is also an "l" command, which will list all entries on the screen.

For the input to this program, upper and lower case letters are considered equivalent. For example, if a product name with the code "Soda" is entered, then the codes "SODA", "soda", and "SOda" will all retrieve the same entry with

"Soda" associated.

The entries are to be stored in a file from run to run. When the program begins, the entries in the file are to be read into an array. The array should allow for up to 200 entries. The inventory need not be kept in alphabetical order. You may use a simple sequential search to retrieve entries. When the program is exited, the entries should be stored back in a file for use when the program is run again.

Must follow program standards:

1. Variables and Constants. All variables and constants in a program must be declared at the beginning of the program

2. Initialization. Variables may not be initialized in their declaration.

3. Methods. The bodies of all functions must be indented at least 3 (but no more than 5) spaces:

double CircleArea(double r) {

const double Pi = 3.1415;

return Pi * r * r;

}

4. Variable names. Variables that denote things should have names that are nouns or noun phrases.

5. Method names. Methods that have side effects should have names that are verbs or verb phrases. Methods that return a value should have names that are nouns or noun phrases.

6. For loops. The bodies of all for-loops must be indented at least 3 (but no more than 5) spaces:

for(int i = 0; i < n; i++) {

cout << "Enter the number:" << flush;

cin >> num;

if (num < 0) num_neg++;

else if (num > 0) num_pos++;

else num_zero++;

}

--------------------------------------

The code at the end of this document has a separate sort command to sort the entries rather than keeping the inventory sorted. Either way is fine.

For the fixed part of this project (the 200 points) you must extend the program as follows:

· The inventory is to be kept in alphabetical order. When a new entry is to be added, you must use the “Insertion Sort” to find the appropriate location in the array.

· Add a command ‘d’ for delete to delete an entry corresponding to a product name typed by the user.

· Add a command “u” to allow a user to update (edit) one or more of the fields in the inventory

· Make the program object-oriented. This means having a separate class for the internal representation of the inventory, with methods like sort, list, or delete.

Also, this project must extend the program in any of various ways. Some areas for extension are as follows:

· Usability and Aesthetics: Make the output especially pleasing to see and use, or keep the inventory file in a reasonably attractive format. For example, for output one might have

Name: Milk

Quantity: 10

Notes: Chilled storage

· Human Engineering: Provide good human engineering for the user. This means being very tolerant of user errors and making it easy to use. For example, you might give the user an option to name the inventory file, or you might check if the user tries to add another entry with the same name. You might also add a simple help command.

· Reliability: Make the program especially reliable. Try to make it so that the program will not crash even under incorrect inputs. For example, handle a missing file well or prevent an array out of bounds error.

· Maintainability: Make the program especially well structured and readable. As per discussed in class.

· Functionality: Enhance the functionality of the program in various ways, even small ways.

For functionality, one enhancement would be to check that a find or enter command actually has a non-null string for the name. A little more work would be to check the validity of an item’s quantity. For instance, always verifying that an item’s quantity is greater than or equal to zero.

For another example, one could allow a partial match to find or delete an entry. For example, “F po” would match any entry with “pop” in the name, for example “Pop” or “Popcorn” or “Potato”. You might use the function “find” in C++ for this feature.

Final Submission:

· A printout of your code,

· A printout from several consecutive runs, illustrating the various features of your program. For example, you must show that the file I/O works.

· A printout from the list command

In the sample runs, each of the commands "e", "f", "l", “d”, “u”, and "q" should be illustrated.

You can use this program to complete this project.

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

struct Entry {

string name, quantity, notes;

};

Entry entryList[100];

int rec_num = 0;

int num_entries;

string toUpper (string S) {

for (int i= 0; i<S.length(); i++)

S[i] = toupper(S[i]);

return S;

}

void ReadFile () {

string S, eol;

fstream input("Inventory.txt");

while (!input.eof() && !input.fail()){

input >> entryList[rec_num].name

>> entryList[rec_num].quantity;

getline(input, eol); //reads the end-of-line marker

getline(input, S);

entryList[rec_num].notes = S;

rec_num++;

}

cout << "Inventory read." << endl;

num_entries = rec_num;

input.close();

return;

}

void StoreFile () {

fstream F("Inventory.txt");

rec_num = 0;

while (rec_num < num_entries){

F << entryList[rec_num].name << " "

<< entryList [rec_num].quantity << " "

<< entryList [rec_num].notes << " " << endl;

rec_num++;

}

cout << "Inventory stored." << endl;

return;

}

void add_item(string name, string quantity, string notes){

entryList [num_entries].name = name;

entryList [num_entries].quantity = quantity;

entryList [num_entries].notes = notes;

num_entries++;

return;

}

void retrieve_item(string name){

for (int i = 0; i < num_entries; i++){

if (toUpper(entryList [i].name) == toUpper(name)) {

cout << "Quantity: " << entryList [i].quantity << endl

<< "Notes: " << entryList [i].notes << endl;

return;

}

}

cout << "Item not found" << endl;

return;

}

void listAllItems() {

int i = 0;

while (i < num_entries) {

cout << "-- " << entryList [i].name << " "

<< entryList [i].quantity << endl

<< "-- " << entryList [i].notes << endl << endl;

i++;

}

}

int main(){

string name, quantity, notes;

char command;

ReadFile ();

cout << "Use \"e\" for enter, \"f\" for find, \"l\" for list, \"q\" to quit."

<< endl << "Command: ";

cin >> command;

while (command != 'q'){

switch (command){

case 'e': cin >> name; cout << "Enter Quantity: ";

cin >> quantity; cout << "Enter Notes: ";

cin.ignore(); getline(cin, notes);

add_item(name, quantity, notes); break;

case 'f': cin >> name; retrieve_item(name); break;

case 'l': listAllItems(); break;

}

cout << "\nCommand: "; cin >> command;

}

StoreFile();

cout << "All set !";

return 0;

}