用 java 完成以下题目 有一个银行账号,有若干个人向其内存钱,而另外的一些人从中取钱。 请编写程序模拟实现这种情况。参考代码如下: 账户account.java
时间: 2024-03-17 08:47:33 浏览: 119
好的,以下是一个简单的银行账户模拟程序,包括了账户类 `Account` 和主程序 `BankSimulation`:
`Account.java`:
```java
public class Account {
private String accountNumber; // 账户号码
private double balance; // 账户余额
public Account(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
// 存款
public void deposit(double amount) {
balance += amount;
}
// 取款
public void withdraw(double amount) {
if (amount > balance) {
System.out.println("余额不足,取款失败!");
return;
}
balance -= amount;
}
}
```
`BankSimulation.java`:
```java
import java.util.Scanner;
public class BankSimulation {
public static void main(String[] args) {
Account account = new Account("1234567890", 1000); // 创建账户
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请选择操作类型:");
System.out.println("1. 存款");
System.out.println("2. 取款");
System.out.println("3. 查询余额");
System.out.println("0. 退出");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.print("请输入存款金额:");
double depositAmount = scan.nextDouble();
account.deposit(depositAmount);
System.out.println("存款成功,账户余额为:" + account.getBalance());
break;
case 2:
System.out.print("请输入取款金额:");
double withdrawAmount = scan.nextDouble();
account.withdraw(withdrawAmount);
System.out.println("取款成功,账户余额为:" + account.getBalance());
break;
case 3:
System.out.println("账户余额为:" + account.getBalance());
break;
case 0:
System.out.println("谢谢使用!");
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
这个程序通过一个 `Account` 类来表示账户,提供了存款、取款和查询余额的操作。主程序 `BankSimulation` 则通过循环菜单的方式,让用户选择需要进行的操作。
阅读全文