package lk.ac.pdn.ceit.pos; import lk.ac.pdn.ceit.pos.entities.Bill; import lk.ac.pdn.ceit.pos.entities.Item; import lk.ac.pdn.ceit.pos.entities.LineItem; public class BillManagerImpl implements BillManager { private ItemManager itemManager; private Bill currentBill; // Just for demo purpose. In real world, a database call may be used for this purpose. private int lastBillId = 0; public BillManagerImpl(ItemManager itemManager) { this.itemManager = itemManager; } @Override public Bill createNewBill() { currentBill = new Bill(); lastBillId++; currentBill.setId(lastBillId); return currentBill; } @Override public void addLineItem(String itemId, int quantity) { if (currentBill == null) { throw new IllegalStateException("A bill has not been created so far. Neither a checked exception is thrown."); } Item item = itemManager.findById(itemId); if (item != null) { LineItem lineItem = new LineItem(item.getId(), item.getName(), quantity, item.getUnitPrice()); currentBill.addLineItem(lineItem); } } @Override public void saveBill() { // TODO: Save the currentBill in DB. } @Override public void finishBillSession() { currentBill = null; } }