使用Java来做一个能够有正常功能的饭卡管理系统
时间: 2023-07-16 17:13:17 浏览: 176
mealCardSystem.rar
3星 · 编辑精心推荐
一个饭卡管理系统可以包括以下功能:
1. 管理员登录:管理员需要输入用户名和密码才能登录系统。
2. 饭卡信息管理:管理员可以添加、删除和修改饭卡信息,例如饭卡号码、持卡人姓名、余额等。
3. 消费记录管理:系统需要记录每次使用饭卡的消费记录,包括时间、金额、消费类型等信息。
4. 充值功能:持卡人可以通过管理员或自助充值机来给自己的饭卡充值。
5. 查询功能:持卡人和管理员都可以查询饭卡的余额、消费记录等信息。
以下是一个使用 Java 实现的简单的饭卡管理系统的示例代码。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class MealCardManagementSystem {
static Scanner scanner = new Scanner(System.in);
static ArrayList<MealCard> mealCards = new ArrayList<>();
public static void main(String[] args) {
System.out.println("Welcome to Meal Card Management System!");
// 添加测试数据
mealCards.add(new MealCard("100001", "张三", 100));
mealCards.add(new MealCard("100002", "李四", 50));
mealCards.add(new MealCard("100003", "王五", 80));
while (true) {
System.out.println();
System.out.println("Please select the operation you want to perform:");
System.out.println("1. Administrator login");
System.out.println("2. Cardholder login");
System.out.println("3. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
adminLogin();
break;
case 2:
cardholderLogin();
break;
case 3:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
// 管理员登录
static void adminLogin() {
System.out.println("Please enter your username:");
String username = scanner.next();
System.out.println("Please enter your password:");
String password = scanner.next();
if (username.equals("admin") && password.equals("123456")) {
adminMenu();
} else {
System.out.println("Wrong username or password, please try again.");
}
}
// 管理员菜单
static void adminMenu() {
while (true) {
System.out.println();
System.out.println("Please select the operation you want to perform:");
System.out.println("1. Add a meal card");
System.out.println("2. Delete a meal card");
System.out.println("3. Update a meal card");
System.out.println("4. View all meal cards");
System.out.println("5. Back to main menu");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addMealCard();
break;
case 2:
deleteMealCard();
break;
case 3:
updateMealCard();
break;
case 4:
viewAllMealCards();
break;
case 5:
return;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
// 添加饭卡
static void addMealCard() {
System.out.println("Please enter the meal card number:");
String number = scanner.next();
// 检查饭卡号是否已存在
for (MealCard mealCard : mealCards) {
if (mealCard.getNumber().equals(number)) {
System.out.println("The meal card number already exists, please try again.");
return;
}
}
System.out.println("Please enter the cardholder's name:");
String name = scanner.next();
System.out.println("Please enter the balance:");
double balance = scanner.nextDouble();
MealCard mealCard = new MealCard(number, name, balance);
mealCards.add(mealCard);
System.out.println("Add meal card " + number + " success.");
}
// 删除饭卡
static void deleteMealCard() {
System.out.println("Please enter the meal card number:");
String number = scanner.next();
for (MealCard mealCard : mealCards) {
if (mealCard.getNumber().equals(number)) {
mealCards.remove(mealCard);
System.out.println("Delete meal card " + number + " success.");
return;
}
}
System.out.println("The meal card number does not exist, please try again.");
}
// 修改饭卡
static void updateMealCard() {
System.out.println("Please enter the meal card number:");
String number = scanner.next();
for (MealCard mealCard : mealCards) {
if (mealCard.getNumber().equals(number)) {
System.out.println("Please enter the cardholder's name(" + mealCard.getName() + "):");
String name = scanner.next();
System.out.println("Please enter the balance(" + mealCard.getBalance() + "):");
double balance = scanner.nextDouble();
mealCard.setName(name);
mealCard.setBalance(balance);
System.out.println("Update meal card " + number + " success.");
return;
}
}
System.out.println("The meal card number does not exist, please try again.");
}
// 查看所有饭卡
static void viewAllMealCards() {
System.out.println("Meal Card Number\tCardholder's Name\tBalance");
for (MealCard mealCard : mealCards) {
System.out.println(mealCard.getNumber() + "\t" + mealCard.getName() + "\t" + mealCard.getBalance());
}
}
// 持卡人登录
static void cardholderLogin() {
System.out.println("Please enter your meal card number:");
String number = scanner.next();
for (MealCard mealCard : mealCards) {
if (mealCard.getNumber().equals(number)) {
cardholderMenu(mealCard);
return;
}
}
System.out.println("The meal card number does not exist, please try again.");
}
// 持卡人菜单
static void cardholderMenu(MealCard mealCard) {
while (true) {
System.out.println();
System.out.println("Please select the operation you want to perform:");
System.out.println("1. View balance");
System.out.println("2. View consumption records");
System.out.println("3. Recharge");
System.out.println("4. Back to main menu");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Your balance is " + mealCard.getBalance() + ".");
break;
case 2:
viewConsumptionRecords(mealCard);
break;
case 3:
recharge(mealCard);
break;
case 4:
return;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
// 查看消费记录
static void viewConsumptionRecords(MealCard mealCard) {
System.out.println("Time\tAmount\tType");
for (String record : mealCard.getConsumptionRecords()) {
System.out.println(record);
}
}
// 充值
static void recharge(MealCard mealCard) {
System.out.println("Please enter the recharge amount:");
double amount = scanner.nextDouble();
mealCard.setBalance(mealCard.getBalance() + amount);
mealCard.addConsumptionRecord(System.currentTimeMillis(), amount, "Recharge");
System.out.println("Recharge success, your balance is " + mealCard.getBalance() + ".");
}
}
class MealCard {
private String number;
private String name;
private double balance;
private ArrayList<String> consumptionRecords = new ArrayList<>();
public MealCard(String number, String name, double balance) {
this.number = number;
this.name = name;
this.balance = balance;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public ArrayList<String> getConsumptionRecords() {
return consumptionRecords;
}
public void addConsumptionRecord(long time, double amount, String type) {
String record = time + "\t" + amount + "\t" + type;
consumptionRecords.add(record);
}
}
```
这是一个简单的饭卡管理系统,你可以根据需要进行修改和完善。
阅读全文