From 1c4825c1bf2e53d99406ae43b3b2f611163e5529 Mon Sep 17 00:00:00 2001 From: mohadesemoradzade Date: Sat, 4 May 2024 16:56:10 +0330 Subject: [PATCH 1/4] created 5 files --- first/Address.java | 0 first/Customer.java | 5 +++++ first/Food.java | 5 +++++ first/Invoice.java | 5 +++++ first/Item.java | 5 +++++ 5 files changed, 20 insertions(+) create mode 100644 first/Address.java create mode 100644 first/Customer.java create mode 100644 first/Food.java create mode 100644 first/Invoice.java create mode 100644 first/Item.java diff --git a/first/Address.java b/first/Address.java new file mode 100644 index 0000000..e69de29 diff --git a/first/Customer.java b/first/Customer.java new file mode 100644 index 0000000..2b03536 --- /dev/null +++ b/first/Customer.java @@ -0,0 +1,5 @@ +package first; + +public class Customer { + +} diff --git a/first/Food.java b/first/Food.java new file mode 100644 index 0000000..300ff5f --- /dev/null +++ b/first/Food.java @@ -0,0 +1,5 @@ +package first; + +public class Food { + +} diff --git a/first/Invoice.java b/first/Invoice.java new file mode 100644 index 0000000..9375eeb --- /dev/null +++ b/first/Invoice.java @@ -0,0 +1,5 @@ +package first; + +public class Invoice { + +} diff --git a/first/Item.java b/first/Item.java new file mode 100644 index 0000000..0b1f7be --- /dev/null +++ b/first/Item.java @@ -0,0 +1,5 @@ +package first; + +public class Item { + +} From 904d244fce037782d6c24d87f6179654e2e8f783 Mon Sep 17 00:00:00 2001 From: mohadesemoradzade Date: Sat, 4 May 2024 17:21:20 +0330 Subject: [PATCH 2/4] create & change address, customer, food filse --- first/Address.java | 41 +++++++++++++++++++++++++++++++++++++++++ first/Customer.java | 34 +++++++++++++++++++++++++++++++--- first/Food.java | 24 +++++++++++++++++++++++- first/Invoice.java | 5 ----- 4 files changed, 95 insertions(+), 9 deletions(-) delete mode 100644 first/Invoice.java diff --git a/first/Address.java b/first/Address.java index e69de29..c5f3712 100644 --- a/first/Address.java +++ b/first/Address.java @@ -0,0 +1,41 @@ +package first; + +public class Address { + private double latitude; + private double longitude; + private String written_address; + + public Address(double latitude, double longitude, String written_address) { + this.latitude = latitude; + this.longitude = longitude; + this.written_address = written_address; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public String getWrittenAddress() { + return written_address; + } + + public double distanceFrom(Address otherAddress) { + double lat1 = (this.latitude); + double lon1 = (this.longitude); + double lat2 = (otherAddress.getLatitude()); + double lon2 = (otherAddress.getLongitude()); + + double dLat = lat2 - lat1; + double dLon = lon2 - lon1; + + double distance = Math.sqrt(Math.pow(dLat, 2) + Math.pow(dLon, 2)); + + + + return distance; + } +} \ No newline at end of file diff --git a/first/Customer.java b/first/Customer.java index 2b03536..466fe69 100644 --- a/first/Customer.java +++ b/first/Customer.java @@ -1,5 +1,33 @@ package first; - -public class Customer { + public class Customer { + private static int nextCustomerNumber = 1; + private int customerNumber; + private String name; + private Address address; -} + public Customer(String name, Address address) { + this.customerNumber = nextCustomerNumber++; + 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; + } + } \ No newline at end of file diff --git a/first/Food.java b/first/Food.java index 300ff5f..617336a 100644 --- a/first/Food.java +++ b/first/Food.java @@ -1,5 +1,27 @@ package first; +import java.util.Vector; + public class Food { - + private static Vector menu = new Vector<>(); + private String name; + public 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 Vector getMenu() { + return menu; + } } diff --git a/first/Invoice.java b/first/Invoice.java deleted file mode 100644 index 9375eeb..0000000 --- a/first/Invoice.java +++ /dev/null @@ -1,5 +0,0 @@ -package first; - -public class Invoice { - -} From 069942655e3edb85ab910a44a8c7c44aa82d7fa7 Mon Sep 17 00:00:00 2001 From: mohadesemoradzade Date: Sat, 4 May 2024 17:25:04 +0330 Subject: [PATCH 3/4] create & change invoice, item filse --- first/Invoice.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++ first/Item.java | 24 +++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 first/Invoice.java diff --git a/first/Invoice.java b/first/Invoice.java new file mode 100644 index 0000000..2584b55 --- /dev/null +++ b/first/Invoice.java @@ -0,0 +1,48 @@ +package first; +import java.util.ArrayList; +import java.util.List; +public class Invoice { + private final Customer customer; + private final List items; + private int state; + private static final float TAX_RATE = 9.4f; + public List getItems() { + return items; + } + public Invoice(Customer customer) { + this.customer = customer; + this.items = new ArrayList<>(); + this.state = -1; + } + public boolean addItem(Item item) { + if (state == -1) { + items.add(item); + return true; + } + return false; + } + public boolean removeItem(Item item) { + if (state == -1 && items.contains(item)) { + items.remove(item); + return true; + } + return false; + } + public void nextStage() { + state++; + } + public int getTotalPrice() { + int totalPrice = 0; + for (Item item : items) { + totalPrice += item.getFood().getPrice() * item.getCount(); + } + float tax = totalPrice * TAX_RATE / 100; + return (int) Math.ceil(totalPrice + tax); + } + public Customer getCustomer() { + return customer; + } + public int getState() { + return state; + } +} diff --git a/first/Item.java b/first/Item.java index 0b1f7be..7d0b973 100644 --- a/first/Item.java +++ b/first/Item.java @@ -1,5 +1,25 @@ package first; - public class Item { - + private final Food food; + private final int count; + private final String description; + + public Item(Food food, int count, String description) { + this.food = food; + this.count = count; + if(description != null) { + this.description = description; + } else { + this.description = ""; + } + } + public Food getFood() { + return food; + } + public int getCount() { + return count; + } + public String getDescription() { + return description; + } } From caa06628e694d7c4586955e2c67192d5c78488ba Mon Sep 17 00:00:00 2001 From: mohadesemoradzade Date: Sat, 4 May 2024 17:29:33 +0330 Subject: [PATCH 4/4] create & change main filse --- first/Restaurant.java | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 first/Restaurant.java diff --git a/first/Restaurant.java b/first/Restaurant.java new file mode 100644 index 0000000..50d6a7c --- /dev/null +++ b/first/Restaurant.java @@ -0,0 +1,54 @@ +package first; +public class Restaurant { + public static void main(String[] args) { + + Address address1 = new Address(35.7219, 51.3347, "Tehran, Iran"); + Address address2 = new Address(35.652832, 139.839478, "Tokyo, Japan"); + + Customer customer1 = new Customer("Mahdi", address1); + Customer customer2 = new Customer("Haruto", address1); + + Food food1 = new Food("Tahchin", 15); + Food food2 = new Food("Sushi", 25); + + + Item item1 = new Item(food1, 2, "Extra barberry"); + Item item2 = new Item(food2, 3, "soy sauce"); + + + + Invoice invoice1 = new Invoice(customer1); + invoice1.addItem(item1); + invoice1.addItem(item2); + + + System.out.println("\n--------------------------------------\n"); + System.out.println("Distance From Address address1 to address2 is: " + address1.distanceFrom(address2)); + System.out.println("\n--------------------------------------\n"); + System.out.println("First Customer's ID: " + customer1.getCustomerNumber()); + System.out.println("Second Customer's ID: " + customer2.getCustomerNumber()); + 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"); + if (invoice1.addItem(item1) && invoice1.addItem(item2)) System.out.println("Items added successfully"); + else System.out.println("Items could not be added"); + + + invoice1.nextStage(); + + System.out.println("Current state: " + invoice1.getState()); + + System.out.println("\n--------------------------------------\n"); + + for (int i = 0; i < invoice1.getItems().size(); i++) { + Item items = invoice1.getItems().get(i); + System.out.println("Food Name: " + items.getFood().getName() + " | Food Description: " + items.getDescription()); + } + System.out.println("Total price: " + invoice1.getTotalPrice()); + } + +} \ No newline at end of file