自动刷卡机GateMachine类:
public class GateMachine {
// 分离欢迎信息、购票成功信息、购票失败信息、显示余额、支付扣款、读取余额等功能到不同方法模块中
// 设计编写构造函数、使得用户在创建对象的同时,实现price的初始化。
// 设计对price进行修改维护的方法
// 可以看作是一个购票系统,所以需要有价格price这个数据,一般设为私有的
private double price;
// 因为需要显示余额信息,所以需要创建一个用户信息
private Account act;
// 题目要求对价格price在构造方法中进行初始化
public GateMachine(double price,Account act){
this.price = price;
this.act = act;
}
// 欢迎信息
public void welcome(){
System.out.println("欢迎使用无人售票机");
System.out.println("请选择需要的服务类型:");
}
// 购票成功信息
public void finishPay(){
act.setBalance(act.getBalance()-price);
System.out.println("购票成功!");
}
// 购票失败信息
public void failPay(){
System.out.println("购票失败!");
}
// 显示余额
public void printBalance(){
System.out.println(act.getBalance());
}
// 支付扣款
public void pay(){
act.setBalance(act.getBalance()-price);
}
// 读取余额
// 感觉这个方法和显示余额那个方法有点类似,我认为可以是一个方法
public double getYue(){
return act.getBalance();
}
// 设计对price进行修改维护的方法
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
用户Account类(用于提供用户信息):
public class Account {
// 由于该题只需余额信息 所以只创建了余额和姓名信息,有需要就自己添加信息
private String name;
private double balance;
// 无参构造
public Account(){
}
// 有参构造
// 对用户进行初始化
public Account(String name,double balance){
this.name = name;
this.balance = balance;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
GateMachineTest测试类:
import java.util.Scanner;
public class GateMachineTest {
public static void main(String[] args) {
// 在main()中创建GateMachine类的容器,命名为machines
// 容器中创建3个价格不同的GateMachine对象
// 一次调用三个对象的各种方法,实现对闸机功能的模拟
// 题目中说的创建GateMachine类的容器相当于创建GateMachine类的数组, 容量为3
// 创建1个用户,用于分别存入三个机器中去
Account a1 = new Account("张三", 1000);
// 再创建三个GateMachine,并对其进行初始化
GateMachine m1 = new GateMachine(50, a1);
GateMachine m2 = new GateMachine(100, a1);
GateMachine m3 = new GateMachine(150, a1);
// 将三个机器放入GateMachine容器中
// 题目中说的创建GateMachine类的容器相当于创建GateMachine类的数组, 容量为3
GateMachine[] machines = {m1, m2, m3};
Scanner scanner = new Scanner(System.in);
// 用于重复使用这个闸机
while (true) {
System.out.println("支付类型:1、50 2、100 3、150");
System.out.println("请选择支付类型:");
int t1 = scanner.nextInt();
if (t1 == 1) {
machines[0].welcome();
System.out.println(
"1、购票" +
"2、查询余额" +
"3、退出");
int t2 = scanner.nextInt();
//该模块表示选择支付通道为50的
if (t2 == 1) {
if (machines[0].getYue() >= machines[0].getPrice()) {
machines[0].pay();
machines[0].finishPay();
} else {
machines[0].failPay();
}
} else if (t2 == 2) {
machines[0].printBalance();
} else if (t2 == 3) {
System.out.println("欢迎下次使用!");
break;
}
}
//该模块表示选择支付通道为100的
if (t1 == 2) {
machines[1].welcome();
System.out.println(
"1、购票" +
"2、查询余额" +
"3、退出");
int t2 = scanner.nextInt();
if (t2 == 1) {
if (machines[1].getYue() >= machines[1].getPrice()) {
machines[1].pay();
machines[1].finishPay();
} else {
machines[1].failPay();
}
} else if (t2 == 2) {
machines[1].printBalance();
} else if (t2 == 3) {
System.out.println("欢迎下次使用!");
break;
}
}
//该模块表示选择支付通道为150的
if (t1 == 3) {
machines[2].welcome();
System.out.println(
"1、购票" +
"2、查询余额" +
"3、退出");
int t2 = scanner.nextInt();
if (t2 == 1) {
if (machines[2].getYue() >= machines[2].getPrice()) {
machines[2].pay();
machines[2].finishPay();
} else {
machines[2].failPay();
}
} else if (t2 == 2) {
machines[2].printBalance();
} else if (t2 == 3) {
System.out.println("欢迎下次使用!");
break;
}
}
}
}
}
测试类也可以不用这种,就直接用那个调用那些方法就行,只是为了更方便,所以才这样写,避免多次编译运行。
如果你们有其他要求,就自行改写一下。