diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/hw5.iml b/.idea/hw5.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/hw5.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8f8a12d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 44540b9..0a012f7 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,4 @@ ### در نهایت با ارسال پول ریکوئست به صفحه تمرین کد شما تصحیح خواهد شد. -### ددلاین تمرین تا روز یکشنبه 9 اردیبهشت 1403 ساعت 11.5 صبح می باشد. ### موفق باشید :) diff --git a/Restaurant/.gitignore b/Restaurant/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Restaurant/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Restaurant/.idea/.gitignore b/Restaurant/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Restaurant/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Restaurant/.idea/misc.xml b/Restaurant/.idea/misc.xml new file mode 100644 index 0000000..779255b --- /dev/null +++ b/Restaurant/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Restaurant/.idea/modules.xml b/Restaurant/.idea/modules.xml new file mode 100644 index 0000000..1703cce --- /dev/null +++ b/Restaurant/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Restaurant/.idea/vcs.xml b/Restaurant/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Restaurant/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Restaurant/Restaurant.iml b/Restaurant/Restaurant.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Restaurant/Restaurant.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Restaurant/src/Address.java b/Restaurant/src/Address.java new file mode 100644 index 0000000..7396d41 --- /dev/null +++ b/Restaurant/src/Address.java @@ -0,0 +1,44 @@ +public class Address { + + private double latitude; + private double longitude; + private String written_address; + + public Address(double latitude, double longitude, String writtenAddress) { + this.latitude = latitude; + this.longitude = longitude; + this.written_address = writtenAddress; + } + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public String getWritten_address() { + return written_address; + } + + public void setWrittenAddress(String written_address) { + this.written_address = written_address; + } + + public double distanceFrom(Address Address2) { + double la_distance = this.latitude - Address2.latitude; + double lo_distance = this.longitude - Address2.longitude; + double distance = Math.sqrt(Math.pow(la_distance, 2) + Math.pow(lo_distance, 2)); + + return Math.round(distance * 100000.0) / 100000.0; + } +} \ No newline at end of file diff --git a/Restaurant/src/Customer.java b/Restaurant/src/Customer.java new file mode 100644 index 0000000..89c53a2 --- /dev/null +++ b/Restaurant/src/Customer.java @@ -0,0 +1,35 @@ +public class Customer { + private static int newId = 1; + private final int id; + private String name; + private Address address; + + public Customer(String name, Address address) { + this.id = newId; + newId++; + this.name = name; + this.address = address; + } + + public int getCustomerNumber() { + return id; + } + + // no set for customer number as it is final + + 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/Restaurant/src/Food.java b/Restaurant/src/Food.java new file mode 100644 index 0000000..90cfe2a --- /dev/null +++ b/Restaurant/src/Food.java @@ -0,0 +1,26 @@ +import java.util.Vector; + +public class Food { + + private static Vector menu = new Vector<>(); + private final String name; + private final int price; + + public Food(String name, int price) { + this.name = name; + this.price = price; + menu.add(this); + } + + public static Vector getMenu() { + return menu; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +} \ No newline at end of file diff --git a/Restaurant/src/Invoice.java b/Restaurant/src/Invoice.java new file mode 100644 index 0000000..17302c5 --- /dev/null +++ b/Restaurant/src/Invoice.java @@ -0,0 +1,75 @@ +import java.util.Vector; + +public class Invoice { + + private static final float tax_rate = 0.094f; + private int state; + private final Customer customer; + private final Vector items; + private float totalPrice; + + public Invoice(Customer customer) { + this.state = -1; + this.customer = customer; + this.items = new Vector<>(); + this.totalPrice = 0.0f; + } + + public int getState() { + return state; + } + + public Customer getCustomer() { + return customer; + } + + public boolean addItem(Item item) { + if (state == -1) { + items.add(item); + calculateTotalPrice(); + return true; + } + else { + return false; + } + } + + public boolean removeItem(Item item) { + int i = 0; + if (state == -1) { + for (Item currItem : items) { + if (currItem.getFood().equals(item.getFood())) { + items.remove(i); + calculateTotalPrice(); + return true; + } + i++; + } + } + return false; + } + + public void nextStage() { + state++; + + } + + public Vector getItems() { + return items; + } + + public int getTotalPrice() { + return (int) Math.ceil(totalPrice); + } + + private void calculateTotalPrice() { + totalPrice = 0.0f; + for (Item item : items) { + // add up all prices + totalPrice += item.getCount() * item.getFood().getPrice(); + } + // totalprice + calculated tax + totalPrice += totalPrice * tax_rate; + + } +} \ No newline at end of file diff --git a/Restaurant/src/Item.java b/Restaurant/src/Item.java new file mode 100644 index 0000000..aa5db83 --- /dev/null +++ b/Restaurant/src/Item.java @@ -0,0 +1,31 @@ +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; + this.description = description; + } + + public Item(Food food, int count) { + this.food = food; + this.count = count; + // empty + this.description = "-"; + } + + public Food getFood() { + return food; + } + + public int getCount() { + return count; + } + + public String getDescription() { + return description; + } +} \ No newline at end of file diff --git a/Restaurant/src/Main.java b/Restaurant/src/Main.java new file mode 100644 index 0000000..b5607a7 --- /dev/null +++ b/Restaurant/src/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()); +} \ No newline at end of file diff --git a/first/restaurant/Address.java b/first/restaurant/Address.java new file mode 100644 index 0000000..7396d41 --- /dev/null +++ b/first/restaurant/Address.java @@ -0,0 +1,44 @@ +public class Address { + + private double latitude; + private double longitude; + private String written_address; + + public Address(double latitude, double longitude, String writtenAddress) { + this.latitude = latitude; + this.longitude = longitude; + this.written_address = writtenAddress; + } + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public String getWritten_address() { + return written_address; + } + + public void setWrittenAddress(String written_address) { + this.written_address = written_address; + } + + public double distanceFrom(Address Address2) { + double la_distance = this.latitude - Address2.latitude; + double lo_distance = this.longitude - Address2.longitude; + double distance = Math.sqrt(Math.pow(la_distance, 2) + Math.pow(lo_distance, 2)); + + return Math.round(distance * 100000.0) / 100000.0; + } +} \ No newline at end of file diff --git a/first/restaurant/Customer.java b/first/restaurant/Customer.java new file mode 100644 index 0000000..89c53a2 --- /dev/null +++ b/first/restaurant/Customer.java @@ -0,0 +1,35 @@ +public class Customer { + private static int newId = 1; + private final int id; + private String name; + private Address address; + + public Customer(String name, Address address) { + this.id = newId; + newId++; + this.name = name; + this.address = address; + } + + public int getCustomerNumber() { + return id; + } + + // no set for customer number as it is final + + 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/restaurant/Food.java b/first/restaurant/Food.java new file mode 100644 index 0000000..90cfe2a --- /dev/null +++ b/first/restaurant/Food.java @@ -0,0 +1,26 @@ +import java.util.Vector; + +public class Food { + + private static Vector menu = new Vector<>(); + private final String name; + private final int price; + + public Food(String name, int price) { + this.name = name; + this.price = price; + menu.add(this); + } + + public static Vector getMenu() { + return menu; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +} \ No newline at end of file diff --git a/first/restaurant/Invoice.java b/first/restaurant/Invoice.java new file mode 100644 index 0000000..17302c5 --- /dev/null +++ b/first/restaurant/Invoice.java @@ -0,0 +1,75 @@ +import java.util.Vector; + +public class Invoice { + + private static final float tax_rate = 0.094f; + private int state; + private final Customer customer; + private final Vector items; + private float totalPrice; + + public Invoice(Customer customer) { + this.state = -1; + this.customer = customer; + this.items = new Vector<>(); + this.totalPrice = 0.0f; + } + + public int getState() { + return state; + } + + public Customer getCustomer() { + return customer; + } + + public boolean addItem(Item item) { + if (state == -1) { + items.add(item); + calculateTotalPrice(); + return true; + } + else { + return false; + } + } + + public boolean removeItem(Item item) { + int i = 0; + if (state == -1) { + for (Item currItem : items) { + if (currItem.getFood().equals(item.getFood())) { + items.remove(i); + calculateTotalPrice(); + return true; + } + i++; + } + } + return false; + } + + public void nextStage() { + state++; + + } + + public Vector getItems() { + return items; + } + + public int getTotalPrice() { + return (int) Math.ceil(totalPrice); + } + + private void calculateTotalPrice() { + totalPrice = 0.0f; + for (Item item : items) { + // add up all prices + totalPrice += item.getCount() * item.getFood().getPrice(); + } + // totalprice + calculated tax + totalPrice += totalPrice * tax_rate; + + } +} \ No newline at end of file diff --git a/first/restaurant/Item.java b/first/restaurant/Item.java new file mode 100644 index 0000000..aa5db83 --- /dev/null +++ b/first/restaurant/Item.java @@ -0,0 +1,31 @@ +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; + this.description = description; + } + + public Item(Food food, int count) { + this.food = food; + this.count = count; + // empty + this.description = "-"; + } + + public Food getFood() { + return food; + } + + public int getCount() { + return count; + } + + public String getDescription() { + return description; + } +} \ No newline at end of file diff --git a/first/restaurant/Main.java b/first/restaurant/Main.java new file mode 100644 index 0000000..b5607a7 --- /dev/null +++ b/first/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()); +} \ No newline at end of file