Implementation Requirements You must use the following features or structures of
ID: 666974 • Letter: I
Question
Implementation Requirements You must use the following features or structures of the C++ programming language At least one (1) instance of inheritance using your own classes At least one (1) use of polymorphism and at least one (1) use of composition Initialization of class data must be performed in class constructors The array class template Pointer data types and the C++ String class At least one (1) instance of operator overloading other than stream input or output At least one (l) use of an STL container with associated iterator and algorithms Exception handling must be implemented to validate user inputs All customer, account, cart and inventory data must be stored in random access binary files (.dat Inventory control input and output should be implemented with a simple text-based console window User Input Definitions Customer requests Individual shopping actions for each customer must be batched together into "request" text files. Each file name must end with a sequence number that indicates the order in which files must be processed by your application (e.g REQUEST01.TXT, REQUEST02.TXT, You must pre-generate enough sample request files to adequately test your application and all of its functions REQUEST01.TXT REQUEST02.TXT ADD: pples CARTREPORT: 20102245 ADD: erea CREDIT 20102245,500.00 REMOVE: Apples, ADD: Bread,3 ADD: Milk, 1 CHECKOUT: 20102245, [Cereal,-0.50] Bread,-0.75] Request Formats (text file content) Adding a food item to a cart ADD:customer number, food item name, quantity Removing a food item from a cart REMOVE: customer number, food item name, quantity Creation of shopping cart reports CARTREPORT customer number Customer checkout CHECKOUT customer number, coupon list Add money to debit account CREDIT customer number,money amount Notes: l. coupon list" is a list of food item money amount pairs. Each pair is enclosed in square brackets and values separated with a comma (e.g., Apples,-1.00 Milk,-0.750 2. customer number is an 8-digit value (e.g., 23738495) Inventory Control User Interface The user interface for the inventory control must be implemented with a simple text-based console window. Use basic stream input and output (cin and cout) to prompt the user for input and display resultsExplanation / Answer
account.cpp
#include <string>
#include "account.h"
using namespace std;
account::account() : accountName(""), accountBalance(0.00), accountNumber(0)
{
cart userCart;
cart * const cartPtr = &userCart;
}
account::account(int accountNumber, const string & accountName, double accountBalance) {
setAccountBalance(accountBalance);
setAccountName(accountName);
setAccountNumber(accountNumber);
cart userCart;
//cart * const cartPtr = &userCart;
}
account::~account(){ }
void account::setAccountBalance(double balance) {
accountBalance = balance;
}
double account::getAccountBalance() const {
return accountBalance;
}
void account::setAccountNumber(int accountNum) {
accountNumber = accountNum;
}
int account::getAccountNumber() const {
return accountNumber;
}
void account::setAccountName(const std::string& accName){
int len = (int)accName.size();
len = ( len < 15 ? len : 14 );
accName.copy( accountName, len);
accountName[len] = '';
//accountName = accName;
}
std::string account::getAccountName() const {
return accountName;
}
void account::addToCart(food::food food){
userCart.addToCart(food);
}
account.h
#ifndef __account__
#define __account__
#include <iostream>
#include <string>
#include "cart.h"
class account : public cart
{
public:
account();
account(int accountNumber, const std::string& accountName, double accountBalance);
//account(account const &);
~account();
void setAccountNumber(int accountNumber);
int getAccountNumber() const;
void setAccountBalance(double accountBalance);
double getAccountBalance() const;
void setAccountName(const std::string& accountName);
std::string getAccountName() const;
void addToCart(food);
void removeFromCart(food);
double getTotal() const;
void toString();
private:
double accountBalance;
int accountNumber;
// std::string accountName;
char accountName[15];
cart userCart;
cart * const cartPtr = &userCart;
};
#endif
cart.cpp
#include <iostream>
#include <string>
#include <vector>
#include "food.h"
#include "cart.h"
using namespace std;
cart::cart(){
std::vector<food> cartContents;
}
cart::~cart(){
cartContents.clear();
//delete cartPointer;
}
void cart::addToCart(food::food food, int qty) {
food.setFoodQuantity(qty);
//cartTotal = cartTotal + (food.getFoodCost() * food.getFoodQuantity());
cartContents.push_back(food);
}
void cart::addToCart(food::food food) {
//cartTotal = cartTotal + (food.getFoodCost() * food.getFoodQuantity());
cartContents.push_back(food);
}
double cart::getCartTotal() const {
double cartTotal = 0.00;
for (int i=0; i < cartContents.size(); i++){
cartTotal = cartTotal + (cartContents[i].getFoodQuantity() * cartContents[i].getFoodCost());
}
return cartTotal;
}
void cart::clearCart() {
cartContents.clear();
}
cart.h
#ifndef __cart__
#define __cart__
#include <iostream>
#include <string>
#include <vector>
#include "food.h"
class cart {
public:
cart();
~cart();
cart * returnCartPointer();
void addToCart(food::food, int quantity);
void addToCart(food::food);
double getCartTotal() const;
void clearCart();
void toString();
private:
food foodItem();
//account currentAccount();
cart * cartPointer;
std::vector<food> cartContents;
std::vector<food> * const cartPtr = &cartContents;
int foodQty;
double cartTotal;
};
#endif
food.cpp
#include <fstream>
#include <iomanip>
#include <string>
#include "food.h"
using namespace std;
food::food() : foodName(""), foodQuantity(0), foodCost(0.00)
{
}
food::food(const string &foodName) : foodQuantity(0), foodCost(0.00)
{
setFoodName(foodName);
}
food::food( const string &foodName, int quantity) : foodCost(0.00)
{
setFoodName(foodName);
setFoodQuantity(quantity);
}
food::food ( const string &foodName, int quantity, double foodPrice)
{
setFoodName(foodName);
setFoodCost(foodPrice);
setFoodQuantity(quantity);
}
food::~food(){
}
void food::setFoodName(const string &foodN){
int len = foodN.size();
len = ( len < 10 ? len : 9 );
foodN.copy( foodName, len);
foodName[len] = '';
// foodName = name;
}
string food::getFoodName() const {
return foodName;
}
void food::setFoodQuantity(int quantity) {
foodQuantity = quantity;
}
int food::getFoodQuantity() const {
return foodQuantity;
}
void food::setFoodCost(double cost) {
foodCost = cost;
}
void food::toString() {
std::cout << "food name: " << this->getFoodName() << std::endl
<< "quantity: " << this->getFoodQuantity() << std::endl
<< "cost per: " << this->getFoodCost() << std::endl;
}
double food::getFoodCost() const {
if (foodCost <= 0.00){
}
return foodCost;
}
double food::getTotalFoodCost() const {
return (foodCost * foodQuantity);
}
food.h
#ifndef __food__
#define __food__
#include <iostream>
#include <string>
class food {
public:
food();
food (const std::string & );// int = 0, double = 0.0
food (const std::string & , int);
food( const std::string &, int, double);
~food();
void setFoodQuantity(int quantity);
void setFoodName(const std::string &);
void setFoodCost(double cost);
double getFoodCost() const;
double getTotalFoodCost() const;
std::string getFoodName() const;
int getFoodQuantity() const;
void toString();
private:
//std::string foodName;
char foodName[10];
int foodQuantity;
double foodCost;
};
#endif
inventory.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include "inventory.h"
#include "food.h"
inventory::inventory(){
//upon construction load the inventory file
std::ifstream inOut("/Users/tj/Documents/cis2252/cis2252_final_project/cis2252_final_project/backend/MAININVENTORY.TXT",
std::ios::in | std::ios::binary);
if (!inOut) {
std::cerr << "INVENTORY FILE MISSING SHOULD BE LOCATED IN ./backend/ SUBDIRECTORY FROM WHICH PROGRAM WAS EXECUTED"
<< std::endl;
exit (EXIT_FAILURE);
}
std::string buffer;
while (std::getline(inOut, buffer)) {
//finding locations split by different delimiters
size_t foodLocation = buffer.find_first_of(",");
size_t quantityLocation = buffer.find_last_of(",");
food currentFood( buffer.substr(0,foodLocation),
stoi(buffer.substr(foodLocation +1, quantityLocation - foodLocation -1)),
stod(buffer.substr(quantityLocation+1)));
currentInventory.push_back(currentFood);
//std::cout << currentFood.getFoodName();
}
currentInventory.shrink_to_fit();
inOut.close();
inventorySize = (int)currentInventory.size();
}
inventory::~inventory(){
//code to save the inventory here
saveInventory();
}
int inventory::getInventorySize() const {
return inventorySize;
}
void inventory::addItem(){
std::string name;
int quantity;
double cost;
std::cout <<"please enter the name for the new item" << std::endl << "> ";
std::cin >> name;
std::cout <<"please enter the quantity" << std::endl << "> ";
std::cin >> quantity;
std::cout <<"please enter the cost of the new item" << std::endl << "> ";
std::cin >> cost;
food newItem(name,quantity,cost);
currentInventory.push_back(newItem);
}
void inventory::report() {
std::cout << " contents in inventory" << std::endl;
std::cout << std::left << std::setw(10)
<< "Item:" << std::setw(16)
<< "Quantity:" << std::setw(11) << std::setprecision(3)
<< "Price:" <<std::endl;
for ( int i = 0; i < currentInventory.size(); ++i){
std::cout << std::left << std::setw(10) << currentInventory[i].getFoodName()
<< std::setw(16) << currentInventory[i].getFoodQuantity()
<< std::setw(11) << std::setprecision(2) << std::fixed << std::showpoint
<< currentInventory[i].getFoodCost() << std::endl;
}
}
void inventory::indvReport() {
bool success = 1;
std::cout << "please enter an item you wish to search for " << std::endl << "> ";
std::string input ("");
std::cin >> input;
for (int i = 0; i < currentInventory.size(); ++i) {
if (currentInventory[i].getFoodName().compare(input) == 0) {
success = 1;
std::cout << " " << std::left << std::setw(10)
<< "Item:" << std::setw(16)
<< "Quantity:" << std::setw(11) << std::setprecision(2) << std::right
<< "Price:" <<std::endl;
std::cout << std::left << std::setw(10) << currentInventory[i].getFoodName()
<< std::setw(16) << currentInventory[i].getFoodQuantity()
<< std::setw(11) << std::setprecision(2) << std::right << std::fixed << std::showpoint
<< currentInventory[i].getFoodCost() << " " << std::endl;
}
}
}
void inventory::toString() {
//std::cout << "contents in inventory" << std::endl;
for ( int i = 0; i < currentInventory.size(); ++i){
std::cout << "food name: " << currentInventory[i].getFoodName()
<< std::endl
<< "quantity: " << currentInventory[i].getFoodQuantity() << std::endl
<< "cost per: " << currentInventory[i].getFoodCost() << std::endl;
}
}
std::string inventory::getFoodName() const{
return foodName;
}
double inventory::searchForPrice(const std::string &foodName) {
double stupid = 0.00;
for (int j = 0; j < currentInventory.size(); j++) {
std::cout <<std::endl<<"comparing:::" << currentInventory[j].getFoodName()<< std::endl<< " with "<< foodName;
if (currentInventory[j].getFoodName() == foodName) {
std::cout<<" we have a match: " <<currentInventory[j].getFoodCost()<<std::endl;
stupid = currentInventory[j].getFoodCost();
}
}
return stupid;
}
void inventory::saveInventory() {
std::ofstream outPutFile("/Users/tj/Documents/cis2252/cis2252_final_project/cis2252_final_project/backend/MAININVENTORY.TXT",
std::ios::in | std::ios::binary);
if (!outPutFile) {
std::cerr << "INVENTORY FILE MISSING SHOULD BE LOCATED IN ./backend/ SUBDIRECTORY FROM WHICH PROGRAM WAS EXECUTED"
<< std::endl;
exit (EXIT_FAILURE);
}
for (int j= 0; j < currentInventory.size(); ++j) {
outPutFile << currentInventory[j].getFoodName() <<',' << currentInventory[j].getFoodQuantity() << ',' << currentInventory[j].getFoodCost() << std::endl;
}
}
inventory.h
#ifndef __inventory__
#define __inventory__
#include <iostream>
#include <string>
#include <vector>
#include "food.h"
class inventory {
public:
inventory();
~inventory();
void setFoodQty( const std::string &, int);
void setFoodPrice ( double price);
void setFoodName ( const std::string &);
void toString();
void addItem();
void saveInventory();
std::string getFoodName() const;
int getFoodQuantity() const;
int getInventorySize() const;
void report();
void indvReport();
double searchForPrice(const std::string &);
private:
std::vector <food> currentInventory;
char foodName[10];
food foodItem;
int foodQty;
int inventorySize;
double foodPrice;
};
#endif
main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <time.h>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include "cart.h"
#include "food.h"
#include "inventory.h"
#include "account.h"
using namespace std;
int enterChoice();
int inventoryChoice();
void outputLine(int , string, double);
const std::string currentDate();
void batchRequests();
vector<account> loadAccounts();
void inventoryControl();
enum choices { REQUEST = 1, INVOICE = 2, PROCESS = 3, INVEN_CONTROLS = 4, END = 5 };
enum comms { ADD = 1, REMOVE, CARTREPORT, CHECKOUT, CREDIT };
enum choices2 { ALL = 1, SINGLE , RESTOCK , ADDITEM, BACK };
inventory storeTotal;
//inventory * const invtPtr = &storeTotal;
int main()
{
int menuChoice;
while ( ( menuChoice = enterChoice()) != END ) {
switch (menuChoice) {
case REQUEST:
cout << "please enter the txt file to import for processing" << endl << "(exit will exit program)"
<< endl << "> ";
batchRequests();
break;
case INVOICE:
cout <<"processing transactions and updating inventory please wait..."<< endl;
break;
case PROCESS:
cout << "doing work please wait while reciept is printed to screen"<< endl;
break;
case INVEN_CONTROLS:
//system("clr");
cout << " ...loading inventory controls... " << endl;
inventoryControl();
break;
default:
cerr << "Incorrect choice!"<< endl << "> ";
break;
}
}
}
int inventoryMenu() {
cout << "would you like to: "<< endl;
cout <<"1.) list all items in inventory" << endl;
cout <<"2.) list a single item" << endl;
cout <<"3.) restocking" << endl;
cout <<"4.) add new items" << endl;
cout <<"5.) return" << endl << "> ";
int choice2;
cin >> choice2;
return choice2;
}
void inventoryControl() {
int cho;
while ( (cho = inventoryMenu()) != BACK) {
switch (cho) {
case ALL:
storeTotal.report();
break;
case SINGLE:
storeTotal.indvReport();
break;
case RESTOCK:
break;
case ADDITEM:
storeTotal.addItem();
break;
case BACK:
break;
default:
cerr << "incorrect choice!"<< endl << "> ";
break;
}
}
}
void outputLine(int account, string name, double balance) {
cout << left << setw(10) << account << setw(13) << name << setw(7) << setprecision(2) << right << balance <<endl;
}
int enterChoice() {
cout << " Welcome to the store! " << endl << "today is: " << currentDate()<< endl;
cout << "1.) Send a request file" << endl;
cout << "2.) view invoice" << endl;
cout << "3.) process order" << endl;
cout << "4.) inventory controls" << endl;
cout << "5.) quit" <<endl << "> ";
int choice1;
cin >> choice1;
return choice1;
}
const std::string currentDate() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now); //pointing
//extract just the date from the system time
strftime(buf, sizeof(buf), "%x", &tstruct);
return buf;
}
void batchRequests() {
inventory storesUp;
string requestPath;
cin >> requestPath;
if (requestPath == "exit") {
exit(3);
}
ifstream inOut;
inOut.clear();
inOut.open( requestPath.c_str() );
while (inOut.fail()) {
if (requestPath == "exit") {
exit(3);
}
inOut.clear();
cerr << "File not found"<< endl << "please enter the absolute file path"<<endl<<"> ";
cin >> requestPath;
inOut.open(requestPath.c_str());
}
vector<account> accounts = loadAccounts();
//cart myCart;
vector<string> commands;
string line;
//adds each line to a vector for processing
while (std::getline(inOut, line)) {
commands.push_back(line);
}
for (int i=0; i<commands.size(); i++) {
//work space variables
string newCommand;
int accountNumber;
string foodToAdd;
int qtyToAdd;
//finding locations split by different delimiters
size_t commandLocation = commands[i].find_first_of(":");
size_t accountLocation = commands[i].find_first_of(",");
size_t foodLocation = commands[i].find_last_of(",");
//parsing the commands
newCommand = commands[i].substr(0,commandLocation);
accountNumber = stoi(commands[i].substr(commandLocation +1, accountLocation - commandLocation -1));
foodToAdd = commands[i].substr(accountLocation +1, foodLocation - accountLocation -1);
qtyToAdd = stoi(commands[i].substr(foodLocation+1));
food workingFood(foodToAdd,qtyToAdd);
for ( int i = 0; i < accounts.size(); ++i){
if (accountNumber == accounts[i].getAccountNumber()) {
double test = storesUp.searchForPrice(workingFood.getFoodName());
workingFood.setFoodCost(storesUp.searchForPrice(workingFood.getFoodName()));
accounts[i].addToCart(workingFood);
cout << endl << endl << test <<endl;
}
}
}
inOut.close();
}
vector<account> loadAccounts() {
vector<account> acc;
std::ifstream inOut("/Users/tj/Documents/cis2252/cis2252_final_project/cis2252_final_project/backend/CUSTOMERS.TXT",
std::ios::in);
if (!inOut) {
std::cerr << "CUSTOMER FILE MISSING SHOULD BE LOCATED IN ./backend/ SUBDIRECTORY FROM WHICH PROGRAM WAS EXECUTED"
<< std::endl;
exit (EXIT_FAILURE);
}
std::string buffer;
while (std::getline(inOut, buffer)) {
//finding locations split by different delimiters
size_t accountNumberLocation = buffer.find_first_of(":");
size_t creditLocation = buffer.find_last_of(",");
account currentAccount( stoi(buffer.substr(0,accountNumberLocation)),
(buffer.substr(accountNumberLocation +1, creditLocation - accountNumberLocation -1)),
stod(buffer.substr(creditLocation+1)));
acc.push_back(currentAccount);
}
inOut.close();
return acc;
}