Skip to content

completed the assignment #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions first/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package first;

public class Address {
private double latitude;
private double longitude;
private 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 getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

public String getWrittenAddress() {
return written_address;
}

public double distanceFrom(Address otherAddress) {
double lat1 = (this.latitude);
double lon1 = (this.longitude);
double lat2 = (otherAddress.getLatitude());
double lon2 = (otherAddress.getLongitude());

double dLat = lat2 - lat1;
double dLon = lon2 - lon1;

double distance = Math.sqrt(Math.pow(dLat, 2) + Math.pow(dLon, 2));



return distance;
}
}
33 changes: 33 additions & 0 deletions first/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package first;
public class Customer {
private static int nextCustomerNumber = 1;
private 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 void setName(String name) {
this.name = name;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}
27 changes: 27 additions & 0 deletions first/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package first;

import java.util.Vector;

public class Food {
private static Vector<Food> menu = new Vector<>();
private String name;
public int price;

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<Food> getMenu() {
return menu;
}
}
48 changes: 48 additions & 0 deletions first/Invoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package first;
import java.util.ArrayList;
import java.util.List;
public class Invoice {
private final Customer customer;
private final List<Item> items;
private int state;
private static final float TAX_RATE = 9.4f;
public List<Item> getItems() {
return items;
}
public Invoice(Customer customer) {
this.customer = customer;
this.items = new ArrayList<>();
this.state = -1;
}
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)) {
items.remove(item);
return true;
}
return false;
}
public void nextStage() {
state++;
}
public int getTotalPrice() {
int totalPrice = 0;
for (Item item : items) {
totalPrice += item.getFood().getPrice() * item.getCount();
}
float tax = totalPrice * TAX_RATE / 100;
return (int) Math.ceil(totalPrice + tax);
}
public Customer getCustomer() {
return customer;
}
public int getState() {
return state;
}
}
25 changes: 25 additions & 0 deletions first/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package first;
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;
if(description != null) {
this.description = description;
} else {
this.description = "";
}
}
public Food getFood() {
return food;
}
public int getCount() {
return count;
}
public String getDescription() {
return description;
}
}
54 changes: 54 additions & 0 deletions first/Restaurant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package first;
public class Restaurant {
public static void main(String[] args) {

Address address1 = new Address(35.7219, 51.3347, "Tehran, Iran");
Address address2 = new Address(35.652832, 139.839478, "Tokyo, Japan");

Customer customer1 = new Customer("Mahdi", address1);
Customer customer2 = new Customer("Haruto", address1);

Food food1 = new Food("Tahchin", 15);
Food food2 = new Food("Sushi", 25);


Item item1 = new Item(food1, 2, "Extra barberry");
Item item2 = new Item(food2, 3, "soy sauce");



Invoice invoice1 = new Invoice(customer1);
invoice1.addItem(item1);
invoice1.addItem(item2);


System.out.println("\n--------------------------------------\n");
System.out.println("Distance From Address address1 to address2 is: " + address1.distanceFrom(address2));
System.out.println("\n--------------------------------------\n");
System.out.println("First Customer's ID: " + customer1.getCustomerNumber());
System.out.println("Second Customer's ID: " + customer2.getCustomerNumber());
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");
if (invoice1.addItem(item1) && invoice1.addItem(item2)) System.out.println("Items added successfully");
else System.out.println("Items could not be added");


invoice1.nextStage();

System.out.println("Current state: " + invoice1.getState());

System.out.println("\n--------------------------------------\n");

for (int i = 0; i < invoice1.getItems().size(); i++) {
Item items = invoice1.getItems().get(i);
System.out.println("Food Name: " + items.getFood().getName() + " | Food Description: " + items.getDescription());
}
System.out.println("Total price: " + invoice1.getTotalPrice());
}

}