summaryrefslogtreecommitdiff
path: root/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java
blob: 874c0747ea69f9bccd16d9c2d4acad68e124a8ad (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package lk.ac.pdn.ceit.pos.ui;

import java.util.List;

import lk.ac.pdn.ceit.pos.PointOfSale;
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 TextUI {
    private PointOfSale pos;
    private Bill bill; // Current bill

    private void printBill() {
        // Print top part of the bill
        IO.println(bill); // TODO: Only print suitable parts for the top of the bill.

        // Print line items
        for (LineItem lineItem : bill.getLineItems()) {
            IO.println(lineItem);
        }

        // TODO: Print total, cash given, balance, etc.
    }

    public void setPos(PointOfSale pos) {
        this.pos = pos;
    }

    public void start() {
        IO.println("POS Starting...");
        printHelp();

        while (true) {
            String response = IO.readln("POS> ");

            switch (response.trim().toLowerCase()) {
                case "q":
                case "quit":
                    IO.println("Bye bye!");
                    System.exit(0);

                case "help":
                    printHelp();
                    break;
                    
                case "create":
                    bill = pos.createNewBill();
                    IO.println("New bill created. Bill id: " + bill.getId());
                    break;
                
                case "search":
                    String q = IO.readln("Enter search query (item name): ");
                    List<Item> items = pos.searchItems(q);

                    IO.println("Search results: ");

                    for (Item item : items) {
                        IO.println(item);
                    }
                    break;
                
                case "item":
                    String itemId = IO.readln("Enter item Id: ");
                    String quantity = IO.readln("Enter quantity: ");
                    pos.addLineItem(itemId, Integer.parseInt(quantity));
                    break;
                
                case "save":
                    pos.saveBill();
                    IO.println("Bill saved.");
                    break;
                
                case "finish":
                    pos.finishBillSession();
                    this.bill = null;
                    IO.println("Bill session finished.");
                    break;

                case "print":
                    printBill();
                    break;
            
                default:
                    IO.println("Command not understood.");
                    break;
            }

        }
    }

    private void printHelp() {
        IO.println("Commands available:");
        IO.println("  q or quit - Quit the program.");
        IO.println("  help - Prints this help.");
        IO.println("  create - Create a new bill.");
        IO.println("  item - Enter new item details to be purchased.");
        IO.println("  save - Save the current bill in the system.");
        IO.println("  print - Print the bill.");
        IO.println("  finish - Finish current bill session.");
    }
}