生成java代码,要求如下:该饮品自动贩卖机总容量确定,且贩卖机内通道宽度并排只能容纳一个饮品, 购买饮品出口和添加饮品口在同一个位置。经营者添加饮品时,每次只能添加一 个饮品,饮品按照添加的先后顺序依次排列。购买者每次只能购买一个饮品,可 以选择现有饮品中任意一个购买。(由于贩卖机内通道宽度并排只能容纳一个饮 品,为了让购买者可以购买任一饮品,贩卖机内设置了饮品临时存放区域)。 购买者功能包括: 1、查询所有饮品信息(编号、名称、价格)。 2、根据饮品编号购买饮品,并显示购买信息(名称、价格、购买时间)。 3、退出购买界面,返回主界面。 经营者功能包括: 1、登陆,用户名和密码输入正确才能进入管理界面,不成功时能提示用户 名或密码错误。 2、查询所有饮品信息(编号、名称、价格)。 3、按饮品名称统计该饮品个数。 4、添加饮品(编号自动生成,名称,价格)。 5、查询所有购买记录(名称,价格,购买时间)。 6、统计当日营业额。 7、退出管理界面,返回主界面。 主菜单为: 饮品自动贩卖机系统 1 购买入口 2 经营入口 - 22 - 0 退出系统 两类用户各自的菜单根据功能自主设计。
时间: 2024-02-05 11:11:06 浏览: 190
以下是根据要求生成的Java代码,包含了购买者和经营者两个角色的功能:
```
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
public class VendingMachine {
private static final int MAX_CAPACITY = 10; // 贩卖机总容量
private static final int LOGIN_ATTEMPTS = 3; // 登录尝试次数
private static final String USERNAME = "admin"; // 经营者用户名
private static final String PASSWORD = "password"; // 经营者密码
private ArrayList<Drink> drinks; // 饮品列表
private ArrayList<PurchaseRecord> purchaseRecords; // 购买记录列表
private HashMap<String, Integer> drinkCount; // 饮品数量统计
public VendingMachine() {
drinks = new ArrayList<>();
purchaseRecords = new ArrayList<>();
drinkCount = new HashMap<>();
}
public void start() {
Scanner scanner = new Scanner(System.in);
int choice = -1;
while (choice != 0) {
System.out.println("饮品自动贩卖机系统");
System.out.println("1. 购买入口");
System.out.println("2. 经营入口");
System.out.println("0. 退出系统");
System.out.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
customerMenu(scanner);
break;
case 2:
if (login(scanner)) {
managerMenu(scanner);
} else {
System.out.println("登录失败!");
}
break;
case 0:
break;
default:
System.out.println("无效的选项!");
break;
}
}
scanner.close();
}
private void customerMenu(Scanner scanner) {
int choice = -1;
while (choice != 3) {
System.out.println("购买饮品");
System.out.println("1. 查询所有饮品信息");
System.out.println("2. 根据饮品编号购买饮品");
System.out.println("3. 返回主界面");
System.out.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
displayDrinkList();
break;
case 2:
purchaseDrink(scanner);
break;
case 3:
break;
default:
System.out.println("无效的选项!");
break;
}
}
}
private void managerMenu(Scanner scanner) {
int choice = -1;
while (choice != 7) {
System.out.println("经营饮品自动贩卖机");
System.out.println("1. 查询所有饮品信息");
System.out.println("2. 按饮品名称统计该饮品个数");
System.out.println("3. 添加饮品");
System.out.println("4. 查询所有购买记录");
System.out.println("5. 统计当日营业额");
System.out.println("6. 修改经营者用户名和密码");
System.out.println("7. 返回主界面");
System.out.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
displayDrinkList();
break;
case 2:
countDrinkByName(scanner);
break;
case 3:
addDrink(scanner);
break;
case 4:
displayPurchaseRecords();
break;
case 5:
calculateRevenue();
break;
case 6:
changeCredentials(scanner);
break;
case 7:
break;
default:
System.out.println("无效的选项!");
break;
}
}
}
private void displayDrinkList() {
System.out.println("饮品信息列表:");
System.out.println("编号\t名称\t价格");
for (Drink drink : drinks) {
System.out.printf("%s\t%s\t%.2f元\n", drink.getId(), drink.getName(), drink.getPrice());
}
}
private void purchaseDrink(Scanner scanner) {
System.out.print("请输入饮品编号:");
String id = scanner.next();
Drink drink = findDrinkById(id);
if (drink == null) {
System.out.println("饮品不存在!");
return;
}
if (drinks.remove(drink)) {
purchaseRecords.add(new PurchaseRecord(drink));
System.out.printf("您已购买%s,花费%.2f元,购买时间%s。\n", drink.getName(), drink.getPrice(), new Date());
} else {
System.out.println("饮品已售罄!");
}
}
private void countDrinkByName(Scanner scanner) {
System.out.print("请输入饮品名称:");
String name = scanner.next();
int count = 0;
for (Drink drink : drinks) {
if (drink.getName().equals(name)) {
count++;
}
}
System.out.printf("%s共有%d个。\n", name, count);
}
private void addDrink(Scanner scanner) {
if (drinks.size() >= MAX_CAPACITY) {
System.out.println("饮品已达到最大容量,无法添加!");
return;
}
System.out.print("请输入饮品名称:");
String name = scanner.next();
System.out.print("请输入饮品价格:");
double price = scanner.nextDouble();
Drink drink = new Drink(name, price);
drinks.add(drink);
System.out.printf("饮品%s添加成功,编号为%s。\n", drink.getName(), drink.getId());
}
private void displayPurchaseRecords() {
System.out.println("购买记录列表:");
System.out.println("名称\t价格\t购买时间");
for (PurchaseRecord record : purchaseRecords) {
System.out.printf("%s\t%.2f元\t%s\n", record.getDrink().getName(), record.getDrink().getPrice(), record.getPurchaseTime());
}
}
private void calculateRevenue() {
double revenue = 0;
for (PurchaseRecord record : purchaseRecords) {
revenue += record.getDrink().getPrice();
}
System.out.printf("当日营业额为%.2f元。\n", revenue);
}
private boolean login(Scanner scanner) {
int attempts = 0;
while (attempts < LOGIN_ATTEMPTS) {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
if (username.equals(USERNAME) && password.equals(PASSWORD)) {
return true;
} else {
System.out.println("用户名或密码错误!");
attempts++;
}
}
return false;
}
private void changeCredentials(Scanner scanner) {
System.out.print("请输入新的用户名:");
String username = scanner.next();
System.out.print("请输入新的密码:");
String password = scanner.next();
USERNAME = username;
PASSWORD = password;
System.out.println("经营者用户名和密码修改成功!");
}
private Drink findDrinkById(String id) {
for (Drink drink : drinks) {
if (drink.getId().equals(id)) {
return drink;
}
}
return null;
}
private class Drink {
private String id;
private String name;
private double price;
public Drink(String name, double price) {
this.id = generateId();
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
private String generateId() {
return String.format("D%03d", drinks.size() + 1);
}
}
private class PurchaseRecord {
private Drink drink;
private Date purchaseTime;
public PurchaseRecord(Drink drink) {
this.drink = drink;
this.purchaseTime = new Date();
}
public Drink getDrink() {
return drink;
}
public String getPurchaseTime() {
return purchaseTime.toString();
}
}
}
```
阅读全文