summaryrefslogtreecommitdiff
path: root/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java
blob: 11f78896566e724150604f20bb6659b855a37862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
    }
}