private void PayBill(Bill b) { // Bill b = bills.get(0); if (cashTotal >= (b.price * b.orderedSize)) { cashTotal -= b.price * b.orderedSize; b.market.msgPayment(b.price * b.orderedSize); b.state = Bill.BillState.done; } else { log.add(new LoggedEvent("Don't have enough money to pay")); Do( "I don't have enough money to pay to the " + b.market + " for the order " + b.food + " [extra creidt]"); b.state = Bill.BillState.unPaid; } }
private void returnChange(Check c) { int twentyDollar = 0; int tenDollar = 0; int fiveDollar = 0; int oneDollar = 0; int coins = 0; if (c.cash.totalAmount() >= c.price) { cashTotal += c.price; double change = c.cash.totalAmount() - c.price; Cash Change; twentyDollar = (int) change / 20; change -= 20 * twentyDollar; tenDollar = (int) change / 10; change -= 10 * tenDollar; fiveDollar = (int) change / 5; change -= 5 * fiveDollar; oneDollar = (int) change / 1; change -= 1 * oneDollar; coins = (int) (100 * ((double) (change) + 0.0001)); Change = new Cash(twentyDollar, tenDollar, fiveDollar, oneDollar, coins); Do("Change is " + dFormat.format(Change.totalAmount())); c.customer.msgChange(Change); } else { cashTotal += c.cash.totalAmount(); } checks.remove(c); // check bills if cashier earns money double sum = 0; for (Bill b : bills) { if (b.state == Bill.BillState.unPaid) { sum += (b.price * b.orderedSize); if (cashTotal >= sum) { log.add(new LoggedEvent("Now have enough money to pay")); Do("I can now pay to the " + b.market + " for the order " + b.food + " [extra creidt]"); b.state = Bill.BillState.nothing; } else { break; } } } }
/** Scheduler. Determine what action is called for, and do it. */ public boolean pickAndExecuteAnAction() { synchronized (checks) { if (!checks.isEmpty()) { for (int i = 0; i < checks.size(); i++) { if (checks.get(i).state == Check.CheckState.nothing) { checks.get(i).state = Check.CheckState.computing; ComputeBill(checks.get(i)); return true; } else if (checks.get(i).state == Check.CheckState.doneComputing) { giveCheckToWaiter(checks.get(i)); return true; } else if (checks.get(i).state == Check.CheckState.receivedCash) { checks.get(i).state = Check.CheckState.paid; returnChange(checks.get(i)); // checks.remove(i); return true; } } } } synchronized (pendingChecks) { if (!pendingChecks.isEmpty()) { for (Check chk : pendingChecks) { checks.add(chk); } pendingChecks.clear(); return true; } } synchronized (bills) { if (!bills.isEmpty()) { for (Bill b : bills) { if (b.state == Bill.BillState.nothing) { b.state = Bill.BillState.inProcess; PayBill(b); return true; } else if (b.state == Bill.BillState.done) { bills.remove(b); return true; } } } } return false; }