用Java实现ATM银行系统
时间: 2024-03-05 17:49:49 浏览: 83
银行ATM的java实现版本
好的,下面是一个简单的ATM银行系统Java代码示例。这个示例实现了基本的ATM操作,包括查询余额、取款、存款等,你可以根据自己的需要进行修改和扩展。
```
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BankAccount bankAccount = new BankAccount("123456", 1000);
while (true) {
System.out.println("请选择操作:");
System.out.println("1、查询余额");
System.out.println("2、取款");
System.out.println("3、存款");
System.out.println("4、退出");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("当前余额为:" + bankAccount.getBalance());
break;
case 2:
System.out.println("请输入取款金额:");
double withdrawAmount = input.nextDouble();
bankAccount.withdraw(withdrawAmount);
break;
case 3:
System.out.println("请输入存款金额:");
double depositAmount = input.nextDouble();
bankAccount.deposit(depositAmount);
break;
case 4:
System.out.println("感谢使用ATM系统,再见!");
System.exit(0);
default:
System.out.println("无效选择,请重新输入!");
break;
}
}
}
}
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("取款成功,当前余额为:" + balance);
} else {
System.out.println("余额不足,取款失败!");
}
}
public void deposit(double amount) {
balance += amount;
System.out.println("存款成功,当前余额为:" + balance);
}
}
```
这段代码实现了一个简单的ATM银行系统,包括了查询余额、取款、存款等基本操作。你可以根据自己的需要进行修改和扩展。
阅读全文