diff --git a/first/Resturant/Address.java b/first/Resturant/Address.java new file mode 100644 index 0000000..d04ff89 --- /dev/null +++ b/first/Resturant/Address.java @@ -0,0 +1,60 @@ +public class Address { + + private double latitude; // عرض جغرافیایی + private double longitude; // طول جغرافیایی + private 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 void setLatitude(double latitude) { + this.latitude = latitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public void setWrittenAddress(String writtenAddress) { + this.writtenAddress = writtenAddress; + } + + public double distanceFrom(Address otherAddress) { + double x1 = this.getLatitude(); + double y1 = this.getLongitude(); + + double x2 = otherAddress.getLatitude(); + double y2 = otherAddress.getLongitude(); + + double distanceX = Math.abs(x2 - x1); + double distanceY = Math.abs(y2 - y1); + + double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY); + + return Math.round(distance * 100000) / 100000.0; + } + + @Override + public String toString() { + return "Address{" + + "latitude=" + latitude + + ", longitude=" + longitude + + ", writtenAddress='" + writtenAddress + "'" + + '}'; + } +} diff --git a/first/Resturant/Customer.java b/first/Resturant/Customer.java new file mode 100644 index 0000000..58a8e30 --- /dev/null +++ b/first/Resturant/Customer.java @@ -0,0 +1,42 @@ + +public class Customer { + private static int nextId = 1; + private final int id; + private String name; // نام مشتری + private Address address; // آدرس مشتری + + public Customer(String name, Address address) { + this.id = nextId++; + this.name = name; + this.address = address; + } + + public int getCustomerNumber() { + return id; + } + + 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; + } + + @Override + public String toString() { + return "Customer{" + + "customerId=" + id + + ", name='" + name + + "', address=" + address + + '}'; + } +} diff --git a/first/Resturant/Food.java b/first/Resturant/Food.java new file mode 100644 index 0000000..07cafd7 --- /dev/null +++ b/first/Resturant/Food.java @@ -0,0 +1,34 @@ +import java.util.Vector; + +public class Food { + + private final 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; + } + + @Override + public String toString() { + return "Food{" + + "name='" + name + '\'' + + ", price=" + price + + '}'; + } +} diff --git a/first/Resturant/Invoice.java b/first/Resturant/Invoice.java new file mode 100644 index 0000000..7716d1e --- /dev/null +++ b/first/Resturant/Invoice.java @@ -0,0 +1,83 @@ +import java.util.ArrayList; +import java.util.List; + +public class Invoice { + + private static final float TAX_RATE = 0.094f; // نرخ مالیات (9.4%) + private int state; + private final Customer customer; // مشتری + private final List items; // لیست آیتم های فاکتور + private float totalPrice; // قیمت کل + + public Invoice(Customer customer) { + this.state = -1; + this.customer = customer; + this.items = new ArrayList<>(); + 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); + recalculateTotalPrice(); + return true; + } + else { + return false; + } + } + + public boolean removeItem(Item item) { + if (state == -1) { // فقط در حال ثبت سفارش + for (Item existingItem : items) { + if (existingItem.getFood().equals(item.getFood())) { + items.remove(existingItem); + recalculateTotalPrice(); + return true; + } + } + } + return false; + } + + public void nextStage() { + if (state < 3) + state++; + + } + + public List getItems() { + return items; // لیست آیتم های فاکتور را برمی گرداند + } + + public int getTotalPrice() { + return (int)Math.ceil(totalPrice); + } + + private void recalculateTotalPrice() { + totalPrice = 0.0f; + for (Item item : items) { + totalPrice += item.getCount() * item.getFood().getPrice(); + } + totalPrice += totalPrice * TAX_RATE; + + } + + @Override + public String toString() { + return "Invoice{" + + "state=" + state + + ", customer=" + customer + + ", items=" + items + + ", totalPrice=" + totalPrice + + '}'; + } +} diff --git a/first/Resturant/Item.java b/first/Resturant/Item.java new file mode 100644 index 0000000..0f768d8 --- /dev/null +++ b/first/Resturant/Item.java @@ -0,0 +1,39 @@ +public class Item { + + private final Food food; + private final int count; + private final String description; + + public Item(Food food, int count) { + this.food = food; + this.count = count; + this.description = "_"; // Empty description + } + + public Item(Food food, int count, String description) { + this.food = food; + this.count = count; + this.description = description; + } + + public Food getFood() { + return food; + } + + public int getCount() { + return count; + } + + public String getDescription() { + return description; + } + + @Override + public String toString() { + return "Item{" + + "food=" + food + + ", count=" + count + + ", description='" + description + + "'}"; + } +} diff --git a/first/Resturant/Restaurant.java b/first/Resturant/Restaurant.java new file mode 100644 index 0000000..1de77c1 --- /dev/null +++ b/first/Resturant/Restaurant.java @@ -0,0 +1,53 @@ +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.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()); + } +}