Question
//the bill is found, just increment its count //get the index and the iterator in the same way we did it above unsigned int index
//the bill is found, just increment its count //get the index and the iterator in the same way we did it above unsigned int index = b_it - bills.begin(); vector
void registerT :: removeBillsFromRegister(vector
//billsToReturn contains the bills to return from each // type of bills. bool registerT :: computeBillsToReturn (double change, vector
// //The idea is simple //Simulate what "amoo il dikanji" does //Start from the highest available bill whose value is <= change // Give the customer one of those bills. // Reduce change. // Repeat until change drops below bills[0].value // If your reach your last drawer and change is still > bills[0].value // return false declaring failure // else // remove the billsToReturn from your drawers // return true for (int i = bills.size() -1; i >= 0; i--) { billT & bill = bills[i]; // the following loop can be replaced by a simple computation while (bill.value <= change && count[i] > 0) { change -= bill.value; billsToReturn[i]++; } if (change < bills[0].value){ break;// early exit if done. } } if (change < bills[0].value) { return true; } return false; }
bool registerT :: handleCustomer(customerT & cust) { double tot = cust.b.total(); if (cust.cash.value < tot) { return false;// the customer does not have enough cash } double change = cust.cash.value - tot;
// allocated a vector similar to count that holds // the count of each bill to return to the customer vector
void registerT :: printRegister() { // the assert statement below protects against mistakes in addBill // or in case someone inserts items into either vector using another // interface assert(bills.size() == count.size()); unsigned int sum = 0; cout << "register has " << bills.size() << " bills as follows: " << endl; for(int i = 0; i < bills.size(); i++) { cout << " " << count[i] << " bills of value " << bills[i].value << " " << bills[i].unit << endl; sum+= count[i]*bills[i].value; } cout << " --> for a total of: " << sum << " " << bills[0].unit << endl; }
void storeT :: updateStoreItems(basketT & b) { for (unsigned int i = 0; i < b.items.size(); i++) { storeItemT needle; needle.name = b.items[i].name; vector
void storeT :: run() { while (true) { for (unsigned int i = 0; i < registers.size(); i++) { registerT & reg = registers[i]; cout << "register " << i << " has " << reg.customers.size() << " customers" << endl; if (reg.customers.size() > 0) { // get a customer at the register customerT & cust = reg.customers.back(); //handle the customer if (reg.handleCustomer(cust)) { // if the customer is handled correctly // remove the items of the basket from the // store inventory updateStoreItems(cust.b); } // remove the customer from the vector reg.customers.pop_back(); } } } }
#if 0 int main(int argc, char ** argv) { basketT b; b.total(); //b.total1();//testing the for_each example //b.total2();//testing the for_each example registerT r;
r.buildRegisterRandomly(); r.printRegister(); vector
storeT store; store.buildStoreRandomly(); // store.run();
return 0; } #endif
any other way to write this code please ?
c++
URGENT
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started