Skip to content

Asef #15

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 11 commits into
base: main
Choose a base branch
from
Open

Asef #15

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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/hw5.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@

### در نهایت با ارسال پول ریکوئست به صفحه تمرین کد شما تصحیح خواهد شد.

### ددلاین تمرین تا روز یکشنبه 9 اردیبهشت 1403 ساعت 11.5 صبح می باشد.
### موفق باشید :)
29 changes: 29 additions & 0 deletions Restaurant/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions Restaurant/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Restaurant/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Restaurant/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Restaurant/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Restaurant/Restaurant.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
44 changes: 44 additions & 0 deletions Restaurant/src/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class Address {

private double latitude;
private double longitude;
private String written_address;

public Address(double latitude, double longitude, String writtenAddress) {
this.latitude = latitude;
this.longitude = longitude;
this.written_address = writtenAddress;
}

public double getLatitude() {
return latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}

public String getWritten_address() {
return written_address;
}

public void setWrittenAddress(String written_address) {
this.written_address = written_address;
}

public double distanceFrom(Address Address2) {
double la_distance = this.latitude - Address2.latitude;
double lo_distance = this.longitude - Address2.longitude;
double distance = Math.sqrt(Math.pow(la_distance, 2) + Math.pow(lo_distance, 2));

return Math.round(distance * 100000.0) / 100000.0;
}
}
35 changes: 35 additions & 0 deletions Restaurant/src/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Customer {
private static int newId = 1;
private final int id;
private String name;
private Address address;

public Customer(String name, Address address) {
this.id = newId;
newId++;
this.name = name;
this.address = address;
}

public int getCustomerNumber() {
return id;
}

// no set for customer number as it is final

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;
}
}
26 changes: 26 additions & 0 deletions Restaurant/src/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Vector;

public class Food {

private static Vector<Food> 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<Food> getMenu() {
return menu;
}

public String getName() {
return name;
}

public int getPrice() {
return price;
}
}
75 changes: 75 additions & 0 deletions Restaurant/src/Invoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.util.Vector;

public class Invoice {

private static final float tax_rate = 0.094f;
private int state;
private final Customer customer;
private final Vector<Item> items;
private float totalPrice;

public Invoice(Customer customer) {
this.state = -1;
this.customer = customer;
this.items = new Vector<>();
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);
calculateTotalPrice();
return true;
}
else {
return false;
}
}

public boolean removeItem(Item item) {
int i = 0;
if (state == -1) {
for (Item currItem : items) {
if (currItem.getFood().equals(item.getFood())) {
items.remove(i);
calculateTotalPrice();
return true;
}
i++;
}
}
return false;
}

public void nextStage() {
state++;

}

public Vector<Item> getItems() {
return items;
}

public int getTotalPrice() {
return (int) Math.ceil(totalPrice);
}

private void calculateTotalPrice() {
totalPrice = 0.0f;
for (Item item : items) {
// add up all prices
totalPrice += item.getCount() * item.getFood().getPrice();
}
// totalprice + calculated tax
totalPrice += totalPrice * tax_rate;

}
}
31 changes: 31 additions & 0 deletions Restaurant/src/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;
this.description = description;
}

public Item(Food food, int count) {
this.food = food;
this.count = count;
// empty
this.description = "-";
}

public Food getFood() {
return food;
}

public int getCount() {
return count;
}

public String getDescription() {
return description;
}
}
51 changes: 51 additions & 0 deletions Restaurant/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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());
}
Loading