色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java訂餐系統類和對象

李明濤1年前6瀏覽0評論

Java訂餐系統類和對象是管理餐廳訂單以及菜單的系統。

在Java中,類是一個藍圖,它用于描述對象的屬性和方法。而對象則是類的實例,并且可以擁有自己的屬性和方法。

在訂餐系統中,需要定義以下類:

public class MenuItem {
private String name;
private String description;
private double price;
public MenuItem(String name, String description, double price) {
this.name = name;
this.description = description;
this.price = price;
}
// Getters and setters for the private fields
}
public class Menu {
private ArrayListmenuItems;
public Menu() {
this.menuItems = new ArrayList();
}
public void addMenuItem(MenuItem menuItem) {
this.menuItems.add(menuItem);
}
public void removeMenuItem(MenuItem menuItem) {
this.menuItems.remove(menuItem);
}
// Getter for menuItems
}
public class Order {
private ArrayListitems;
private double totalPrice;
public Order() {
this.items = new ArrayList();
this.totalPrice = 0.0;
}
public void addItem(MenuItem menuItem) {
this.items.add(menuItem);
this.totalPrice += menuItem.getPrice();
}
public void removeItem(MenuItem menuItem) {
this.items.remove(menuItem);
this.totalPrice -= menuItem.getPrice();
}
public double getTotalPrice() {
return this.totalPrice;
}
// Getter for items
}
public class Restaurant {
private Menu menu;
public Restaurant() {
this.menu = new Menu();
}
public void addMenuItem(MenuItem menuItem) {
this.menu.addMenuItem(menuItem);
}
public void removeMenuItem(MenuItem menuItem) {
this.menu.removeMenuItem(menuItem);
}
public Order createOrder() {
return new Order();
}
}
public class Main {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant();
MenuItem menuItem1 = new MenuItem("Spaghetti Carbonara", "Pasta with bacon and egg", 12.99);
MenuItem menuItem2 = new MenuItem("Margherita Pizza", "Pizza with tomato sauce and mozzarella", 10.99);
restaurant.addMenuItem(menuItem1);
restaurant.addMenuItem(menuItem2);
Order order = restaurant.createOrder();
order.addItem(menuItem1);
order.addItem(menuItem2);
System.out.println("Total price of order: " + order.getTotalPrice());
}
}

以上類定義了菜單、菜單項、訂單和餐廳四個類,并且在主函數中創建餐廳、菜單項以及訂單,并且添加菜單項到訂單中,最終輸出訂單的總價格。通過這些類和對象的定義,可以實現一個完整的訂餐系統。