用java编写一个基本账户类,成员变量包含账号,储户姓名和存款金额,方法有存款和取款。编写一个测试程序来测试所定义的账号类是否能实现存款和取款功能
时间: 2024-05-13 18:21:12 浏览: 52
Java语言编写实现银行账开户存款取款和转账等功能.pdf
好的,这是一个关于Java的编程问题,我可以帮您回答。下面是一个基本的账户类的代码示例,包括存款和取款方法:
```
public class Account {
private String accountNumber;
private String accountHolderName;
private double balance;
public Account(String accountNumber, String accountHolderName, double initialBalance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance < amount) {
System.out.println("Insufficient balance!");
} else {
balance -= amount;
}
}
public void showBalance() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder Name: " + accountHolderName);
System.out.println("Balance: " + balance);
}
}
```
然后,您可以使用以下测试程序来测试该账户类是否可以实现存款和取款功能:
```
public class AccountTest {
public static void main(String[] args) {
Account account = new Account("1234567890", "John Smith", 1000.00);
account.showBalance();
account.deposit(500.00);
account.showBalance();
account.withdraw(200.00);
account.showBalance();
account.withdraw(2000.00);
account.showBalance();
}
}
```
输出结果应该是:
```
Account Number: 1234567890
Account Holder Name: John Smith
Balance: 1000.0
Account Number: 1234567890
Account Holder Name: John Smith
Balance: 1500.0
Account Number: 1234567890
Account Holder Name: John Smith
Balance: 1300.0
Insufficient balance!
Account Number: 1234567890
Account Holder Name: John Smith
Balance: 1300.0
```
阅读全文