Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

/*-----------------------------------

Assignment 5: Easter Candy Database


Developer: Raegan Mozal
Date: March 17, 2022
-----------------------------------*/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//variables
char menuOption;
int candyType;
int peeps;
int totalPeeps;
int jellybeans;
int totalJellybeans;
int chocolate;
int totalChocolate;
int other;
int totalOther;
int totalCandy;
int candyAmount;

//function for displaying the menu;


void menu() { //void because nothing here is changing
cout<<"Options Menu:"<<endl;
cout<<"E - Eat a piece of candy."<<endl;
cout<<"F - You found an Egg! Get a piece of candy."<<endl;
cout<<"I - Current inventory list."<<endl;
cout<<"C - List Candy Types."<<endl;
cout<<"M - Print this command menu."<<endl;
cout<<"S - Save to the file easter.txt."<<endl;
cout<<"L - Load from the file easter.txt."<<endl;
cout<<"Q - Quit this program."<<endl;
}

//function for eating command


int eat(int candyType, int &candyAmount) { //pbr for candyAmount as that will
change based on users command
cout<<"Please enter candy type (1-4): ";
cin>>candyType;
if(candyType==1){
if(totalPeeps==0) { //nested statements to prevent a negative amount of candy
cout<<"Oops, you don't have enough candy!"<<endl;
} else {
totalPeeps--;
}
} else if(candyType==2) {
if(totalJellybeans==0) {
cout<<"Oops, you don't have enough candy!"<<endl;
} else {
totalJellybeans--;
}
} else if(candyType==3) {
if(totalChocolate==0) {
cout<<"Oops, you don't have enough candy!"<<endl;
} else {
totalChocolate--;
}
} else if(candyType==4) {
if(totalOther==0) {
cout<<"Oops, you don't have enough candy!"<<endl;
} else {
totalOther--;
}
} else {
cout<<"That is an invalid selection."<<endl;
}
return candyAmount;
}

//function for finding command


int find(int candyType, int &candyAmount) { //pbr for candyAmount same reasoning as
above
cout<<"Please enter candy type (1-4): ";
cin>>candyType;
if(candyType==1) { //don't need nested statements here becuase can't get a
negative in this case
totalPeeps++;
} else if(candyType==2) {
totalJellybeans++;
} else if(candyType==3) {
totalChocolate++;
} else if(candyType==4) {
totalOther++;
} else {
cout<<"That is an invalid selection."<<endl;
}
return candyAmount;

//function for inventory command


int inventory(int candyType, int &totalCandy) { //pbr for totalCandy as that will
change based on eating or finding commands
cout<<"Current Inventory: "<<endl;
cout<<"Peeps: "<<totalPeeps<<endl;
cout<<"Jellybeans: "<<totalJellybeans<<endl;
cout<<"Chocolate: "<<totalChocolate<<endl;
cout<<"Other: "<<totalOther<<endl;
totalCandy=totalPeeps+totalChocolate+totalJellybeans+totalOther;
cout<<"Total Amount: "<<totalCandy<<endl;
return totalCandy;
}

//function for types of candy available


void types() { //void because nothing here is changing
cout<<"Type of Candy Available:"<<endl;
cout<<"1. Peeps"<<endl;
cout<<"2. Jellybeans"<<endl;
cout<<"3. Chocolate"<<endl;
cout<<"4. Other"<<endl;
}

//function for saving to a text file


void save() { //void because nothing is changing here, only in the txt file
ofstream myFile;
myFile.open("easter.txt");
myFile<<"Peeps: "<<totalPeeps<<endl;
myFile<<"Jellybeans: "<<totalJellybeans<<endl;
myFile<<"Chocolate: "<<totalChocolate<<endl;
myFile<<"Other: "<<totalOther<<endl;
myFile<<"Total Candy: "<<totalCandy<<endl;;
myFile.close();
}

//function for loading the text file


void load() {
string line; //could not figure out how to print all lines without a string;
found in cplusplus.com references
ifstream myFile("easter.txt"); //left out ios::app so file can refresh with
every save
if(myFile.is_open()) {
while(getline (myFile, line)) {
cout<<line<<'\n';
}
myFile.close();
} else {
cout<<"Oops! That file is not available!"<<endl;
}
}

//main function
int main() {
cout<<"----------"<<endl;
cout<<"** My Candy Database **"<<endl;
menu(); //calling menu before loop begins
do{
cout<<"Please make a selection from the menu: ";
cin>>menuOption;
if(menuOption=='E' || menuOption=='e') { //statement that calls functions
based on input command
eat(candyType, candyAmount);
} else if(menuOption=='F' || menuOption=='f') {
find(candyType, candyAmount);
} else if(menuOption=='I' || menuOption=='i') {
inventory(candyType, totalCandy);
} else if(menuOption=='C' || menuOption=='c') {
types();
} else if(menuOption=='M' || menuOption=='m') {
menu();
} else if(menuOption=='S' || menuOption=='s') {
save();
} else if(menuOption=='L' || menuOption=='l') {
load();
} else if(menuOption=='Q' || menuOption=='q') {
cout<<"Don't eat too much all at once!"<<endl;
} else {
cout<<"That is not a valid selection. Try again!"<<endl;
}
} while(menuOption!='Q');

return 0;
}

You might also like