Commit 3f99d65a authored by 9531304's avatar 9531304

complete

parents
Pipeline #661 failed with stages
import com.restaurant.*;
public class TestFile {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant("Java AP Lab",3000);
Ingredient i1 = new Ingredient(1, "carrot", 20);
Ingredient i2 = new Ingredient(2, "meat", 50);
Ingredient i3 = new Ingredient(3, "bread", 10);
Ingredient i4 = new Ingredient(4, "potato", 20);
Ingredient i5 = new Ingredient(4, "onion", 30);
Food f1 = new Food(1, "ab-goosht");
f1.addIngredient(i3, 2);
f1.addIngredient(i1, 1);
f1.addIngredient(i2, 1);
f1.addIngredient(i4, 1);
restaurant.addFood(f1);
Food f2 = new Food(2, "kabab");
f2.addIngredient(i2, 4);
f2.addIngredient(i5, 5);
restaurant.addFood(f2);
Customer customer = new Customer(1, "gholi");
restaurant.addCustomer(customer);
Food[] foods = new Food[]{f1, f1};
Order order1 = new Order(1, customer, foods);
order1.addFood(f1, 2);
restaurant.prepareOrder(order1);
restaurant.acceptPayment(customer);
System.out.println(restaurant.getFund());
System.out.println(restaurant.getNumberOfFoodsDelivered());
}
}
package com.restaurant;
public class Customer {
private int id;
private String name;
private Order order;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
package com.restaurant;
import java.util.ArrayList;
public class Food {
private int id;
private String name;
private int price;
private ArrayList<Ingredient> ingredients;
private ArrayList<Integer> ingredientsAmount;
public Food (int id, String name) {
this.id = id;
this.name = name;
price = 0;
ingredients = new ArrayList<Ingredient>();
ingredientsAmount = new ArrayList<Integer>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getPrice(){
return price;
}
public void addIngredient(Ingredient ingredient, int amount) {
ingredients.add(ingredient);
ingredientsAmount.add(amount);
price += amount * ingredient.getPrice();
}
@Override
public String toString() {
return this.name;
}
}
package com.restaurant;
public class Ingredient {
private int id;
private String name;
private int price;
public Ingredient (int id, String name, int price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
package com.restaurant;
public class Order {
private int id;
private Customer customer;
private Food[] foods;
private int price;
public Order(int id, Customer customer, Food[] foods) {
this.id = id;
this.customer = customer;
this.foods = foods;
for (Food food : foods) {
price += food.getPrice();
}
price *= 1.4;
customer.setOrder(this);
}
public Food[] getFoods() {
return foods;
}
public int getPrice() {
return price;
}
public void addFood(Food food, int amount){
Food[] temp = new Food[foods.length + amount];
for (int i = 0; i < foods.length; i++) {
temp[i] = foods[i];
}
for (int i = 0; i < amount; i++) {
temp[foods.length + i] = food;
}
foods = temp;
temp = null;
price = 0;
for(int j = 0 ; j < foods.length ; j++){
price += 1.4*foods[j].getPrice();
}
}
public Customer getCustomer() {
return customer;
}
}
package com.restaurant;
import java.util.ArrayList;
import java.util.HashMap;
public class Restaurant {
private final ArrayList<Customer> currentCustomers;
private String name;
private int fund;
private ArrayList<Food> foods;
private ArrayList<Customer> customers;
public Restaurant(String name, int fund) {
this.name = name;
this.fund = fund;
customers = new ArrayList<>();
currentCustomers = new ArrayList<>();
this.numOrdered = new HashMap<>();
this.foods = new ArrayList<>();
}
public HashMap<Food, Integer> numOrdered = new HashMap<>();
public Restaurant(int fund) {
this("JAVA Coffee house", fund);
}
public String getName() {
return name;
}
public int getFund() {
return fund;
}
public ArrayList<Food> getFoods() {
ArrayList<Food> foodArrayList = new ArrayList<>();
int size = foods.size();
for (int i = 0; i < size; i++) {
int max = 0;
Food chosenFood = foods.get(0);
for (Food food : foods){
if (numOrdered.get(food) > max){
chosenFood = food;
max = numOrdered.get(food);
}
}
foodArrayList.add(chosenFood);
foods.remove(chosenFood);
}
foods = foodArrayList;
return foodArrayList;
}
public void addFood(Food food){
numOrdered.put(food, 0);
foods.add(food);
}
public ArrayList<Customer> getCustomers() {
return customers;
}
public ArrayList<Customer> getCurrentCustomers() {
return currentCustomers;
}
public void addCustomer(Customer customer) {
customers.add(customer);
}
public void prepareOrder(Order m) {
int cost = 0;
for (Food food : m.getFoods()) {
int num = numOrdered.get(food);
numOrdered.put(food, num + 1);
cost += food.getPrice();
}
fund -= cost;
currentCustomers.add(m.getCustomer());
}
public void acceptPayment(Customer customer) {
int menuCost = customer.getOrder().getPrice();
currentCustomers.remove(customer);
fund += menuCost * 0.9;
totalNumOfFood += customer.getOrder().getFoods().length;
}
private int totalNumOfFood = 0;
public int getNumberOfFoodsDelivered(){
return totalNumOfFood;
}
@Override
public String toString() {
String toReturn = "";
toReturn += this.name + "\n";
toReturn += this.fund + "\n";
int totNumSold = 0;
for (int num: numOrdered.values()) {
totNumSold += num;
}
toReturn += currentCustomers.size() + "\n";
toReturn += customers.size() + "\n";
for (Food food: getFoods())
toReturn += food + "\n";
toReturn += totNumSold + "\n";
return toReturn.trim();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment