From 76776f814ec28ca3ed4094764348a5bc7d559d31 Mon Sep 17 00:00:00 2001 From: Amirhossein Mirkazemi Date: Sun, 5 May 2024 09:28:49 +0330 Subject: [PATCH 1/5] address class --- first/Address.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 first/Address.java diff --git a/first/Address.java b/first/Address.java new file mode 100644 index 0000000..24d19d7 --- /dev/null +++ b/first/Address.java @@ -0,0 +1,31 @@ +public class Address { + public double longitude; + public double latitude; + public 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 target) { + return Math.sqrt( + Math.pow((this.latitude - target.getLatitude()), 2) + + + Math.pow((this.longitude - target.getLongitude()), 2) + ); + } +} From 2fc12d935132faac3d01db6defa2944c80d4184f Mon Sep 17 00:00:00 2001 From: Amirhossein Mirkazemi Date: Sun, 5 May 2024 09:28:58 +0330 Subject: [PATCH 2/5] customer class --- first/Address.java => Address.java | 0 Customer.java | 31 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) rename first/Address.java => Address.java (100%) create mode 100644 Customer.java diff --git a/first/Address.java b/Address.java similarity index 100% rename from first/Address.java rename to Address.java diff --git a/Customer.java b/Customer.java new file mode 100644 index 0000000..2178459 --- /dev/null +++ b/Customer.java @@ -0,0 +1,31 @@ +public class Customer { + private int number; + private String name; + private Address address; + + public Customer(int number, String name, Address address) { + this.number = number; + this.name = name; + this.address = new Address(latitude, longitude, writtenAddress); + } + + public int getCustomerNumber() { + return number; + } + + 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 7b83b6a4681093d4bf35e98f98ccc79ccb20a092 Mon Sep 17 00:00:00 2001 From: Amirhossein Mirkazemi Date: Sun, 5 May 2024 09:36:59 +0330 Subject: [PATCH 3/5] food class --- Food.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Food.java diff --git a/Food.java b/Food.java new file mode 100644 index 0000000..f011741 --- /dev/null +++ b/Food.java @@ -0,0 +1,27 @@ +import java.util.Vector; + +public class Food { + private String name; + private int price; + + private static Vector menu = new Vector(); + + 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 Vector getMenu() { + return menu; + } +} From 17cba68ecfcdc0d8cfdb7034f242841535758c3f Mon Sep 17 00:00:00 2001 From: Amirhossein Mirkazemi Date: Sun, 5 May 2024 09:37:09 +0330 Subject: [PATCH 4/5] item class --- Item.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Item.java diff --git a/Item.java b/Item.java new file mode 100644 index 0000000..7ee375c --- /dev/null +++ b/Item.java @@ -0,0 +1,29 @@ +public class Item { + private Food food; + private int count; + private String description; + + public Item(Food food, int count, String description) { + this.food = food; + this.count = count; + this.description = description; + } + + public Item(Food food, int count) { + this.food = food; + this.count = count; + this.description = ""; + } + + public Food getFood() { + return food; + } + + public int getCount() { + return count; + } + + public String getDescription() { + return description; + } +} From 9aafa8aa3e2e36eaaef599a5208bf0ddbbc8aeef Mon Sep 17 00:00:00 2001 From: Amirhossein Mirkazemi Date: Sun, 5 May 2024 09:48:32 +0330 Subject: [PATCH 5/5] move to restuarant dir --- Address.java => restaurant/Address.java | 6 +-- Customer.java => restaurant/Customer.java | 2 +- Food.java => restaurant/Food.java | 0 restaurant/Invoice.java | 52 +++++++++++++++++++++++ Item.java => restaurant/Item.java | 0 restaurant/Main.java | 51 ++++++++++++++++++++++ 6 files changed, 107 insertions(+), 4 deletions(-) rename Address.java => restaurant/Address.java (87%) rename Customer.java => restaurant/Customer.java (88%) rename Food.java => restaurant/Food.java (100%) create mode 100644 restaurant/Invoice.java rename Item.java => restaurant/Item.java (100%) create mode 100644 restaurant/Main.java diff --git a/Address.java b/restaurant/Address.java similarity index 87% rename from Address.java rename to restaurant/Address.java index 24d19d7..fc760e8 100644 --- a/Address.java +++ b/restaurant/Address.java @@ -1,7 +1,7 @@ public class Address { - public double longitude; - public double latitude; - public String writtenAddress; + private double longitude; + private double latitude; + private String writtenAddress; public Address(double latitude, double longitude, String writtenAddress){ this.latitude = latitude; diff --git a/Customer.java b/restaurant/Customer.java similarity index 88% rename from Customer.java rename to restaurant/Customer.java index 2178459..1bba420 100644 --- a/Customer.java +++ b/restaurant/Customer.java @@ -6,7 +6,7 @@ public class Customer { public Customer(int number, String name, Address address) { this.number = number; this.name = name; - this.address = new Address(latitude, longitude, writtenAddress); + this.address = address; } public int getCustomerNumber() { diff --git a/Food.java b/restaurant/Food.java similarity index 100% rename from Food.java rename to restaurant/Food.java diff --git a/restaurant/Invoice.java b/restaurant/Invoice.java new file mode 100644 index 0000000..18f4fbd --- /dev/null +++ b/restaurant/Invoice.java @@ -0,0 +1,52 @@ +import java.util.Vector; + +class Invoice { + private static final float taxRate = 9.4; + private Customer customer; + private boolean acceptingOrder; + private Vector items; + + public Invoice(Customer customer) { + this.customer = customer; + this.acceptingOrder = true; + this.items = new Vector(); + } + + public boolean addItem(Item item) { + if (acceptingOrder) { + items.add(item); + } + return acceptingOrder; + } + + public boolean removeItem(Item item) { + if (acceptingOrder) { + return items.remove(item); + } + return false; + } + + public void nextStage() { + this.acceptingOrder = false + } + + public int getState() { + return state; + } + + public int getTotalPrice() { + double total = 0; + for (Item item : items) { + total += item.getFood().getPrice() * item.getCount(); + } + return (int) (total * (taxRate + 100) / 100); + } + + public Customer getCustomer() { + return customer; + } + + public Vector getItems() { + return items; + } +} diff --git a/Item.java b/restaurant/Item.java similarity index 100% rename from Item.java rename to restaurant/Item.java diff --git a/restaurant/Main.java b/restaurant/Main.java new file mode 100644 index 0000000..7220421 --- /dev/null +++ b/restaurant/Main.java @@ -0,0 +1,51 @@ +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()); +}