温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java项目如何实现模拟ATM机

发布时间:2020-07-21 14:36:08 来源:亿速云 阅读:286 作者:小猪 栏目:编程语言

这篇文章主要讲解了Java项目如何实现模拟ATM机,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

项目名称

模拟ATM机

项目描述

简单实现ATM机功能

代码实现

测试类

public class Test {
    //模拟多功能ATM机
  public static void main(String[] args) {
    ATM atm = new ATM();
    atm.opearte();
  }
}

主类:实现主方法

public class ATM {
  private Bank bank;
  public ATM(){
    bank = new Bank();
  }
  private void success(BankCard bankCard) {
    while (true) {
      System.out.println("请选择:1、查询余额,2、取款,3、存款,4、转账,5、退卡");
      Scanner scanner = new Scanner(System.in);
      int choice = scanner.nextInt();
      if (choice == 5) {
        return;
      }
      switch (choice) {
        case 1:
          int money = bank.printf(bankCard);
          System.out.println("余额:" + money);
          break;
        case 2:
          System.out.println("请输入取款金额:");
          money = scanner.nextInt();
          bankCard.withdraw(money);
          break;
        case 3:
          System.out.println("请输入存款金额:");
          money = scanner.nextInt();
          bankCard.deposit(money);
          break;
        case 4:
          System.out.println("请输入对方账户id:");
          int id = scanner.nextInt();
          BankCard card = bank.transfer(id);
          if(card != null){
            System.out.println("请输入转账金额:");
            money = scanner.nextInt();
            if(money <= bankCard.getMoney()){
              bankCard.withdraw(money);
              card.deposit(money);
            }else {
              System.out.println("转账失败");
            }
          }else {
            System.out.println("未找到该账户");
          }
          break;
        default:
      }
    }
  }
  private void login(){ //登录
    System.out.println("请输入账号,密码");
    Scanner scanner = new Scanner(System.in);
    int id = scanner.nextInt();
    int password = scanner.nextInt();
    BankCard bankCard = bank.search(id,password);
    if(bankCard != null){
      System.out.println("登录成功");
      success(bankCard);
    }else {
      System.out.println("登录失败,请注册");
    }
  }
  private void register(){ //注册
    System.out.println("请输入账号,密码");
    Scanner scanner = new Scanner(System.in);
    int id = scanner.nextInt();
    int password = scanner.nextInt();
    BankCard bankCard = bank.search(id,password);
    if(bankCard != null){
      System.out.println("该账号已存在,请直接登录");
    }else {
      bank.addcard(id,password);
      System.out.println("注册成功");
    }
  }
  public void opearte() {
    while (true) {
      System.out.println("请选择:1、登录,2、注册,3、退出");
      Scanner scanner = new Scanner(System.in);
      int choice = scanner.nextInt();
      if (choice == 3) {
        return;
      }
      switch (choice) {
        case 1:
          login();
          break;
        case 2:
          register();
          break;
        default:
      }
    }
  }
}

银行类:一个银行对应多个ATM机

public class Bank {
  private BankCard[] bankCards;
  private final int INITSIZE = 10;
  private int size;
  public Bank(){
    bankCards = new BankCard[INITSIZE];
    size = 0;
  }
  public BankCard search(int id,int password){ //搜索
    BankCard[] bankCard = bankCards;
    for(int i=0;i<size;i++){
      if(bankCards[i].getId() == id && bankCards[i].getPassword() == password){
        return bankCard[i];
      }
    }
    return null;
  }
  public void addcard(int id,int password){ //注册
    BankCard bankCard = new BankCard(id, password);
    if (size == bankCards.length){
      bankCards = Arrays.copyOf(bankCards,bankCards.length * 2);
    }
    bankCards[size++] = bankCard;
  }
  public int printf(BankCard bankCard){ //显示余额
     return bankCard.getMoney();
  }
  public BankCard transfer(int id){
    for(int i=0;i<size;i++){
      if(bankCards[i].getId() == id){
        return bankCards[i];
      }
    }
    return null;
  }
}

银行卡类:不同账户的操作

public class BankCard {
  private int id;
  private int password;
  private int money;
  private int size = 0;
  public BankCard(int id,int password){
    this.id = id;
    this.password = password;
    money = 0;
  }
  public int getId(){
    return id;
  }
  public int getPassword(){
    return password;
  }
  public int getMoney(){
    return money;
  }
  public void withdraw(int money){ //取款
    if(this.money >= money){
      this.money -= money;
    }else {
      System.out.println("取款失败");
    }
  }
  public void deposit(int money){ //存款
    this.money += money;
  }
}

看完上述内容,是不是对Java项目如何实现模拟ATM机有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI