使用java模拟一个简易的银行的用户行为
时间: 2023-05-18 21:03:00 浏览: 84
利用java实现模拟银行系统
好的,我可以回答这个问题。以下是一个简单的Java代码示例,用于模拟银行用户的行为:
import java.util.Scanner;
public class BankSimulation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount();
System.out.println("欢迎使用银行系统!");
while (true) {
System.out.println("请选择您要进行的操作:");
System.out.println("1. 存款");
System.out.println("2. 取款");
System.out.println("3. 查询余额");
System.out.println("4. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入存款金额:");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
System.out.println("存款成功,当前余额为:" + account.getBalance());
break;
case 2:
System.out.println("请输入取款金额:");
double withdrawAmount = scanner.nextDouble();
if (account.withdraw(withdrawAmount)) {
System.out.println("取款成功,当前余额为:" + account.getBalance());
} else {
System.out.println("余额不足,取款失败!");
}
break;
case 3:
System.out.println("当前余额为:" + account.getBalance());
break;
case 4:
System.out.println("感谢使用银行系统,再见!");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入!");
break;
}
}
}
}
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
} else {
return false;
}
}
public double getBalance() {
return balance;
}
}
希望这个代码示例能够帮助您模拟银行用户的行为。
阅读全文