package test;
import java.util.Scanner;
//定义test类
public class Test {
//用于存储账户信息
public String account;
public double[] moneyData;
public Test(String account) {
this.account = account;
}
//定义菜单方法
public void showMain() {
System.out.printf("欢迎使用热水卡充值系统,当前账号:%s\n",account);
System.out.println("【1】 热水卡充值");
System.out.println("【2】 查询充值金额");
System.out.println("【3】 支付充值金额");
System.out.println("【0】 退出系统");
}
//热水卡充值方法
public void saveOrder() {
Scanner but = new Scanner(System.in);
System.out.println("请输入充值的数量:");
this.moneyData = new double[but.nextInt()];
for(int i = 0; i < this.moneyData.length ; i ++) {
System.out.printf("请输入第 %d 张热水卡的金额:",i+1);
this.moneyData[i] = but.nextDouble();
}
//充值逻辑
}
//查询充值总金额的方法
public double getOrder(double[] data) {
//查询逻辑
int money = 0;
for(int i = 0; i < data.length ; i ++) {
money += data[i];
}
return money;
}
//定义支付的方法
public double payOrder(double[] data) {
//支付逻辑
Scanner but = new Scanner(System.in);
int PayMoney = but.nextInt();
int PayMoneyAdd = 0;
for(int i = 0 ; i < this.moneyData.length ; i ++) {
PayMoneyAdd += data[i];
}
return PayMoney - PayMoneyAdd;
}
public static void main(String[] args) {
//创建一个键盘输入方法
Scanner but = new Scanner(System.in);
System.out.println("请输入账户:");
//传入全局变量,存放账户信息
Test test = new Test(but.next());
//初始化数组
test.moneyData = new double[0];
//实现充值方法
while(true) {
//展示菜单
test.showMain();
//提示输入选择
System.out.println("请输入您的选择:");
//定义变量存放输入选项
String menu = but.next();
switch(menu) {
case "1"://充值
test.saveOrder();
break;
case "2"://查询
if(test.moneyData.length == 0) {
System.out.println("未存在充值记录!");
}else {
double TextData = test.getOrder(test.moneyData);
System.out.printf("当前存在%d张热水卡充值记录,您需支付充值金额为:%.2f\n",test.moneyData.length,TextData);
}
break;
case "3"://支付
System.out.println("请输入充值金额:");
double MoneyData = test.payOrder(test.moneyData);
do {
System.out.println("您输入的金额小于充值金额,请重新输入");
MoneyData = test.payOrder(test.moneyData);
}while(MoneyData < 0);
int PayMoneyAdd = 0;
for(int i = 0 ; i < test.moneyData.length ; i ++) {
PayMoneyAdd += test.moneyData[i];
}
System.out.printf("充值成功,共支付金额%.2f元,返回金额%.2f元\n",(double)PayMoneyAdd,MoneyData);
test.moneyData = new double[0];
break;
case "0"://退出
System.out.println("您已退出此系统");
System.exit(0);
break;
default://其它
System.out.println("无此选项,请重新选择");
test.showMain();
System.out.println("请输入您的选择:");
break;
}
}
}
}
Java写一个热水卡充值系统
发布于 2023-03-08 90 次阅读
Comments NOTHING