summaryrefslogtreecommitdiff
path: root/oop/11-point-of-sale/src/main/java/lk/ac
diff options
context:
space:
mode:
Diffstat (limited to 'oop/11-point-of-sale/src/main/java/lk/ac')
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/App.java24
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManager.java16
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java52
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManager.java12
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java47
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSale.java15
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java49
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Bill.java79
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Cashier.java31
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Item.java43
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/LineItem.java53
-rw-r--r--oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java102
12 files changed, 523 insertions, 0 deletions
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/App.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/App.java
new file mode 100644
index 0000000..c646f5e
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/App.java
@@ -0,0 +1,24 @@
+package lk.ac.pdn.ceit.pos;
+
+import lk.ac.pdn.ceit.pos.ui.TextUI;
+
+/**
+ * Hello world!
+ */
+public class App {
+ public static void main(String[] args) {
+ // Create an ItemManager
+ ItemManager itemManager = new ItemManagerImpl();
+
+ // Create a BillManager
+ BillManager billManager = new BillManagerImpl(itemManager);
+
+ // Create a POS
+ PointOfSale pos = new PointOfSaleImpl(itemManager, billManager);
+
+ TextUI ui = new TextUI();
+ ui.setPos(pos);
+
+ ui.start();
+ }
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManager.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManager.java
new file mode 100644
index 0000000..7ac0e53
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManager.java
@@ -0,0 +1,16 @@
+package lk.ac.pdn.ceit.pos;
+
+import lk.ac.pdn.ceit.pos.entities.Bill;
+
+public interface BillManager {
+ public Bill createNewBill();
+
+ /**
+ * End the current bill.
+ */
+ public void saveBill();
+
+ public void addLineItem(String itemId, int quantity);
+
+ public void finishBillSession();
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java
new file mode 100644
index 0000000..11f7889
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java
@@ -0,0 +1,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;
+ }
+} \ No newline at end of file
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManager.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManager.java
new file mode 100644
index 0000000..3e31fdc
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManager.java
@@ -0,0 +1,12 @@
+package lk.ac.pdn.ceit.pos;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+import lk.ac.pdn.ceit.pos.entities.Item;
+
+public interface ItemManager {
+ public Item createItem(String id, String name, BigDecimal unitPrice);
+ public Item findById(String id);
+ public List<Item> searchItems(String q);
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java
new file mode 100644
index 0000000..ae325cd
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java
@@ -0,0 +1,47 @@
+package lk.ac.pdn.ceit.pos;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import lk.ac.pdn.ceit.pos.entities.Item;
+
+public class ItemManagerImpl implements ItemManager {
+
+ // For this demo. let's hold Items in memory.
+ private Map<String, Item> items = new HashMap<>();
+
+ public ItemManagerImpl() {
+ // For demo purposes, let's create some items (to be availalbe in system db)
+ createItem("F001", "Red Lentil (Dhal)", new BigDecimal("250.00"));
+ createItem("F002", "Sugar", new BigDecimal("270.00"));
+ createItem("S001", "Beauty Soap", new BigDecimal("165.00"));
+ }
+
+ @Override
+ public Item createItem(String id, String name, BigDecimal unitPrice) {
+ Item item = new Item(id, name, unitPrice);
+ items.put(id, item);
+ return item;
+ }
+
+ @Override
+ public Item findById(String id) {
+ return items.get(id);
+ }
+
+ @Override
+ public List<Item> searchItems(String q) {
+ List<Item> results = new ArrayList<>();
+
+ for (Item item : items.values()) {
+ if (item.getName().toLowerCase().contains(q.toLowerCase())) {
+ results.add(item);
+ }
+ }
+
+ return results;
+ }
+} \ No newline at end of file
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSale.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSale.java
new file mode 100644
index 0000000..d318af0
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSale.java
@@ -0,0 +1,15 @@
+package lk.ac.pdn.ceit.pos;
+
+import java.util.List;
+
+import lk.ac.pdn.ceit.pos.entities.Bill;
+import lk.ac.pdn.ceit.pos.entities.Item;
+
+public interface PointOfSale {
+ public Bill createNewBill();
+ public void addLineItem(String itemId, int quantity);
+ public void cashByCustomer(String amount);
+ public List<Item> searchItems(String q);
+ public void saveBill();
+ public void finishBillSession();
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java
new file mode 100644
index 0000000..b4d697d
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java
@@ -0,0 +1,49 @@
+package lk.ac.pdn.ceit.pos;
+
+import java.util.List;
+
+import lk.ac.pdn.ceit.pos.entities.Bill;
+import lk.ac.pdn.ceit.pos.entities.Item;
+
+public class PointOfSaleImpl implements PointOfSale {
+
+ private ItemManager itemManager;
+ private BillManager billManager;
+
+ public PointOfSaleImpl(ItemManager itemManager, BillManager billManager) {
+ this.itemManager = itemManager;
+ this.billManager = billManager;
+ }
+
+ @Override
+ public Bill createNewBill() {
+ return billManager.createNewBill();
+ }
+
+ @Override
+ public void addLineItem(String itemId, int quantity) {
+ billManager.addLineItem(itemId, quantity);
+ }
+
+ @Override
+ public void cashByCustomer(String amount) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException("Unimplemented method 'cashByCustomer'");
+ }
+
+ @Override
+ public List<Item> searchItems(String q) {
+ return itemManager.searchItems(q);
+ }
+
+ @Override
+ public void saveBill() {
+ billManager.saveBill();
+ }
+
+ @Override
+ public void finishBillSession() {
+ billManager.finishBillSession();
+ }
+
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Bill.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Bill.java
new file mode 100644
index 0000000..c78f88b
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Bill.java
@@ -0,0 +1,79 @@
+package lk.ac.pdn.ceit.pos.entities;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Bill {
+
+ private List<LineItem> lineItems = new ArrayList<>();
+ private Cashier cashier;
+
+ private int id;
+ private BigDecimal total = new BigDecimal("0.00");
+ private BigDecimal tax;
+ private BigDecimal cashByCustomer;
+ private BigDecimal balance;
+
+ public Bill() {
+ }
+
+ public Cashier getCashier() {
+ return cashier;
+ }
+
+ public void setCashier(Cashier cashier) {
+ this.cashier = cashier;
+ }
+
+ public int getId() {
+ return id;
+ }
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public BigDecimal getTotal() {
+ return total;
+ }
+
+ public BigDecimal getTax() {
+ return tax;
+ }
+
+ public void setTax(BigDecimal tax) {
+ this.tax = tax;
+ }
+
+ public BigDecimal getCashByCustomer() {
+ return cashByCustomer;
+ }
+ public void setCashByCustomer(BigDecimal cashByCustomer) {
+ this.cashByCustomer = cashByCustomer;
+ }
+
+ public BigDecimal getBalance() {
+ return balance;
+ }
+
+ @Override
+ public String toString() {
+ return "Bill [id=" + id + ", total=" + total + ", tax=" + tax + ", cashByCustomer=" + cashByCustomer
+ + ", balance=" + balance + "]";
+ }
+
+ public void addLineItem(LineItem lineItem) {
+ lineItems.add(lineItem);
+
+ // Update total
+ for (LineItem _lineItem : lineItems) {
+ total = total.add(_lineItem.getUnitPrice().multiply(BigDecimal.valueOf(_lineItem.getQuantity())));
+ }
+ }
+
+ public List<LineItem> getLineItems() {
+ // TODO: Do a deep copy and return to avoid external modification of line items.
+ // We return an immutable copy to avoid external world adding LineItems.
+ return List.copyOf(lineItems);
+ }
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Cashier.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Cashier.java
new file mode 100644
index 0000000..f951a9c
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Cashier.java
@@ -0,0 +1,31 @@
+package lk.ac.pdn.ceit.pos.entities;
+
+public class Cashier {
+ private String id;
+ private String name;
+
+ public Cashier() {
+ }
+
+ public Cashier(String id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Item.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Item.java
new file mode 100644
index 0000000..ecf6bea
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Item.java
@@ -0,0 +1,43 @@
+package lk.ac.pdn.ceit.pos.entities;
+
+import java.math.BigDecimal;
+
+public class Item {
+ private String id;
+ private String name;
+ private BigDecimal unitPrice;
+
+ public Item() {
+ }
+
+ public Item(String id, String name, BigDecimal unitPrice) {
+ this.id = id;
+ this.name = name;
+ this.unitPrice = unitPrice;
+ }
+
+ public String getId() {
+ return id;
+ }
+ public void setId(String id) {
+ this.id = id;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public BigDecimal getUnitPrice() {
+ return unitPrice;
+ }
+ public void setUnitPrice(BigDecimal unitPrice) {
+ this.unitPrice = unitPrice;
+ }
+
+ @Override
+ public String toString() {
+ return "Item [id=" + id + ", name=" + name + ", unitPrice=" + unitPrice + "]";
+ }
+
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/LineItem.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/LineItem.java
new file mode 100644
index 0000000..8c53ec4
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/LineItem.java
@@ -0,0 +1,53 @@
+package lk.ac.pdn.ceit.pos.entities;
+
+import java.math.BigDecimal;
+
+public class LineItem {
+
+ private String itemId;
+ private String itemName;
+ private int quantity;
+ private BigDecimal unitPrice;
+
+ public LineItem() {
+ }
+
+ public LineItem(String itemId, String itemName, int quantity, BigDecimal unitPrice) {
+ this.itemId = itemId;
+ this.itemName = itemName;
+ this.quantity = quantity;
+ this.unitPrice = unitPrice;
+ }
+
+ public String getItemId() {
+ return itemId;
+ }
+ public void setItemId(String itemId) {
+ this.itemId = itemId;
+ }
+ public String getItemName() {
+ return itemName;
+ }
+ public void setItemName(String itemName) {
+ this.itemName = itemName;
+ }
+ public int getQuantity() {
+ return quantity;
+ }
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+ public BigDecimal getUnitPrice() {
+ return unitPrice;
+ }
+ public void setUnitPrice(BigDecimal unitPrice) {
+ this.unitPrice = unitPrice;
+ }
+
+ @Override
+ public String toString() {
+ return "LineItem [itemId=" + itemId + ", itemName=" + itemName + ", quantity=" + quantity + ", unitPrice="
+ + unitPrice + "]";
+ }
+
+}
diff --git a/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java
new file mode 100644
index 0000000..874c074
--- /dev/null
+++ b/oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java
@@ -0,0 +1,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.");
+ }
+} \ No newline at end of file