我打算只用Vue2做前端,后端用Java,因为是数据结构大作业,老师不允许使用数据库存储数据,要做的外卖程序中的菜品列表、订单列表、顾客列表等都必须采用所学过的一种数据结构(链表、栈、队列、树等)存储,你能帮我一个完整的代码让我参考吗
时间: 2023-07-15 18:11:24 浏览: 83
当然可以,以下是一个简单的外卖程序的Java代码示例,使用了链表来存储菜品列表、订单列表和顾客列表:
```java
import java.util.*;
// 菜品类
class Dish {
private String name;
private double price;
public Dish(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
// 订单类
class Order {
private int id;
private LinkedList<Dish> dishes;
private String customer;
public Order(int id, LinkedList<Dish> dishes, String customer) {
this.id = id;
this.dishes = dishes;
this.customer = customer;
}
public int getId() {
return id;
}
public LinkedList<Dish> getDishes() {
return dishes;
}
public String getCustomer() {
return customer;
}
}
// 顾客类
class Customer {
private String name;
private LinkedList<Order> orders;
public Customer(String name, LinkedList<Order> orders) {
this.name = name;
this.orders = orders;
}
public String getName() {
return name;
}
public LinkedList<Order> getOrders() {
return orders;
}
}
public class Main {
public static void main(String[] args) {
LinkedList<Dish> dishList = new LinkedList<>();
LinkedList<Order> orderList = new LinkedList<>();
LinkedList<Customer> customerList = new LinkedList<>();
// 添加菜品
dishList.add(new Dish("鱼香肉丝", 18.0));
dishList.add(new Dish("宫保鸡丁", 20.0));
dishList.add(new Dish("回锅肉", 22.0));
// 添加订单
LinkedList<Dish> dishes1 = new LinkedList<>();
dishes1.add(new Dish("鱼香肉丝", 18.0));
dishes1.add(new Dish("宫保鸡丁", 20.0));
Order order1 = new Order(1, dishes1, "张三");
orderList.add(order1);
LinkedList<Dish> dishes2 = new LinkedList<>();
dishes2.add(new Dish("回锅肉", 22.0));
Order order2 = new Order(2, dishes2, "李四");
orderList.add(order2);
// 添加顾客
LinkedList<Order> orders1 = new LinkedList<>();
orders1.add(order1);
Customer customer1 = new Customer("张三", orders1);
customerList.add(customer1);
LinkedList<Order> orders2 = new LinkedList<>();
orders2.add(order2);
Customer customer2 = new Customer("李四", orders2);
customerList.add(customer2);
// 输出菜品列表
System.out.println("菜品列表:");
for (Dish dish : dishList) {
System.out.println(dish.getName() + " " + dish.getPrice() + "元");
}
// 输出订单列表
System.out.println("订单列表:");
for (Order order : orderList) {
System.out.println("订单编号:" + order.getId() + " 顾客:" + order.getCustomer() + " 菜品列表:");
for (Dish dish : order.getDishes()) {
System.out.println(dish.getName() + " " + dish.getPrice() + "元");
}
}
// 输出顾客列表
System.out.println("顾客列表:");
for (Customer customer : customerList) {
System.out.println("顾客姓名:" + customer.getName() + " 订单列表:");
for (Order order : customer.getOrders()) {
System.out.println("订单编号:" + order.getId() + " 菜品列表:");
for (Dish dish : order.getDishes()) {
System.out.println(dish.getName() + " " + dish.getPrice() + "元");
}
}
}
}
}
```
在该程序中,菜品列表、订单列表和顾客列表都使用了Java的LinkedList类来存储。菜品类、订单类和顾客类则分别表示了菜品、订单和顾客的信息,包括名称、价格、编号、顾客姓名、菜品列表等。在程序中,我们通过添加菜品、订单和顾客的方式来演示了如何使用链表来存储数据,并且通过输出的方式来展示了这些数据的内容。
阅读全文