diff --git a/first/Address.java b/first/Address.java new file mode 100644 index 0000000..12808be --- /dev/null +++ b/first/Address.java @@ -0,0 +1,48 @@ +public class Address{ + public double latitude; + public double longitude; + public 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 distanceFrom(Address otherAddress) { + double la1 = this.latitude; + double lo1 = this.longitude; + double la2 = otherAddress.latitude; + double lo2 = otherAddress.longitude; + double longitude=lo2 - lo1; + double latitude = la2-la1; + double power1=Math.pow(longitude,2); + double power2=Math.pow(latitude , 2); + double Distance= Math.sqrt(power1+power2); + double distanceRound=Math.round(Distance * 100000)/100000.0; + return distanceRound; + } + public double getLatitude(){ + return latitude; + } + public void setLatitude(double newlatitude){ + this.latitude=newlatitude; + } + public double getLongitude(){ + return longitude; + } + public void setLongitude(double newlongitude){ + this.longitude=newlongitude; + } + public String getWrittenAddress() { + return written_address; + } + + public void setWrittenAddress(String newwritten_address) { + this.written_address = newwritten_address; + } + + +} + + \ No newline at end of file diff --git a/first/Customer.java b/first/Customer.java new file mode 100644 index 0000000..1178b20 --- /dev/null +++ b/first/Customer.java @@ -0,0 +1,27 @@ +public class Customer{ + private static int nextCustomerNumber = 1; + private final 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 Address getAddress(){ + return address; + } + public void setName(String name){ + this.name = name; + } + public void setAddress(Address address){ + this.address = address; + } +} \ No newline at end of file diff --git a/first/Food.java b/first/Food.java new file mode 100644 index 0000000..f79672b --- /dev/null +++ b/first/Food.java @@ -0,0 +1,23 @@ +import java.util.Vector; +public class Food{ + private final String name; + private final int price; + private static Vector menu= new Vector<>(); + + public Food(String n , int p){ + this.name=n; + this.price = p; + menu.add(this); + + } + public static Vector getMenu(){ + return menu; + } + public int getPrice(){ + return price; + } + public String getName(){ + return name; + } + +} \ No newline at end of file diff --git a/first/Invoice.java b/first/Invoice.java new file mode 100644 index 0000000..690fe04 --- /dev/null +++ b/first/Invoice.java @@ -0,0 +1,52 @@ +import java.util.ArrayList; +import java.util.List; +public class Invoice{ + private int state; + private Customer customer; + private List items; + private static final double tax_rate = 0.094; + + public Invoice(Customer customer){ + this.customer = customer; + this.state = -1; + this.items=new ArrayList<>(); + } + public int getState(){ + return this.state; + } + public Customer getCustomer(){ + return this.customer; + } + public boolean addItem(Item item){ + if (this.state == -1){ + items.add(item); + return true; + } + else{ + return false;} + } + public boolean removeItem(Item item){ + if (this.state== -1 && items.contains(item)){ + items.remove(item); + return true; + } + else{ + return false;} + } + public void nextStage(){ + this.state= state+1; + } + public int getTotalPrice(){ + double total =0 ; + for(Item item : items){ + total += item.getFood().getPrice() * item.getCount(); + } + total += total * tax_rate; + return (int) Math.ceil(total); + } + public List getItems(){ + return items; + } + + } + diff --git a/first/Item.java b/first/Item.java new file mode 100644 index 0000000..055848b --- /dev/null +++ b/first/Item.java @@ -0,0 +1,27 @@ +public class Item{ + private final Food food; + private final int count; + private final String description; + + public Item(Food food , int count){ + this.count=count; + this.food=food; + this.description="-"; + } + + public Item(Food food , int count , String d){ + this.count=count; + this.food=food; + this.description=d; + } + public int getCount(){ + return count; + } + public Food getFood(){ + return food; + } + public String getDescription(){ + return description; + } + +} \ No newline at end of file diff --git a/first/Restaurant.java b/first/Restaurant.java new file mode 100644 index 0000000..85f23b0 --- /dev/null +++ b/first/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()); +} +} \ No newline at end of file