用java编写程序,模拟银行账户功能。 属性:账号,姓名,存款余额 方法:存款、取款、查询。 根据用户操作显示储户相关信息。 如存款操作后,显示储户原有余额、今日存款数额及最终存款余额。 取款时,若取款成功,显示信息提示;若最后余额小于最小余额,拒绝取款,并显示信息提示。
时间: 2023-05-17 09:06:38 浏览: 85
以下是用Java编写的模拟银行账户功能的代码:
```
public class BankAccount {
private String accountNumber;
private String name;
private double balance;
private double minBalance = 1000.0;
public BankAccount(String accountNumber, String name, double balance) {
this.accountNumber = accountNumber;
this.name = name;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("原有余额:" + (balance - amount));
System.out.println("今日存款数额:" + amount);
System.out.println("最终存款余额:" + balance);
}
public void withdraw(double amount) {
if (balance - amount < minBalance) {
System.out.println("余额不足,无法取款!");
} else {
balance -= amount;
System.out.println("取款成功!");
}
}
public void query() {
System.out.println("账号:" + accountNumber);
System.out.println("姓名:" + name);
System.out.println("余额:" + balance);
}
}
```
使用示例:
```
BankAccount account = new BankAccount("123456789", "张三", 5000.0);
account.query(); // 显示账户信息
account.deposit(1000.0); // 存款1000元
account.withdraw(2000.0); // 取款2000元
account.query(); // 显示账户信息
```
输出结果:
```
账号:123456789
姓名:张三
余额:5000.0
原有余额:5000.0
今日存款数额:1000.0
最终存款余额:6000.0
取款成功!
账号:123456789
姓名:张三
余额:4000.0
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)