From 0b97cd0069fbfdb868e02dc2e80816305b3b5adf Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:14 +0300 Subject: [PATCH 1/6] Address Obj --- restaurant/Address.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 restaurant/Address.java diff --git a/restaurant/Address.java b/restaurant/Address.java new file mode 100644 index 0000000..e92ff52 --- /dev/null +++ b/restaurant/Address.java @@ -0,0 +1,29 @@ +package restaurant; + +public class Address { + private double latitude; + private double longitude; + private String writtenAddress; + + public Address(double latitude, double longitude, String writtenAddress) { + this.latitude = latitude; + this.longitude = longitude; + this.writtenAddress = writtenAddress; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public String getWrittenAddress() { + return writtenAddress; + } + + public double distanceFrom(Address other) { + return Math.sqrt(Math.pow(other.latitude - this.latitude, 2) + Math.pow(other.longitude - this.longitude, 2)); + } +} From 332c20cd7b923971d01c33047eb567bfcd4089e0 Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:24 +0300 Subject: [PATCH 2/6] Customer Obj --- restaurant/Customer.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 restaurant/Customer.java diff --git a/restaurant/Customer.java b/restaurant/Customer.java new file mode 100644 index 0000000..f9a735d --- /dev/null +++ b/restaurant/Customer.java @@ -0,0 +1,34 @@ +package restaurant; + +public class Customer { + private static int customerCounter = 0; + private final int customerNumber; + private String name; + private Address address; + + public Customer(String name, Address address) { + this.customerNumber = ++customerCounter; + this.name = name; + this.address = address; + } + + public int getCustomerNumber() { + return customerNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} From fde6355ae023d0ff921075092ce2817fc7fe90cd Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:30 +0300 Subject: [PATCH 3/6] Food Obj --- restaurant/Food.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 restaurant/Food.java diff --git a/restaurant/Food.java b/restaurant/Food.java new file mode 100644 index 0000000..16c1b8c --- /dev/null +++ b/restaurant/Food.java @@ -0,0 +1,28 @@ +package restaurant; + +import java.util.ArrayList; +import java.util.List; + +public class Food { + private static List menu = new ArrayList<>(); + private final String name; + private final int price; + + public Food(String name, int price) { + this.name = name; + this.price = price; + menu.add(this); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public static List getMenu() { + return menu; + } +} From 6d54ac6f259999e2b1cc8d1b110b14b1025375f4 Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:36 +0300 Subject: [PATCH 4/6] Invoice Obj --- restaurant/Invoice.java | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 restaurant/Invoice.java diff --git a/restaurant/Invoice.java b/restaurant/Invoice.java new file mode 100644 index 0000000..5cd7fa8 --- /dev/null +++ b/restaurant/Invoice.java @@ -0,0 +1,57 @@ +package restaurant; + +import java.util.ArrayList; +import java.util.List; + +public class Invoice { + private static final float TAX_RATE = 0.094f; + private int state = -1; + private final Customer customer; + private final List items = new ArrayList<>(); + + public Invoice(Customer customer) { + this.customer = customer; + } + + public int getState() { + return state; + } + + public Customer getCustomer() { + return customer; + } + + public boolean addItem(Item item) { + if (state == -1) { + items.add(item); + return true; + } + return false; + } + + public boolean removeItem(Item item) { + if (state == -1) { + return items.removeIf(i -> i.getFood().equals(item.getFood())); + } + return false; + } + + public void nextStage() { + if (state < 2) { + state++; + } + } + + public int getTotalPrice() { + int total = 0; + for (Item item : items) { + total += item.getFood().getPrice() * item.getCount(); + } + total = (int) Math.ceil(total * (1 + TAX_RATE)); + return total; + } + + public List getItems() { + return items; + } +} From 04b9de477fcaac7ea48c315dc92d7786396f3a69 Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:45 +0300 Subject: [PATCH 5/6] item Obj --- restaurant/Item.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 restaurant/Item.java diff --git a/restaurant/Item.java b/restaurant/Item.java new file mode 100644 index 0000000..d7c24a6 --- /dev/null +++ b/restaurant/Item.java @@ -0,0 +1,29 @@ +package restaurant; + +public class Item { + private final Food food; + private final int count; + private final String description; + + public Item(Food food, int count) { + this(food, count, ""); + } + + public Item(Food food, int count, String description) { + this.food = food; + this.count = count; + this.description = description; + } + + public Food getFood() { + return food; + } + + public int getCount() { + return count; + } + + public String getDescription() { + return description; + } +} From 0cd9721867615dfe59f841215dc898a3cfc01052 Mon Sep 17 00:00:00 2001 From: MelikaShirvani Date: Wed, 15 May 2024 15:51:51 +0300 Subject: [PATCH 6/6] main functionality --- restaurant/Restaurant.java | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 restaurant/Restaurant.java diff --git a/restaurant/Restaurant.java b/restaurant/Restaurant.java new file mode 100644 index 0000000..d1e8e72 --- /dev/null +++ b/restaurant/Restaurant.java @@ -0,0 +1,59 @@ +package restaurant; + +public class Restaurant { + public static void main(String[] args) { + Address a1 = new Address(2, 3, "Lahijan"); + Address a2 = new Address(5, 6, "Tehran"); + + System.out.println("\n--------------------------------------\n"); + System.out.println("Distance From Address a1 to a2 is: " + a1.distanceFrom(a2)); + System.out.println("\n--------------------------------------\n"); + + Customer c1 = new Customer("MAMAD", a1); + Customer c2 = new Customer("ALI", a2); + + System.out.println("First Customer's ID: " + c1.getCustomerNumber()); + System.out.println("Second Customer's ID: " + c2.getCustomerNumber()); + + Food f1 = new Food("BURGER", 130); + Food f2 = new Food("PIZZA", 200); + Food f3 = new Food("PASTA", 110); + + System.out.println("\n--------------------------------------\n"); + + for (int i = 0; i < Food.getMenu().size(); i++) { + System.out.println("Food Name: " + Food.getMenu().get(i).getName()); + } + + System.out.println("\n--------------------------------------\n"); + + Item it1 = new Item(f1, 2); + Item it2 = new Item(f2, 3, "MORE SAUCE"); + Item it3 = new Item(f3, 1); + + Invoice inv = new Invoice(c1); + + if (inv.addItem(it1) && inv.addItem(it2)) + System.out.println("Items added successfully"); + else + System.out.println("Items could not be added"); + + if (inv.removeItem(it3)) + System.out.println("Item removed successfully"); + else + System.out.println("Failed to remove item (item doesn't exist in invoice)"); + + inv.nextStage(); + + System.out.println("Current state: " + inv.getState()); + + System.out.println("\n--------------------------------------\n"); + + for (int i = 0; i < inv.getItems().size(); i++) { + Item it = inv.getItems().get(i); + System.out.println("Food Name: " + it.getFood().getName() + " | Food Description: " + it.getDescription()); + } + + System.out.println("Total price: " + inv.getTotalPrice()); + } +}