Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------
- // CS215-401 PROJECT 2
- //--------------------------------------------------------------------
- // Author: Ash Rule
- // Date: 10/11/19
- // Description: 2nd project using arrays and functions.
- //--------------------------------------------------------------------
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <ctype.h>
- #include <fstream>
- using namespace std;
- const int READ_ERROR = -1;
- const int MAX_INV_ITEMS = 10;
- const int MAX_ORDERS = 7;
- const int MAX_ORDER_ITEMS = 5;
- //int numberOfInvItems = 0;
- //int lastOrderNum = 0;
- //int totalOrder = 0;
- //int selectItemNum;
- //int numOfOrders = 0;
- //int numBasket = 0;
- ifstream fin;
- ofstream fout;
- // describes a single item in the inventory, and on an order
- struct item {
- string prodCode; // product code: length 12, no spaces
- string description; // product description: max length 28, has spaces
- double price = 0; // price of the product, max 999.99
- };
- // describes a customer order or "basket"
- struct order {
- long orderNumber = 0; // unique order number for this order
- string custName; // customer name
- double totalPrice = 0; // price of all items purchased
- item items[MAX_ORDER_ITEMS]; // list of items purchased
- int numItems = 0; // number of items purchased
- };
- //----------------------------------------------------------------------------
- // readInventory
- //----------------------------------------------------------------------------
- // Modifies: inventory list (sets numberOfItems to -1 on READ ERROR)
- // lastOrderNum
- // Reads inventory data from a file into an array of inventory items
- //----------------------------------------------------------------------------
- void readInventory(item inv[], int& numberOfInvItems, int& lastOrderNum) {
- ifstream f;
- // open the inventory file
- f.open("inventory.txt");
- if (f.fail()) {
- cout << "readFile:: error opening inventory.txt\n";
- numberOfInvItems = READ_ERROR;
- return;
- }
- // read number of items from first line
- f >> numberOfInvItems >> lastOrderNum;
- // for each item, read the data for the item
- for (int i = 0; i < numberOfInvItems; i++) {
- f >> inv[i].prodCode >> inv[i].price;
- f.ignore(); // finished reading integer, getline() on string is next
- getline(f, inv[i].description);
- }
- f.close();
- } // readInventory()
- /*bool isValidOption(char option, string validOptions) {
- for (int i = 0; i < validOptions.length(); i++)
- return true;
- */
- bool isValidOption(char option, string validOptions) {
- for (int i = 0; i < validOptions.length(); i++)
- if (validOptions[i] == option) {
- return true;
- }
- return false;
- }
- char getMainOption() {
- string userInput, validOptions = "IOLX";
- char option;
- cout << "+--------------------------------------------------+" << endl;
- cout << "+ AMZN.COM +" << endl;
- cout << "+ by Ash Rule +" << endl;
- cout << "+--------------------------------------------------+" << endl;
- cout << "I - List our Inventory" << endl;
- cout << "O - Make an order" << endl;
- cout << "L - List all Orders made" << endl;
- cout << "X - Exit" << endl;
- cout << "Enter an option: ";
- cin >> userInput;
- option = toupper(userInput[0]);
- while (!isValidOption(option, validOptions)) {
- cout << "Invalid option, enter I, O, L, or X!" << endl;
- cout << "Enter an option: ";
- cin >> userInput;
- option = toupper(userInput[0]);
- }
- return option;
- }
- void displayList(item inventory[], int numberOfInvItems) {
- for (int i = 0; i < numberOfInvItems; i++)
- cout << fixed << setw(3) << right << i << " " << setw(12) << left << inventory[i].prodCode << " $" << setw(6) << right << setprecision(2) << inventory[i].price << " " << inventory[i].description << endl;
- }
- void displayInventory(item inventory[], int numberOfInvItems) {
- cout << "+--------------------------------------------------+" << endl;
- cout << "+ Products +" << endl;
- cout << "+--------------------------------------------------+" << endl;
- cout << " # PRODUCT CODE PRICE PRODUCT DESCRIPTION " << endl;
- cout << "--- ------------ ------- ------------------------" << endl;
- displayList(inventory, numberOfInvItems);
- cout << "Number of items in inventory: " << numberOfInvItems << endl << endl;
- }
- void displayOrder(order basket) {
- cout << "ORDER: " << basket.orderNumber << " " << basket.custName << endl;
- displayList(basket.items, basket.numItems);
- cout << "Total $" << basket.totalPrice << endl;
- //cout << "Total Number of Orders = " << basket[numBasket] << endl << endl;
- //cout << "Total number of orders: " << basket.numItems << endl;
- }
- void startOrder(order basket[], int &numBasket, int &lastOrderNum) {
- lastOrderNum++;
- basket[numBasket].totalPrice = 0;
- basket[numBasket].numItems = 0;
- basket[numBasket].orderNumber = lastOrderNum;
- cout << "Order Number: " << lastOrderNum << endl;
- cout << "Enter customer name: ";
- if (cin.peek() == '\n') cin.ignore();
- getline(cin, basket[numBasket].custName);
- }
- bool orderItem(item inventory[], order &basket, int numInvItems) {
- int selectItemNum;
- cout << "Enter item number (-1 to end): ";
- cin >> selectItemNum;
- while (selectItemNum >= numInvItems || selectItemNum < -1) {
- cout << "Invalid entry. Enter number -1 to " << numInvItems - 1 << endl;
- cout << "Enter item number (-1 to end): ";
- cin >> selectItemNum;
- }
- if (selectItemNum < numInvItems && selectItemNum > -1) {
- basket.items[basket.numItems].prodCode = inventory[selectItemNum].prodCode;
- basket.items[basket.numItems].description = inventory[selectItemNum].description;
- basket.items[basket.numItems].price = inventory[selectItemNum].price;
- basket.totalPrice = basket.totalPrice += basket.items[basket.numItems].price;
- cout << inventory[selectItemNum].description << " added to your basket. Current total is $ " << basket.totalPrice << endl;
- basket.numItems++;
- //return false;
- }
- else if (selectItemNum == -1) {
- cout << "Thank you for your order!" << endl;
- displayOrder(basket);
- system("pause");
- return true;
- }
- }
- void makeOrder(item inventory[], order basket[], int numberOfInvItems, int &numOfOrders, int &lastOrderNum){
- bool check = false;
- if (numOfOrders < MAX_ORDERS) {
- startOrder(basket, numOfOrders, lastOrderNum);
- displayInventory(inventory, numberOfInvItems);
- while (basket[numOfOrders].numItems < MAX_ORDER_ITEMS && check != true){
- check = orderItem(inventory, basket[numOfOrders], numberOfInvItems);
- }
- numOfOrders++;
- }
- else {
- cout << "Sorry, we can not take more orders today." << endl;
- }
- }
- void listOrders(order basket[], int& numBasket) {
- cout << "+--------------------------------------------------+" << endl;
- cout << "+ Orders +" << endl;
- cout << "+--------------------------------------------------+" << endl;
- for (int i = 0; i < numBasket; i++) {
- displayOrder(basket[i]);
- }
- cout << endl;
- cout << "Total Number of Orders = " << numBasket << endl << endl;
- }
- /*void writeOrders(basket[]) {
- fin.open("orders.txt");
- if (fin.fail()) {
- cout << "File open failed." << endl;
- system("pause");
- return;
- }
- cout << totalOrder << endl;
- for (int i = 0; i < totalOrder; i++) {
- cout << basket[i].orderNumber << basket[i].custName;
- }
- fin.close();
- }*/
- int main(){
- order basket[MAX_ORDERS];
- item inventory[MAX_INV_ITEMS];
- int numberOfInvItems = 0;
- int lastOrderNum; //moved some global variables here
- int numOfOrders = 0;
- int numBasket = 0;
- readInventory(inventory, numberOfInvItems, lastOrderNum);
- char option = getMainOption();
- while (option != 'X') {
- if (option == 'I') {
- displayInventory(inventory, numberOfInvItems); // added numberOfInvItems
- }
- else if (option == 'O') {
- makeOrder(inventory, basket, numberOfInvItems, numOfOrders, lastOrderNum);
- }
- else if (option == 'L') {
- listOrders(basket, numOfOrders);
- }
- option = getMainOption();
- }
- cout << "This is where the orders should be printed out to an output file." << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement