Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//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 :: iterator c_it = count.begin() + index; //increment count unsigned int & c = *c_it; c+=num; } assert(bills.size() == count.size()); }

void registerT :: removeBillsFromRegister(vector & billsToReturn) { assert(count.size() == billsToReturn.size()); for (int i = count.size() -1; i >= 0; i--) { assert (count[i] >= billsToReturn[i]); count[i] -= billsToReturn[i]; } }

//billsToReturn contains the bills to return from each // type of bills. bool registerT :: computeBillsToReturn (double change, vector & billsToReturn) { //This function computes the minimum number of bills needed // to pass to the customer as change. // It is successful and returns true in case change drops less // than the value of the lowest bill (250LL in this case). // It fails otherwise. // //In case the function is successful count is updated // by subtracting billsToReturn from it.

// //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 billsToReturn; // initialize it to 0, and count.size items billsToReturn.resize(count.size(), 0); if (computeBillsToReturn(change, billsToReturn) ) { addBill(cust.cash,1);// add the money from the customer to the register removeBillsFromRegister(billsToReturn); return true; } else { // can not pay the customer back. // We need this so that // transactions is canceled and items // should not be removed from store items. return false; } }

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::iterator it = lower_bound(items.begin(), items.end(), needle); storeItemT & foundItem = *it; // some business logic is not fully correct here // find the problem! assert(foundItem.available); foundItem.available--; if (foundItem.available < 5) { cout << "Store inventory request: refill " << foundItem.name << endl; } } }

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 billsToReturn; billsToReturn.resize(r.count.size(),0); if ( r.computeBillsToReturn(27650, billsToReturn) ){ r.removeBillsFromRegister(billsToReturn); cout << "success" << endl; } r.printRegister();

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago