From 938427dace1a9622a55e3f81845fb82af697d001 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Fri, 20 Mar 2026 22:48:14 +0530 Subject: Added 11-point-of-sale sample application --- oop/11-point-of-sale/.gitignore | 1 + oop/11-point-of-sale/.mvn/jvm.config | 0 oop/11-point-of-sale/.mvn/maven.config | 0 oop/11-point-of-sale/.vscode/settings.json | 3 + oop/11-point-of-sale/README | 11 +++ oop/11-point-of-sale/docs/01-requirements.txt | 7 ++ oop/11-point-of-sale/docs/02-domain-model.uxf | 16 ++++ oop/11-point-of-sale/docs/03-design.uxf | 71 ++++++++++++++ oop/11-point-of-sale/pom.xml | 103 +++++++++++++++++++++ .../src/main/java/lk/ac/pdn/ceit/pos/App.java | 24 +++++ .../main/java/lk/ac/pdn/ceit/pos/BillManager.java | 16 ++++ .../java/lk/ac/pdn/ceit/pos/BillManagerImpl.java | 52 +++++++++++ .../main/java/lk/ac/pdn/ceit/pos/ItemManager.java | 12 +++ .../java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java | 47 ++++++++++ .../main/java/lk/ac/pdn/ceit/pos/PointOfSale.java | 15 +++ .../java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java | 49 ++++++++++ .../java/lk/ac/pdn/ceit/pos/entities/Bill.java | 79 ++++++++++++++++ .../java/lk/ac/pdn/ceit/pos/entities/Cashier.java | 31 +++++++ .../java/lk/ac/pdn/ceit/pos/entities/Item.java | 43 +++++++++ .../java/lk/ac/pdn/ceit/pos/entities/LineItem.java | 53 +++++++++++ .../main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java | 102 ++++++++++++++++++++ .../src/test/java/lk/ac/pdn/ceit/pos/AppTest.java | 19 ++++ 22 files changed, 754 insertions(+) create mode 100644 oop/11-point-of-sale/.gitignore create mode 100644 oop/11-point-of-sale/.mvn/jvm.config create mode 100644 oop/11-point-of-sale/.mvn/maven.config create mode 100644 oop/11-point-of-sale/.vscode/settings.json create mode 100644 oop/11-point-of-sale/README create mode 100644 oop/11-point-of-sale/docs/01-requirements.txt create mode 100644 oop/11-point-of-sale/docs/02-domain-model.uxf create mode 100644 oop/11-point-of-sale/docs/03-design.uxf create mode 100644 oop/11-point-of-sale/pom.xml create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/App.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManager.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/BillManagerImpl.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManager.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ItemManagerImpl.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSale.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/PointOfSaleImpl.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Bill.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Cashier.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/Item.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/entities/LineItem.java create mode 100644 oop/11-point-of-sale/src/main/java/lk/ac/pdn/ceit/pos/ui/TextUI.java create mode 100644 oop/11-point-of-sale/src/test/java/lk/ac/pdn/ceit/pos/AppTest.java (limited to 'oop') diff --git a/oop/11-point-of-sale/.gitignore b/oop/11-point-of-sale/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/oop/11-point-of-sale/.gitignore @@ -0,0 +1 @@ +target diff --git a/oop/11-point-of-sale/.mvn/jvm.config b/oop/11-point-of-sale/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/oop/11-point-of-sale/.mvn/maven.config b/oop/11-point-of-sale/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/oop/11-point-of-sale/.vscode/settings.json b/oop/11-point-of-sale/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/oop/11-point-of-sale/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/oop/11-point-of-sale/README b/oop/11-point-of-sale/README new file mode 100644 index 0000000..1fd7424 --- /dev/null +++ b/oop/11-point-of-sale/README @@ -0,0 +1,11 @@ +This sample Maven project shows how object oriented software is developed. + +The docs directory contains .uxf files that can be opened with UMLet diagram editor. UMLet is available as a Visual Studio Code extension. It is also available as a standalone application. + +To build the project, run: + + mvn clean package + +To run the application packaged earlier, execute: + + java -jar target/point-of-sale-1.0-SNAPSHOT.jar \ No newline at end of file diff --git a/oop/11-point-of-sale/docs/01-requirements.txt b/oop/11-point-of-sale/docs/01-requirements.txt new file mode 100644 index 0000000..4f99432 --- /dev/null +++ b/oop/11-point-of-sale/docs/01-requirements.txt @@ -0,0 +1,7 @@ +A POS system needs to be created. A customer comes to the cashier and the +cashier takes items from the cart and enters the item ids into the system. For +each item type, the system asks the number of items to be purchased. The +system creates a bill and adds line items to it. At the end, the total is +calculated and the tax should be added. The system should ask to enter the +amount of money the customer would give. The system should print the entire +bill with the balance to be given back to the customer. diff --git a/oop/11-point-of-sale/docs/02-domain-model.uxf b/oop/11-point-of-sale/docs/02-domain-model.uxf new file mode 100644 index 0000000..04dae9e --- /dev/null +++ b/oop/11-point-of-sale/docs/02-domain-model.uxf @@ -0,0 +1,16 @@ +10UMLClass45020010030CustomerUMLClass31010010030CashierUMLClass5052010080Item +-- +id: String +name: String +unitPriceUMLClass5033010030CartRelation8035040190lt=<<<<- +m2=*10;10;10;170UMLClass270340180100Bill +-- +id +total +tax +cashByCustomer +balanceUMLClass31053010040LineItem +-- +quantity: intRelation35043040120lt=<<<<<- +m2=*10;10;10;100UMLClass6010012030PointOfSaleSystemRelation14054019040lt=<- +m1=110;10;170;10 \ No newline at end of file diff --git a/oop/11-point-of-sale/docs/03-design.uxf b/oop/11-point-of-sale/docs/03-design.uxf new file mode 100644 index 0000000..8eaee19 --- /dev/null +++ b/oop/11-point-of-sale/docs/03-design.uxf @@ -0,0 +1,71 @@ +10Space for diagram notesUMLClass88047013070Item +-- +-id: String +-name: String +-unitPrice: BigDecimalUMLClass880340130100LineItem +-- +itemId: String +itemName: String +quantity: int +unitPrice: BigDecimal +Relation94025040110lt=<<<<<- +m2=*10;10;10;90UMLClass860140180120Bill +-- +id: int +total: BigDecimal +tax: BigDecimal +cashByCustomer: BigDecimal +balance: BigDecimal +-- +addLineItem(lineItem: LineItem)UMLClass900010060Cashier +-- +name: String +id: StringRelation9405030110lt=<-10;10;10;90UMLClass2702010060TextUI +-- +-- +-printBill() ++start()UMLClass48020250140<<interface>> +PointOfSale +-- +-- +createNewBill(): Bill +addLineItem(itemId: String, quantity: int) +cashByCustomer(amount: String) +searchItems(q: String): ListsavBill() +saveBill() +finishBillSession()Relation3603014030lt=<-120;10;10;10UMLClass490420240120<<interface>> +BillManager +-- +-- +createNewBill():Bill +addLineItem(itemId: String, quantity: int) +cashByCustomer(amount: String) +saveBill() +finishBillSession()Relation60032030120lt=<-10;100;10;10UMLClass89064010030TaxCalculatorUMLClass0370370100<<interface>> +ItemManager +-- +-- +createItem(id: String, name: String, unitPrice: BigDecimal): Item +findById(id: String): Item +searchItems(q: String): ListRelation180250320140lt=<-10;120;10;10;300;10UMLClass480210250120PointOfSaleImpl +-- +-- +createNewBill(): Bill +addLineItem(itemId: String, quantity: int) +cashByCustomer(amount: String) +searchItems(q: String): List +saveBill() +finishBillSession()Relation6001503080lt=<<.10;10;10;60UMLClass490600240120BillManagerImpl +-- +- currentBill:Bill +-- +createNewBill():Bill +addLineItem(itemId: String, quantity: int) +cashByCustomer(amount: String) +saveBill() +finishBillSession()Relation6005303090lt=<<.10;10;10;70UMLClass050037080ItemManagerImpl +-- +-- +createItem(id: String, name: String, unitPrice: BigDecimal) +findById(id: String): Item +searchItems(q: String): ListRelation1804603060lt=<<.10;10;10;40Relation36438019060lt=<-10;10;170;10;170;40 \ No newline at end of file diff --git a/oop/11-point-of-sale/pom.xml b/oop/11-point-of-sale/pom.xml new file mode 100644 index 0000000..65094ab --- /dev/null +++ b/oop/11-point-of-sale/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + lk.ac.pdn.ceit.pos + point-of-sale + 1.0-SNAPSHOT + + point-of-sale + + http://www.example.com + + + UTF-8 + 25 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + + maven-clean-plugin + 3.4.0 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + + true + lk.ac.pdn.ceit.pos.App + + + + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + \ No newline at end of file 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 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 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 searchItems(String q) { + List 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 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 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 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 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 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 diff --git a/oop/11-point-of-sale/src/test/java/lk/ac/pdn/ceit/pos/AppTest.java b/oop/11-point-of-sale/src/test/java/lk/ac/pdn/ceit/pos/AppTest.java new file mode 100644 index 0000000..2fb51b5 --- /dev/null +++ b/oop/11-point-of-sale/src/test/java/lk/ac/pdn/ceit/pos/AppTest.java @@ -0,0 +1,19 @@ +package lk.ac.pdn.ceit.pos; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +public class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } +} -- cgit v1.2.3