Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Program Description: A user can choose to create a file containing
- stock information for an inventory, or read the stock information
- from a file. The user can then request various data from the file via
- a menu function.*/
- #include "stdafx.h"
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- double profit(int num_items, double buy_price, double sell_price, double store_fee);
- class Table_item //an object of type Table_item represents one whole row in the inventory table
- {
- public:
- int item_ID;
- int num_items;
- double buy_price;
- double sell_price;
- double store_fee;
- Table_item(int id, int num, double buy, double sell, double store){ //constructor
- item_ID = id;
- num_items = num;
- buy_price = buy;
- sell_price = sell;
- store_fee = store;
- }
- friend Table_item operator>>(istream &in, Table_item& item){ //defines input stream for table items
- in >> item.item_ID >> item.num_items >> item.buy_price >> item.sell_price >> item.store_fee;
- return item;
- }
- friend Table_item operator<<(ostream &out, Table_item& item){ //defines output stream for table items
- out << item.item_ID << item.num_items << item.buy_price << item.sell_price << item.store_fee;
- return item;
- }
- };
- int main(){
- char option;
- char menu_choice;
- int store_num;
- vector <Table_item> inv_vect;
- cout << "Would you like to enter data by hand (H) or read it from a file (F)" << endl;
- cin >> option;
- if(option == 'H'){
- fstream inv_table("Tablefile.txt", fstream::in | fstream::out | fstream::trunc);
- cout << "Please enter the number of items you would like to store:" << endl; //capped at 100 throughout program
- cin >> store_num;
- int i = 1;
- while(i <= store_num){
- //inv_table.open();
- int id_loc;
- int num_loc;
- double buy_loc, sell_loc, fee_loc;
- cout << "Please enter the item ID for item " << i << ":";
- cin >> id_loc;
- cout << endl;
- cout << "Please enter the number of items for item " << i << ":";
- cin >> num_loc;
- cout << endl;
- cout << "Please enter the buying price for item " << i << ":" << endl;
- cin >> buy_loc;
- cout << endl;
- cout << "Please enter the selling price for item " << i << ":" << endl;
- cin >> sell_loc;
- cout << endl;
- cout << "Please enter the storage fee for item " << i << ":" << endl;
- cin >> fee_loc;
- cout << endl;
- Table_item item(id_loc, num_loc, buy_loc, sell_loc, fee_loc);
- inv_table << item;
- inv_table << endl;
- //inv_table.flush();
- inv_vect.push_back(item);
- i++;
- }//end while for writing inventory file
- //inv_table.close();
- cout << inv_table;
- cout << endl;
- cout << inv_vect[0];
- cout << endl;
- cout << "Menu | Please choose one of the following options: \n" ;
- cout << "Option a: Print stock information. \n";
- cout << "Option b: Print general information. \n";
- cout << "Option c: Print all the items in a table. \n";
- cout << "Option d: Edit an item’s information, store in a file. \n"; //options d, e in the syllabus have been merged into a single block
- cout << "Option e: Exit. \n";
- cin >> menu_choice;
- if (menu_choice == 'a'){
- cout<< "Stock Information: \n";
- for(int i=1; i<5; i++){
- double profit_indexed;
- profit_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
- cout << "Item" << i << endl;
- cout<<"* Item ID:"<< inv_vect[i].item_ID << endl;
- cout<< "* Number of items:"<< inv_vect[i].num_items << endl;
- cout<< "* Buying price: "<< inv_vect[i].buy_price << endl;
- cout<< "* Selling price"<< inv_vect[i].sell_price << endl;
- cout<< "* Storage fees:"<< inv_vect[i].store_fee << endl;
- cout << "* Profit:" << profit_indexed << endl;
- }
- }
- if(menu_choice == 'b'){
- int total_items = 0;
- double profit_total = 0;
- double buying_total = 0, selling_total = 0;
- for (int i=1; i<5; i++){
- total_items += inv_vect[i].num_items;
- }
- cout << "Total number of items: " << total_items << endl;
- for ( int i=1; i<5; i++){
- double profit_total_indexed;
- profit_total_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
- profit_total += profit_total_indexed;
- }
- cout << "Total Profit:" << profit_total << endl;
- cout << "Average Profit:" << (profit_total)/4 << endl;
- for ( int i=1; i<5; i++){
- buying_total += inv_vect[i].buy_price;
- selling_total += inv_vect[i].sell_price;
- }
- cout << "Buying price average:" << (buying_total)/4 << endl;
- cout << "Selling price average:" << (selling_total)/4 << endl;
- }
- if(menu_choice == 'c') //Print all inventory items in a table
- {
- int k=0;
- while(k<100){
- cout << inv_vect[k]; //automatically tabular; see definition of << operator for objects of type Table_item
- cout << endl;
- k++;
- }
- }
- if(menu_choice == 'd') //Edit an item from user input, given an item's ID number
- {
- int edit_id;
- cout << "Please enter the ID of the item you wish to edit" << endl;
- cin >> edit_id;
- int k=0;
- while(inv_vect[k].item_ID != edit_id){ //traversing array for item with ID edit_id, for loop
- k++;
- }
- int new_id, new_num;
- double new_buyp, new_sellp, new_storef;
- cout << "The current item information is:" << endl;
- cout << inv_vect[k]; //k+1 used since above while loop exits before incrementing k
- cout << "Enter a new item ID: ";
- cin >> new_id;
- cout << endl;
- cout << "Enter a new number of items: ";
- cin >> new_num;
- cout << endl;
- cout << "Enter a new buying price: ";
- cin >> new_buyp;
- cout << endl;
- cout << "Enter a new selling price: ";
- cin >> new_sellp;
- cout << endl;
- cout << "Enter a new storage fee: ";
- cin >> new_storef;
- cout << endl;
- Table_item item(new_id, new_num, new_buyp, new_sellp, new_storef);
- inv_vect.push_back(item);
- char response;
- cout << "Item successfully edited. Would you like to store updated item in a file? (Y/N)" << endl;
- if(response == 'Y')
- {
- int h=0;
- while(h<100){
- inv_table << inv_vect[h];
- h++;
- }
- }
- if(response == 'N'){
- cout << "Okay then." << endl;
- }
- }
- if(menu_choice == 'e') //Closes the program and exits
- {
- char a;
- cout << "Enter any char to exit." << endl;
- cin >> a;
- } //exit
- }//end if block for option H (entering by hand)
- if (option == 'F'){
- fstream inv_table;
- string file_location;
- cout << "Please enter the location of your file (full path):";
- getline(cin, file_location);
- inv_table.open(file_location.c_str());
- /*ifstream inv_file_i;
- ofstream inv_file_o;*/
- if(inv_table.fail()){
- cout << "Input file failed to open.\n";
- exit(1);
- }
- inv_table.open("Tablefile.txt");
- int k=0;
- int j=0, n=0;
- double b=0, s=0, f=0; //dummy parameters for constructor; needed to read from file into array
- Table_item item(j, n, b, s, f);
- vector <Table_item> inv_vect;
- while(k < 100){//building array from file
- inv_table >> item;
- item = inv_vect[k];
- k++;
- }
- cout << "Menu | Please choose one of the following options: \n" ;
- cout << "Option a: Print stock information. \n";
- cout << "Option b: Print general information. \n";
- cout << "Option c: Print all the items in a table. \n";
- cout << "Option d: Edit an item’s information, store in a file. \n"; //options d, e in the syllabus have been merged into a single block
- cout << "Option e: Exit. \n";
- cin >> menu_choice;
- if (menu_choice == 'a')
- {
- cout<< "Stock Information: \n";
- for(int i=1; i<5; i++)
- {
- double profit_indexed;
- profit_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
- cout << "Item" << i << endl;
- cout<<"* Item ID:"<< inv_vect[i].item_ID << endl;
- cout<< "* Number of items:"<< inv_vect[i].num_items << endl;
- cout<< "* Buying price: "<< inv_vect[i].buy_price << endl;
- cout<< "* Selling price"<< inv_vect[i].sell_price << endl;
- cout<< "* Storage fees:"<< inv_vect[i].store_fee << endl;
- cout << "* Profit:" << profit_indexed << endl;
- }
- }
- if(menu_choice == 'b')
- {
- int total_items = 0;
- double profit_total = 0;
- double buying_total = 0, selling_total = 0;
- for (int i=1; i<5; i++)
- {
- total_items += inv_vect[i].num_items;
- }
- cout << "Total number of items: " << total_items << endl;
- for ( int i=1; i<5; i++)
- {
- double profit_total_indexed;
- profit_total_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
- profit_total += profit_total_indexed;
- }
- cout << "Total Profit:" << profit_total << endl;
- cout << "Average Profit:" << (profit_total)/4 << endl;
- for ( int i=1; i<5; i++)
- {
- buying_total += inv_vect[i].buy_price;
- selling_total += inv_vect[i].sell_price;
- }
- cout << "Buying price average:" << (buying_total)/4 << endl;
- cout << "Selling price average:" << (selling_total)/4 << endl;
- }
- if(menu_choice == 'c') //Print all inventory items in a table
- {
- int k=0;
- while(k<100){
- cout << inv_vect[k]; //automatically tabular; see definition of << operator for objects of type Table_item
- cout << endl;
- k++;
- }
- }
- if(menu_choice == 'd') //Edit an item from user input, given an item's ID number
- {
- int edit_id;
- cout << "Please enter the ID of the item you wish to edit" << endl;
- cin >> edit_id;
- int k=0;
- while(inv_vect[k].item_ID != edit_id){ //traversing array for item with ID edit_id
- k++;
- }
- int new_id, new_num;
- double new_buyp, new_sellp, new_storef;
- cout << "The current item information is:" << endl;
- cout << inv_vect[k+1]; //k+1 used since above while loop exits before incrementing k
- cout << "Enter a new item ID: ";
- cin >> new_id;
- cout << endl;
- cout << "Enter a new number of items: ";
- cin >> new_num;
- cout << endl;
- cout << "Enter a new buying price: ";
- cin >> new_buyp;
- cout << endl;
- cout << "Enter a new selling price: ";
- cin >> new_sellp;
- cout << endl;
- cout << "Enter a new storage fee: ";
- cin >> new_storef;
- cout << endl;
- Table_item item(new_id, new_num, new_buyp, new_sellp, new_storef);
- inv_vect.push_back(item);
- char response;
- cout << "Item successfully edited. Would you like to store updated item in a file? (Y/N)" << endl;
- if(response == 'Y')
- {
- int h=0;
- while(h<100){
- inv_table << inv_vect[h];
- h++;
- }
- }
- if(response == 'N')
- {
- cout << "Okay then." << endl;
- }
- }
- if(menu_choice == 'e') //Closes the program and exits
- {
- char a;
- cout << "Enter any char to exit." << endl;
- cin >> a;
- } //exit
- }//end if block for option F (opening file at location)
- }//end main
- double profit (int num_items, double buy_price, double sell_price, double store_fee)
- {
- using namespace std ;
- double a;
- a = (num_items) * ((sell_price ) - (buy_price + store_fee));
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement