ATM java基础练习

TestMain类 main方法

import java.util.Scanner;

import com.lovo.bean.ATM;

public class TestMain {

 public static void main(String[] args) {
  ATM atm = new ATM();
  atm.run();
 }

}
//ATM类

import java.util.Scanner;

public class ATM {

 public final int MAX_CASH = 500000;// ATM中的最大现金量

 private int cash;// ATM中的现金

 private User theUser;

 // 控制流程
 public void run() {
  boolean flag = false;
  this.init();
  this.welcome();
   if (this.login()) {
    while (true) {
    flag=false;
     int choice = this.choiceMenu();
     switch (choice) {
     case 1:
      this.query();
      break;
     case 2:
      this.getMoney();
      break;
     case 3:
      this.storeMoney();
      break;
     case 4:
      if(this.changePwd()&&!this.login()){
       System.out.println("三次登陆失败,您的卡被没收!");
       return ;
      }
      break;
     case 5:
      this.exit();
      break;
     default:
      System.out.println("没有该选项!");
      break;
     }
    }
   } else {
    System.out.println("三次登陆失败,您的卡被没收!");
   }
 }

 // 初始化
 private void init() {
  this.cash = 100000;
  this.theUser = new User("t98", "123456", 1200);
 }

 // 欢迎
 private void welcome() {
  System.out.println("******************************");
  System.out.println("*********周周的地下钱庄*********");
  System.out.println("******************************");
  System.out.println("************version:1.0*******");
  System.out.println("******************************");
  System.out.println("******************************");
 }

 // 登陆
 private boolean login() {
  int count = 0;
  do {
   count++;
   Scanner scan = new Scanner(System.in);
   System.out.println("请输入您的用户名:");
   String inputName = scan.next();
   System.out.println("请输入您的密码:");
   String inputPwd = scan.next();
   if (inputName.equals(theUser.getUsername())
     && inputPwd.equals(theUser.getPassword())) {
    System.out.println("欢迎您," + inputName);
    return true;
   }

  } while (count < 3);

  return false;
 }

 // 选择菜单
 private int choiceMenu() {
  Scanner scan = new Scanner(System.in);
  System.out.println("请选择您的操作:");
  System.out.println("1、查询;2、取款;3、存款;4、修改密码;5、退出");
  return scan.nextInt();
 }

 // 查询
 private void query() {
  System.out.println("您当前的余额是:" + theUser.getAccount() + "元");
 }

 // 取钱
 private void getMoney() {
  System.out.println("请输入您的取款金额:");
  Scanner scan = new Scanner(System.in);
  int inputMoney1 = scan.nextInt();
  if (inputMoney1 <= 0) {
   System.out.println("你见过哪家银行取负数的?");
  } else if (inputMoney1 % 100 != 0) {
   System.out.println("本ATM只支持存取百元整数!");
  } else if (inputMoney1 >= theUser.getAccount()) {
   System.out.println("对不起,余额不足!");
  } else if (theUser.getAccount() > this.cash) {
   System.out.println("对不起,本ATM机上余额不足!");
   System.out.println("最多可以取" + this.cash + "元。");
  } else {
   theUser.setAccount(theUser.getAccount() - inputMoney1);
   this.cash -= inputMoney1;
   System.out.println("操作已经成功,请收好!");
  }

 }

 // 存钱
 private void storeMoney() {
  System.out.println("请输入您的存款金额:");
  Scanner scan = new Scanner(System.in);
  int inputMoney2 = scan.nextInt();
  if (inputMoney2 <= 0) {
   System.out.println("你见过哪家银行存负数的?");
  } else if (inputMoney2 % 100 != 0) {
   System.out.println("本ATM只支持存取百元整数!");
  } else if (this.cash + inputMoney2 > this.MAX_CASH) {
   System.out.println("本ATM你最多还可以存" + (this.MAX_CASH - this.cash)
     + "元!");
  } else {
   theUser.setAccount(theUser.getAccount() + inputMoney2);
   this.cash += inputMoney2;
   System.out.println("操作已经成功,请查询检查一次!");
  }
 }

 // 修改密码
 private boolean changePwd() {
  System.out.println("请输入原密码:");
  Scanner scan = new Scanner(System.in);
  String oldPwd = scan.next();
  System.out.println("请输入新密码:");
  String newPwd = scan.next();
  System.out.println("请确认新密码:");
  String reNewPwd = scan.next();
  if (!theUser.getPassword().equals(oldPwd)) {
   System.out.println("原密码输入错误!");
   return false;
  } else if (!newPwd.equals(reNewPwd)) {
   System.out.println("两次新密码输入不一致!");
   return false;
  } else {
   theUser.setPassword(newPwd);
   System.out.println("密码修改成功!请牢记!");
   return true;
  }
 }

 // 退出
 private void exit() {
  System.out.println("谢谢您的使用,期待您的下次光临!");
  System.exit(0);
 }
}
//用户类

public class User {

 private String username;

 private String password;

 private int account;//卡上的钱

 public User() {

 }

 public User(String username, String password, int account) {
  this.username = username;
  this.password = password;
  this.account = account;
 }

 public int getAccount() {
  return account;
 }

 public void setAccount(int account) {
  this.account = account;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值