diff --git a/Restaurant/Address.java b/Restaurant/Address.java new file mode 100644 index 0000000..f7185c6 --- /dev/null +++ b/Restaurant/Address.java @@ -0,0 +1,33 @@ +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 otherAddress){ + double la1 = this.latitude; + double lo1 = this.longitude; + double la2 = otherAddress.getLatitude(); + double lo2 = otherAddress.getLongitude(); + double distance; + double x; + x = Math.pow((la2 - la1), 2) + Math.pow((lo2 - lo1), 2); + distance = Math.pow(x, 0.5); + + return Math.round(distance * 100000.0) / 100000.0; + } +} diff --git a/Restaurant/Customer.java b/Restaurant/Customer.java new file mode 100644 index 0000000..a242d04 --- /dev/null +++ b/Restaurant/Customer.java @@ -0,0 +1,35 @@ +public class Customer { + private int customerNumber; + private String name; + private Address address; + public double latitude; + public double longitude; + public String writtenAddress; + + public Customer(String name , Address address) { + this.customerNumber = generateUniqueCustomerNumber(); + this.name = ""; + this.address = new Address(latitude, longitude, writtenAddress); + } + + private static int nextCustomerNumber = 1; + private int generateUniqueCustomerNumber() { + return nextCustomerNumber++; + } + + 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/Restaurant/Food.java b/Restaurant/Food.java new file mode 100644 index 0000000..a459726 --- /dev/null +++ b/Restaurant/Food.java @@ -0,0 +1,21 @@ +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; + } +} diff --git a/Restaurant/Invoice.java b/Restaurant/Invoice.java new file mode 100644 index 0000000..ec21cf5 --- /dev/null +++ b/Restaurant/Invoice.java @@ -0,0 +1,50 @@ +import java.util.Vector; +class Invoice { + private static final float taxRate = 0.094f; + private Customer customer; + private int state; + private Vector items; + private int price; + private Item item; + + public Invoice(Customer customer) { + this.customer = customer; + this.state = -1; + this.items = new Vector<>(); + } + public int getState(){ + return state; + } + public Customer getCustomer(){ + return customer; + } + public Vector getItems(){ + return items; + } + 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)) { + return items.remove(item); + } + return false; + } + public void nextStage() { + if (state < 2) { + state++; + } + } + public int getTotalPrice() { + double total = 0.0; + for (Item item : items) { + total += (item.getFood().getPrice()) * item.getCount(); + } + double totalTax = total * taxRate; + return (int) Math.ceil(total + totalTax); + } +} \ No newline at end of file diff --git a/Restaurant/Item.java b/Restaurant/Item.java new file mode 100644 index 0000000..e09f930 --- /dev/null +++ b/Restaurant/Item.java @@ -0,0 +1,27 @@ +public class Item { + private Food food; + private int count; + private String description; + private int price; + + 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; + } +} \ No newline at end of file diff --git a/Restaurant/Main.java b/Restaurant/Main.java new file mode 100644 index 0000000..9f081cf --- /dev/null +++ b/Restaurant/Main.java @@ -0,0 +1,53 @@ +public class Main { + 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