306 lines
7.8 KiB
C++
306 lines
7.8 KiB
C++
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <ctime>
|
|
|
|
namespace Coffee {
|
|
|
|
#pragma warning(disable : 4996)
|
|
|
|
int DisplayMessageBox(LPCWSTR msg) {
|
|
int msgboxID = MessageBox(
|
|
NULL,
|
|
msg,
|
|
(LPCWSTR)L"Error",
|
|
MB_ICONERROR | MB_RETRYCANCEL | MB_DEFBUTTON2
|
|
);
|
|
|
|
switch(msgboxID) {
|
|
case IDCANCEL:
|
|
break;
|
|
case IDRETRY:
|
|
|
|
break;
|
|
}
|
|
return msgboxID;
|
|
}
|
|
|
|
bool isString(std::string s) {
|
|
for(int i = 0; i < s.length(); i++) {
|
|
if(isdigit(s[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int inputInt() { //we use it to input int
|
|
int x;
|
|
while(!(std::cin >> x)) {
|
|
std::cout << "Error!\nPlease enter number!\n";
|
|
std::cin.clear();
|
|
std::cin.ignore(9223372036854775807, '\n'); //cin.ignore(numeric_limits<std::streamsize>::max()); (max possible long long)
|
|
}
|
|
return x;
|
|
}
|
|
|
|
std::string inputString() { //we use it to input string
|
|
std::string s;
|
|
std::cin >> s;
|
|
return s;
|
|
}
|
|
|
|
|
|
class Drink {
|
|
public:
|
|
int price;
|
|
std::string name;
|
|
std::string view;
|
|
Drink(std::string s, double p,std::string n) : //constructor
|
|
view(s),
|
|
price(p),
|
|
name(n)
|
|
{}
|
|
bool canBuy(double cash) { //check if we can buy drink
|
|
if(price <= cash) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
//Initialize Drink type objects, we input ascii art, price, name
|
|
Drink tea(" ) (\n ( ) )\n ) ( (\n _______)_\n .-'---------|\n( C|/\\/\\/\\/\\/|\n '-./\\/\\/\\/\\/|\n '_________'\n '-------'\n", 200, "Tea");
|
|
Drink coffee(" )))\n (((\n +-----+\n | |]\n `-----'\n___________\n`---------'\n", 200, "Coffee");
|
|
Drink hotChocolate(" ( (\n ) )\n ........\n | |]\n \\ /\n `----'\n", 300, "Hot Cohocolate");
|
|
Drink latte(" /#/\n _---------//_\n ( / )\n /=============\\\n(///////////////)\n \\ /\n C===========O \n", 200, "Latte");
|
|
Drink cappucino(" ..\n .. ..\n ..\n ..\n ..\n ..\n ..\n## .. ####\n##.............## ##\n##.............## ##\n##.............## ##\n##.............###\n ##...........##\n #############\n #############\n#################\n", 250, "Cappucino");
|
|
|
|
class CoffeeMachine {
|
|
private:
|
|
bool admin;
|
|
bool saveReceipt;
|
|
int receiptCount;
|
|
public:
|
|
CoffeeMachine() : //constructor
|
|
admin(false),
|
|
saveReceipt(false),
|
|
receiptCount(0)
|
|
{}
|
|
void pour(Drink& drink) { //show drink
|
|
std::cout << "Pouring the drink...\n";
|
|
std::cout << drink.view;
|
|
}
|
|
void buy(Drink& drink) { //buy drink
|
|
if(saveReceipt) { //save receipt
|
|
receiptCount++;
|
|
std::ofstream receiptFile; //create ofsteam object
|
|
//string name = "Receipt No"+to_string(receiptCount);
|
|
//name.append(".txt");
|
|
std::string name ="receipt.txt";
|
|
receiptFile.open(name,std::ios::app); //create and open new receipt file
|
|
time_t timeNow = time(0);
|
|
receiptFile << "Receipt:\n";
|
|
receiptFile << "Time: " << ctime(&timeNow); //write currect time to receipt
|
|
receiptFile<<"You ordered: "<<drink.name<<std::endl; //write drink name to receipt
|
|
receiptFile << "The drink cost: " << drink.price << " rubles\n\n"; //write drink cost to receipt
|
|
receiptFile.close(); //close file
|
|
std::cout << "Receipt Saved\n";
|
|
}
|
|
pour(drink);
|
|
}
|
|
void adminMode() { //enter admin mode
|
|
admin = true;
|
|
}
|
|
bool isAdminMode() { //check if in admin mode
|
|
return admin;
|
|
}
|
|
void toggleSaveReceipts() { //toggle if we save recepits
|
|
if(admin) {
|
|
if(!saveReceipt) {
|
|
saveReceipt = true;
|
|
std::cout << "Coffee machie now saves receipts!\n";
|
|
} else {
|
|
saveReceipt = false;
|
|
std::cout << "Coffee machie now dont saves receipts!\n";
|
|
}
|
|
} else {
|
|
std::cout << "You are not in admin mode!\n";
|
|
}
|
|
}
|
|
};
|
|
CoffeeMachine cm; //initialize CoffeeMachine type object
|
|
|
|
|
|
class Client {
|
|
private:
|
|
void continueBuyingDrink(Drink& drink) { //call this method if client has insufficient funds to buy drink
|
|
while(!drink.canBuy(cash)) {
|
|
std::cout << "Insufficient funds!\n";
|
|
std::cout << "Do you want to deposit funds and buy the drink?\t Input: 1-for yes\t 2-for no\n";
|
|
int choice;
|
|
choice = inputInt();
|
|
int money;
|
|
switch(choice){
|
|
case 1:
|
|
std::cout << "How much do you want to deposit?\n"; //deposit
|
|
money = inputInt();
|
|
deposit(money);
|
|
printf("You now have %d rubles\n", cash);
|
|
break;
|
|
case 2:
|
|
return;
|
|
default:
|
|
std::cout << "Incorrect input\n";
|
|
break;
|
|
}
|
|
|
|
}
|
|
cash -= drink.price;
|
|
cm.pour(drink);
|
|
}
|
|
int cash;
|
|
public:
|
|
Client() { //constructor
|
|
cash = 0;
|
|
}
|
|
void setCash(int n) {
|
|
cash = n;
|
|
}
|
|
int getCash() {
|
|
return cash;
|
|
}
|
|
void deposit(int n) {
|
|
cash += n;
|
|
}
|
|
void buyDrink(CoffeeMachine& cm) { //call if client buys a drink
|
|
int choice;
|
|
std::cout << "What do You want to drink?\n1-Tea 2-Coffee 3-Hot Chocolate 4-Latte 5-Cappucino 6-Cancel\n";
|
|
printf("You now have %d rubles\n", cash);
|
|
choice = inputInt();
|
|
switch(choice) {
|
|
case 1:
|
|
printf("The drink costs: %d\n", tea.price);
|
|
if(tea.canBuy(cash)) { //check if we can buy drink, decrease clients money
|
|
cash -= tea.price;
|
|
cm.buy(tea);
|
|
} else {
|
|
continueBuyingDrink(tea);
|
|
}
|
|
break;
|
|
case 2:
|
|
printf("The drink costs: %d\n", coffee.price);
|
|
if(tea.canBuy(cash)) {
|
|
cash -= coffee.price;
|
|
cm.buy(coffee);
|
|
} else {
|
|
continueBuyingDrink(coffee);
|
|
}
|
|
break;
|
|
case 3:
|
|
printf("The drink costs: %d\n", hotChocolate.price);
|
|
if(tea.canBuy(cash)) {
|
|
cash -= hotChocolate.price;
|
|
cm.buy(hotChocolate);
|
|
} else {
|
|
continueBuyingDrink(hotChocolate);
|
|
}
|
|
break;
|
|
case 4:
|
|
printf("The drink costs: %d\n", latte.price);
|
|
if(tea.canBuy(cash)) {
|
|
cash -= latte.price;
|
|
cm.buy(latte);
|
|
} else {
|
|
continueBuyingDrink(latte);
|
|
}
|
|
break;
|
|
case 5:
|
|
printf("The drink costs: %d\n", cappucino.price);
|
|
if(tea.canBuy(cash)) {
|
|
cash -= cappucino.price;
|
|
cm.buy(cappucino);
|
|
} else {
|
|
continueBuyingDrink(cappucino);
|
|
}
|
|
break;
|
|
case 6:
|
|
return;
|
|
default:
|
|
std::cout << "Wrong drink choice\n";
|
|
buyDrink(cm);
|
|
break;
|
|
}
|
|
printf("You now have %d rubles\n", cash);
|
|
}
|
|
};
|
|
Client client; //initialize Client type object
|
|
|
|
std::string password = "123qwe43"; //maintenance(admin) password
|
|
|
|
int main() {
|
|
std::cout << "Welcome to our coffee machine!\n\n";
|
|
while(true) {
|
|
std::cout << "You have a few options to choose from\n1 - check balance\n2 - replenish the balance(only credit card is accepted)\n3 - buy drink\n4 - input maintenance password\n5 - toggle receipt mode\n";
|
|
int choice;
|
|
choice = inputInt();
|
|
int cash;
|
|
std::string pass;
|
|
LPCWSTR msg;
|
|
msg = L"Do you want to try again?";
|
|
switch(choice) { //choices, client can make
|
|
case 1:
|
|
printf("You now have %d rubles\n", client.getCash());
|
|
break;
|
|
case 2:
|
|
while(true) {
|
|
cash = inputInt();
|
|
if(cash <= 0) {
|
|
std::cout << "You can not enter negative summ or 0!\nPlease input valid summ!\n";
|
|
} else {
|
|
client.deposit(cash);
|
|
printf("You now have %d rubles\n", client.getCash());
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
case 3:
|
|
client.buyDrink(cm);
|
|
break;
|
|
case 4:
|
|
if(cm.isAdminMode()) {
|
|
std::cout << "Already in admin mode!\n";
|
|
break;
|
|
}
|
|
while(true) {
|
|
std::cout << "Please input password\n";
|
|
pass = inputString();
|
|
if(pass == password) {
|
|
cm.adminMode();
|
|
std::cout << "You entered admin mode\n";
|
|
break;
|
|
} else {
|
|
std::cout << "Incorrect password\n";
|
|
}
|
|
if(DisplayMessageBox(msg) == IDCANCEL) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 5:
|
|
cm.toggleSaveReceipts();
|
|
break;
|
|
default:
|
|
std::cout << "Incorrect input\n";
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
} |