21xrx.com
2024-11-22 12:19:49 Friday
登录
文章检索 我的文章 写文章
使用Java编写ATM机系统
2023-06-16 11:00:12 深夜i     --     --

ATM机是现代人们非常常见的一种取款工具,为了提高ATM机系统的效率和可靠性,我们可以使用Java编写ATM机系统。通过使用Java语言的面向对象特性和封装性,我们可以实现一个结构清晰、易于维护的ATM机系统。

代码案例:

下面是ATM机系统的Java代码实现:

public class Account {

  private String name;

  private String password;

  private double balance;

  // constructor

  public Account(String name, String password)

    this.name = name;

    this.password = password;

    this.balance = 0;

  // getters and setters

  public String getName()

    return name;

  public void setName(String name)

    this.name = name;

  public String getPassword()

    return password;

  public void setPassword(String password)

    this.password = password;

  public double getBalance()

    return balance;

  public void setBalance(double balance)

    this.balance = balance;

  // methods

  public boolean login(String password) {

    return this.password.equals(password);

  }

  public void deposit(double amount) {

    balance += amount;

  }

  public boolean withdraw(double amount) {

    if (amount > balance)

      return false;

     else

      balance -= amount;

      return true;

  }

}

public class ATM {

  private Account account;

  private Scanner scanner;

  // constructor

  public ATM(Account account) {

    this.account = account;

    this.scanner = new Scanner(System.in);

  }

  // methods

  public void run() {

    while (true) {

      System.out.println("Welcome to ATM");

      System.out.println("Please enter your password:");

      String password = scanner.nextLine();

      if (!account.login(password)) {

        System.out.println("Invalid password. Please try again.");

        continue;

      }

      System.out.println("What would you like to do?");

      System.out.println("1. Check balance");

      System.out.println("2. Deposit");

      System.out.println("3. Withdraw");

      System.out.println("4. Exit");

      int choice = Integer.parseInt(scanner.nextLine());

      switch (choice) {

        case 1:

          System.out.println("Your balance is: " + account.getBalance());

          break;

        case 2:

          System.out.println("How much would you like to deposit?");

          double depositAmount = Double.parseDouble(scanner.nextLine());

          account.deposit(depositAmount);

          System.out.println("Your new balance is: " + account.getBalance());

          break;

        case 3:

          System.out.println("How much would you like to withdraw?");

          double withdrawAmount = Double.parseDouble(scanner.nextLine());

          if (!account.withdraw(withdrawAmount)) {

            System.out.println("Insufficient balance.");

          } else {

            System.out.println("Your new balance is: " + account.getBalance());

          }

          break;

        case 4:

          System.out.println("Thank you for using ATM.");

          System.exit(0);

        default:

          System.out.println("Invalid choice. Please try again.");

          break;

      }

    }

  }

}

public class Main {

  public static void main(String[] args) {

    Account account = new Account("John", "123456");

    ATM atm = new ATM(account);

    atm.run();

  }

}

运行以上代码后,可使用ATM机系统的功能,如查询余额、存款和取款等。

关键词:

1. ATM

2. Java

3. 面向对象

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复