diff --git a/first/restaurant/Address.java b/first/restaurant/Address.java new file mode 100644 index 0000000..e6cebd9 --- /dev/null +++ b/first/restaurant/Address.java @@ -0,0 +1,16 @@ +package restaurant; +public class Address { + public double latitude , longitude; + public String written_address; + public double distance_from(Address other) { + double xDistance = other.latitude - this.latitude; + double yDistance = other.longitude - this.longitude; + double distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance); + return distance; + } + public Address(double latitude, double longitude, String written_address) { + this.latitude = latitude; + this.longitude = longitude; + this.written_address = written_address; + } +} \ No newline at end of file diff --git a/first/restaurant/Customer.java b/first/restaurant/Customer.java new file mode 100644 index 0000000..b0b1799 --- /dev/null +++ b/first/restaurant/Customer.java @@ -0,0 +1,27 @@ +package restaurant; +public class Customer { + private static int nId = 1; + private Address address; + private String name; + private final int cuNum; + public Customer(String name, Address address) { + this.cuNum = nId++; + this.name = name; + this.address = address; + } + public int getCustomerNumber() { + return this.cuNum; + } + public String getName() { + return this.name; + } + public void setName(String name) { + this.name = name; + } + public Address getAddress() { + return this.address; + } + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/first/restaurant/Food.java b/first/restaurant/Food.java new file mode 100644 index 0000000..0a1633d --- /dev/null +++ b/first/restaurant/Food.java @@ -0,0 +1,21 @@ +package restaurant; +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 String getName() { + return this.name; + } + public int getPrice() { + return this.price; + } + public static Vector getMenu() { + return menu; + } +} diff --git a/first/restaurant/Invoice.java b/first/restaurant/Invoice.java new file mode 100644 index 0000000..8c54087 --- /dev/null +++ b/first/restaurant/Invoice.java @@ -0,0 +1,48 @@ +package restaurant; +import java.util.ArrayList; +public class Invoice { + private static final float tax_rate = 9.4f; + private int state = -1; + private Customer customer; + private ArrayList items = new ArrayList<>(); + public Invoice(String customerName, Address customerAddress) { + this.customer = new Customer(customerName, customerAddress); + } + public Invoice(Customer c1) { + } + public int getState() { + return this.state; + } + public Customer getCustomer() { + return this.customer; + } + public boolean addItem(Item item) { + if (this.state == -1) { + this.items.add(item); + return true; + } + return false; + } + public boolean removeItem(Item item) { + if (this.state == -1 && this.items.contains(item)) { + this.items.remove(item); + return true; + } + return false; + } + public void nextStage() { + this.state++; + } + public int getTotalPrice() { + double total = 0; + for (Item item : this.items) { + total += item.getFood().getPrice() * item.getCount(); + } + total += total * tax_rate / 100; + return (int) Math.ceil(total); + } + public ArrayList getItems() { + return this.items; + } +} + diff --git a/first/restaurant/Item.java b/first/restaurant/Item.java new file mode 100644 index 0000000..420feba --- /dev/null +++ b/first/restaurant/Item.java @@ -0,0 +1,24 @@ +package restaurant; +public class Item { + private final Food food; + private final int count; + private String description = ""; + public Item(Food food, int count) { + this.food = food; + this.count = count; + } + public Item(Food food, int count , String desc) { + this.food = food; + this.count = count; + this.description = desc; + } + public Food getFood() { + return this.food; + } + public int getCount() { + return this.count; + } + public String getDescription() { + return this.description; + } +} diff --git a/first/restaurant/Restaurant.java b/first/restaurant/Restaurant.java new file mode 100644 index 0000000..3c0ec80 --- /dev/null +++ b/first/restaurant/Restaurant.java @@ -0,0 +1,56 @@ +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.distance_from(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()); + } + + +}