I need help making a Supermarket program in C++ Here are the instructions: Imple
ID: 3669075 • Letter: I
Question
I need help making a Supermarket program in C++
Here are the instructions:
Implement the class SuperMarket that has:
1. Method Greetings: ask the customer name. Check the time, greeting the customer properly, e.g. good morning, afternoon, evening.
2. Method Menu: like an ATM, ask the user to select from a list of 5 items which items was purchased, how many of each item.
3. Method Bill: calculates the amount to be paid by the customer.
4. Method printInvoice: print in a formatted way, just like as in a supermarket reciept. Item description, amount purchased, total on that item, and final total to be paid.
5. Method readDatabase: this method reads the file that should contain in each line the product name and the price. e.g.
Shampoo Blue Moon 6.99
Apple each 0.35 etc.
6. Method printInvoice: the reciept should be printed in a file that will be generated for each customer, having the customer name as the file name. e.g. johnmeyer_receipt.txt, paulgreen_receipt.txt
I am aware that there is a supermarket program out there that lets you add items and make an inventory, as well as place orders of those items. That program is so much more complicated that the one i need. I only need the program to do the above.
We were also given a time function used to check the time to greet the customer properly.
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void getTime(void);
void getTime(void)
{
time_t t = time(0);
struct tm * now = localtime( &t );
cout << (now->tm_year + 1900) << "-"
<< (now->tm_mon + 1) << "-"
<< now->tm_mday << "-----"
<< now->tm_hour << ":"
<< now->tm_min << ":"
<< now->tm_sec
<< endl;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <cstdlib>
using namespace std;
class SuperMarket
{
private:
void SuperMarketAPI(); // API runs methods in proper(?) order
void Greetings(); // Ask customer name, check time, greet customer (good morning, afternoon, evening)
void Menu(); // Ask user to select from list of items which items were purchased and how many of each item
void PrintInvoice();// Prints invoice from printInvoice to a file with the customer name as the file name (e.g. johnmeyer_receipt.txt)
void ReadDatabase(); // Calculate and return the amount to be paid by the customer
void PrintReceipt(); // Print a receipt with: Description, amount purchased, total on that item, and final total to be paid
float Bill(); // Calculate and return the amount to be paid by the customer
float totalBill; // The total bill paid
char end;
int lineCount;
struct session {
string customerName;
} currentSession;
struct item {
string itemName;
float itemPrice;
int itemAmount;
}items[5];
public:
SuperMarket();
~SuperMarket();
};
SuperMarket::SuperMarket() {
SuperMarketAPI();
}
SuperMarket::~SuperMarket() {
cout << "Deleted Object";
}
void SuperMarket::Greetings() {
time_t TheTime; // declare a time object
time(&TheTime); // Get the current time
struct tm * timeinfo; // This will hold the time in an easy-to-use structure
timeinfo = localtime(&TheTime); // This converts from raw time to the structure.
cout << "Hello there, valued customer! May I get your name? " << endl;
getline(cin, this->currentSession.customerName);
if (timeinfo->tm_hour < 12) {
cout << "Good morning, ";
}
else if (timeinfo->tm_hour < 17) {
cout << "Good afternoon, ";
}
else {
cout << "Good evening, ";
}
cout << this->currentSession.customerName << "!" << endl;
};
void SuperMarket::Menu()
{
cout << "Here is the menu for today: " << endl;
for (int i = 0; i < lineCount; i++) {
cout << "Item: " << items[i].itemName << ", Price: " << items[i].itemPrice << endl;
cout << "How many would you like? :" << endl;
cin >> items[i].itemAmount;
}
}
float SuperMarket::Bill() {
float totalBill = 0;
for (int i = 0; i < lineCount; i++) {
totalBill = totalBill + items[i].itemPrice * items[i].itemAmount;
}
return totalBill;
}
void SuperMarket::PrintInvoice()
{
cout << "This is your invoice: " << endl;
cout << "Customer: " << currentSession.customerName << endl;
for (int i = 0; i < lineCount; i++) {
cout << "Item: " << items[i].itemName
<< ", Quantity: " << items[i].itemAmount
<< ", Cost: $" << items[i].itemAmount*items[i].itemPrice << endl;
}
cout << "Total: $" << SuperMarket::Bill() << endl;
}
void SuperMarket::ReadDatabase()
{
ifstream inputStream;
inputStream.open("item_list.txt");
if (!inputStream) {
cout << "No Item_list.txt found!";
char n;
cin >> n;
exit(0);
}
int i = 0;
while (inputStream.good()) {
size_t start = 0, end;
string s, priceString;
getline(inputStream, s);
end = s.find_first_of("0123456789", start);
items[i].itemName = s.substr(start, end - 1 - start);
priceString = s.substr(end);
items[i].itemPrice = stof(priceString);
i++;
}
lineCount = i;
inputStream.close();
}
void SuperMarket::PrintReceipt()
{
string outputName = currentSession.customerName + " Receipt.txt";
std::ofstream outputStream(outputName);
outputStream << "Customer: " << currentSession.customerName << endl;
for (int i = 0; i < lineCount; i++) {
outputStream << "Item: " << items[i].itemName
<< ", Quantity: " << items[i].itemAmount
<< ", Cost: $" << items[i].itemAmount*items[i].itemPrice << endl;
}
outputStream << "Total: $" << SuperMarket::Bill() << endl ;
outputStream.close();
}
void SuperMarket::SuperMarketAPI()
{
ReadDatabase();
Greetings();
Menu();
Bill();
PrintInvoice();
PrintReceipt();
cout << "Press any key to close. ";
char n;
cin >> n;
cout << n;
}
int main()
{
SuperMarket superMarket;
return 0;
}
Item_List.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.