Skip to content

Instantly share code, notes, and snippets.

@monokaijs
Created October 29, 2019 17:50
Show Gist options
  • Save monokaijs/9f0ed895b7aecffc3029b9476335a19a to your computer and use it in GitHub Desktop.
Save monokaijs/9f0ed895b7aecffc3029b9476335a19a to your computer and use it in GitHub Desktop.
Assignment2.cpp
#include <iostream>
using namespace std;
float totalPrice, highestPrice, lowestPrice;
int highestIndex, lowestIndex;
int bookCount = 0;
int bookIds[1000];
string bookNames[1000];
float bookPrices[1000];
void showMenu();
void showSubmenu(int choice);
void showSubmenu(int choice) {
system("cls"); // Clear Screen
switch (choice) {
case 1: {
// Menu add book
int bookId;
string bookName;
float bookPrice;
cout << "Please input these information:"; cout << endl;
cout << " - ID: "; cin >> bookId; cout << endl;
cout << " - Name: "; cin >> bookName; cout << endl;
cout << " - Price: "; cin >> bookPrice; cout << endl;
bookCount += 1;
bookIds[bookCount] = bookId;
bookNames[bookCount] = bookName;
bookPrices[bookCount] = bookPrice;
totalPrice += bookPrice;
if (bookPrice < lowestPrice) {
lowestIndex = bookCount;
lowestPrice = bookPrice;
} else if (bookPrice > highestPrice) {
highestIndex = bookCount;
highestPrice = bookPrice;
}
showMenu();
}
case 2: {
int bookInputId;
bool found = false;
cout << "Please input Book's ID: ";
cin >> bookInputId;
for (int i = 1; i <= bookCount; i++) {
if (bookIds[i] == bookInputId) {
found = true;
int bookIndex = i;
cout << "Here is information about this book:" << endl;
cout << " - ID: " << bookIds[bookIndex] << endl;
cout << " - Name: " << bookNames[bookIndex] << endl;
cout << " - Price: " << bookPrices[bookIndex] << endl;
}
}
if (!found) cout << endl << "There's no book has that ID!";
showMenu();
}
case 3: {
// Menu statistics
int amountLower = 0, amountHigher = 0;
float averagePrice = totalPrice / bookCount;
for (int i = 1; i <= bookCount; i++) {
amountLower += (bookPrices[i] > averagePrice) ? 1 : 0;
amountHigher += (bookPrices[i] > averagePrice) ? 1 : 0;
}
cout << "Average price is: " << averagePrice << endl;
cout << "There're " << amountHigher << " books has price higher than the average price." << endl;
cout << ".... and " << amountLower << " books has price lower than the average price." << endl;
cout << endl << endl;
cout << "The most expensive book has ID: " << bookIds[highestIndex] << " with price: " << highestPrice << endl;
cout << "The cheapest book has ID: " << bookIds[lowestIndex] << " with price: " << lowestPrice << endl;
system("pause");
showMenu();
}
case 4:
// Quit
exit(0);
break;
default:
showMenu();
break;
}
}
void showMenu() {
system("cls"); // Clear Screen
cout << "Please choose an option:" << endl;
string menu[] = { "1. Add book", "2. Check information", "3. Statistic", "4. Quit" };
for (string line : menu) cout << line << endl;
cout << endl << "Your choice is: ";
int choice;
cin >> choice;
showSubmenu(choice);
}
int main() {
showMenu();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment