GateMachine:改写自动刷卡机类,分离欢迎信息、购票成功信息、购票失败信息、显示余额、支付扣款、读取余额等功能到不同方法模块中,设计编写构造函数、使得用户在创建对象的同时实现price的初始化

自动刷卡机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;
                }
            }
        }
    }
}

测试类也可以不用这种,就直接用那个调用那些方法就行,只是为了更方便,所以才这样写,避免多次编译运行。

如果你们有其他要求,就自行改写一下。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值