Skip to content

Instantly share code, notes, and snippets.

@bwedding
Created May 12, 2020 06:43
Show Gist options
  • Save bwedding/658a5184c37c191184859c3f41fe3935 to your computer and use it in GitHub Desktop.
Save bwedding/658a5184c37c191184859c3f41fe3935 to your computer and use it in GitHub Desktop.
Ticket System
#include <iostream>
#include <iomanip>
#include <ctime>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <sstream>
enum class UserChoice
{
LoadPlay = 1,
SalesSummary,
FinalSales,
SellTickets,
CreatePlay,
Quit
};
enum class SeatType
{
Regular,
Balcony,
Orchestra,
};
struct Seating
{
float Price = 0;
int Available = 0;
int Sold = 0;
};
struct Play
{
std::string PlayName = {""};
Seating Seats[3];
time_t StartTime = 0;
time_t EndTime = 0;
float CurrentSales = 0.0;
};
void PressAnyKey()
{
#ifdef WIN32
system("pause");
#else
system("read");
#endif
}
void ClearScreen()
{
#ifdef WIN32
std::system("cls");
#else
// Assume POSIX
std::system("clear");
#endif
}
UserChoice ShowMainMenu(Play &currentPlay)
{
int choice;
ClearScreen();
std::cout << "*************** Acme Ticket System ****************\n\n";
if(currentPlay.PlayName.length() > 0)
{
std::cout << "Now Selling Tickets for " << currentPlay.PlayName << "\n";
std::cout << "1. Load Play\n";
std::cout << "2. Sales Summary\n";
std::cout << "3. Final Sales Totals\n";
std::cout << "4. Sell Tickets\n";
std::cout << "5. CreatePlay\n";
std::cout << "6. Quit\n";
}
else
{
std::cout << "No Play Loaded\n";
std::cout << "1. Load Play\n";
std::cout << "5. CreatePlay\n";
std::cout << "6. Quit\n";
}
// Note: No error handling
std::cin >> choice;
return (UserChoice) choice;
}
void LoadPlay(Play &iPlay)
{
char ch;
std::cout << "Enter filename of new play\n";
std::string PlayName;
std::cin >> PlayName;
PlayName += ".play";
iPlay.CurrentSales = 0;
std::ifstream myiFile;
myiFile.open(PlayName, std::ios::in | std::ios::binary);
myiFile.read((char*)&iPlay, sizeof(iPlay));
myiFile.close();
}
void SalesSummary(Play& currentPlay)
{
std::cout << "You chose SalesSummary()\n";
PressAnyKey();
}
time_t UTCToTimeT(std::string input)
{
struct tm tm;
std::istringstream iss(input);
iss >> std::get_time(&tm, "%m/%d/%Y %H:%M");
tm.tm_sec = 0;
return mktime(&tm);
}
void CreatePlay(Play &newPlay)
{
//Play newPlay;
std::cout << "You chose CreatePlay()\n";
std::cout << "Enter the name of Play (no spaces)\n-->";
std::cin >> newPlay.PlayName;
std::string PlayName = newPlay.PlayName;
PlayName += ".play";
std::cout << "Enter the number of regular seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Regular].Available;
std::cout << "Enter the price of regular seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Regular].Price;
std::cout << "Enter the number of balcony seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Balcony].Available;
std::cout << "Enter the price of balcony seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Balcony].Price;
std::cout << "Enter the number of orchestra seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Orchestra].Available;
std::cout << "Enter the price of orchestra seats\n-->";
std::cin >> newPlay.Seats[(int)SeatType::Orchestra].Price;
std::cout << "Enter the starting sale date in the form mm/dd/yyyy HH:MM (24 hour clock)\n-->";
std::string input;
std::cin.ignore();
std::getline(std::cin, input);
newPlay.StartTime = UTCToTimeT(input);
std::cout << "Enter the final sale date\n-->";
std::cin.ignore();
std::getline(std::cin, input);
newPlay.EndTime = UTCToTimeT(input);
std::ofstream myFile;
myFile.open(PlayName, std::ios::out | std::ios::binary);
myFile.write((char*)&newPlay, sizeof(newPlay));
myFile.close();
}
void FinalSales(Play& currentPlay)
{
std::cout << "You chose FinalSales()\n";
PressAnyKey();
}
void SellTickets(Play& currentPlay)
{
bool done = false;
while(!done)
{
ClearScreen();
std::cout << "Choose your seat type\n";
std::cout << "1. Regular Seats Available: " << currentPlay.Seats[(int)SeatType::Regular].Available << " @ "
<< currentPlay.Seats[(int)SeatType::Regular].Price << "\n";
std::cout << "2. Balcony Seats Available: " << currentPlay.Seats[(int)SeatType::Balcony].Available << " @ "
<< currentPlay.Seats[(int)SeatType::Balcony].Price << "\n";
std::cout << "3. Orchestra Seats Available: " << currentPlay.Seats[(int)SeatType::Orchestra].Available << " @ "
<< currentPlay.Seats[(int)SeatType::Orchestra].Price << "\n";
int choice;
std::cin >> choice;
choice -= 1; // for array indexing
std::cout << "Choose your seat quantity\n";
int quantity;
std::cin >> quantity;
if (currentPlay.Seats[choice].Available >= quantity)
{
std::cout << "Please Confirm... You're purchasing " << quantity << " tickets (y or n)\n";
char confirm;
std::cin >> confirm;
if (confirm == 'y')
{
// Note: No range checking on array access!
currentPlay.Seats[choice].Sold += quantity;
currentPlay.Seats[choice].Available -= quantity;
}
std::cout << std::setprecision(2) << std::fixed << "Reservation Complete\nYour total is: $"
<< currentPlay.Seats[choice].Price * quantity << "\n";
currentPlay.CurrentSales += std::ceil((currentPlay.Seats[choice].Price * quantity) * 100.0) / 100.0;
done = true;
}
else
{
std::cout << "We don't have that many tickets available\nTry Again\n";
PressAnyKey();
}
}
PressAnyKey();
}
void Quit(Play &iPlay)
{
// Save data file
std::string PlayName = iPlay.PlayName;
PlayName += ".play";
std::ofstream myFile;
myFile.open(PlayName, std::ios::out | std::ios::binary);
myFile.write((char*)&iPlay, sizeof(iPlay));
myFile.close();
std::cout << "You chose Quit()\n";
}
int main()
{
Play currentPlay;
bool quitting = false;
while (!quitting)
{
switch (ShowMainMenu(currentPlay))
{
case UserChoice::LoadPlay:
LoadPlay(currentPlay);
break;
case UserChoice::SalesSummary:
SalesSummary(currentPlay);
break;
case UserChoice::FinalSales:
FinalSales(currentPlay);
break;
case UserChoice::SellTickets:
SellTickets(currentPlay);
break;
case UserChoice::CreatePlay:
{
CreatePlay(currentPlay);
break;
}
case UserChoice::Quit:
Quit(currentPlay);
quitting = true;
break;
default:
std::cout << "You can't follow directions\n";
quitting = true;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment