java订餐管理系统源码
时间: 2024-03-30 21:31:58 浏览: 157
Java订餐管理系统是一个用Java语言编写的系统,用于管理餐厅的订餐业务。它可以实现用户注册、登录、浏览菜单、下单、支付等功能。以下是一个简单的Java订餐管理系统的源码示例:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class MenuItem {
private String name;
private double price;
public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class Order {
private List<MenuItem> items;
public Order() {
items = new ArrayList<>();
}
public void addItem(MenuItem item) {
items.add(item);
}
public double getTotalPrice() {
double totalPrice = 0;
for (MenuItem item : items) {
totalPrice += item.getPrice();
}
return totalPrice;
}
}
class Menu {
private List<MenuItem> items;
public Menu() {
items = new ArrayList<>();
}
public void addItem(MenuItem item) {
items.add(item);
}
public void displayMenu() {
System.out.println("Menu:");
for (int i = 0; i < items.size(); i++) {
MenuItem item = items.get(i);
System.out.println((i + 1) + ". " + item.getName() + " - $" + item.getPrice());
}
}
public MenuItem getItem(int index) {
return items.get(index);
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
class UserManager {
private List<User> users;
public UserManager() {
users = new ArrayList<>();
}
public void addUser(User user) {
users.add(user);
}
public User getUser(String username) {
for (User user : users) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
}
public class OrderManagementSystem {
private static Menu menu;
private static UserManager userManager;
public static void main(String[] args) {
menu = new Menu();
userManager = new UserManager();
// 添加菜单项
menu.addItem(new MenuItem("Hamburger", 5.99));
menu.addItem(new MenuItem("Pizza", 8.99));
menu.addItem(new MenuItem("Salad", 4.99));
// 添加用户
userManager.addUser(new User("admin", "admin123"));
// 用户登录
Scanner scanner = new Scanner(System.in);
System.out.print("Username: ");
String username = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
User user = userManager.getUser(username);
if (user != null && user.getPassword().equals(password)) {
System.out.println("Login successful!");
// 显示菜单
menu.displayMenu();
// 下单
Order order = new Order();
while (true) {
System.out.print("Enter item number (0 to finish): ");
int itemNumber = scanner.nextInt();
if (itemNumber == 0) {
break;
}
MenuItem item = menu.getItem(itemNumber - 1);
order.addItem(item);
}
// 显示订单总价
System.out.println("Total price: $" + order.getTotalPrice());
} else {
System.out.println("Invalid username or password!");
}
}
}
```
这个示例代码实现了一个简单的Java订餐管理系统,包括菜单管理、用户管理、登录验证、下单和计算订单总价等功能。你可以根据自己的需求进行修改和扩展。
阅读全文