使用java语言,设计一个银行账户挂历系统,具体功能为:开户、存款、取款、转账、查询、修改密码、销户,请给出设计思路
时间: 2023-12-28 22:17:13 浏览: 149
设计思路:
1. 首先,需要创建一个账户类(Account),该类包含以下属性:
- 账户号码(accountNumber)
- 账户余额(balance)
- 账户持有人姓名(accountHolderName)
- 账户持有人地址(accountHolderAddress)
- 账户持有人电话号码(accountHolderPhoneNumber)
- 账户密码(password)
2. 接下来,创建一个银行类(Bank),该类包含以下属性:
- 银行名称(bankName)
- 所有账户的列表(accountsList)
3. 在银行类中实现如下方法:
- 开户(createAccount):输入账户的基本信息,生成账户号码并存入账户列表中。
- 存款(deposit):输入账户号码和存款金额,将账户余额增加存款金额。
- 取款(withdraw):输入账户号码和取款金额,将账户余额减去取款金额。
- 转账(transfer):输入源账户号码、目标账户号码和转账金额,将源账户余额减去转账金额,目标账户余额增加转账金额。
- 查询(getAccountDetails):输入账户号码,返回账户的基本信息和余额。
- 修改密码(changePassword):输入账户号码和新密码,将账户的密码修改为新密码。
- 销户(deleteAccount):输入账户号码,将该账户从账户列表中删除。
4. 在主函数中,创建银行对象和账户对象,并调用各个方法进行操作。
代码实现:
1. Account 类的代码实现:
```java
public class Account {
private String accountNumber;
private double balance;
private String accountHolderName;
private String accountHolderAddress;
private String accountHolderPhoneNumber;
private String password;
public Account(String accountNumber, String accountHolderName, String accountHolderAddress, String accountHolderPhoneNumber, String password) {
this.accountNumber = accountNumber;
this.balance = 0.0;
this.accountHolderName = accountHolderName;
this.accountHolderAddress = accountHolderAddress;
this.accountHolderPhoneNumber = accountHolderPhoneNumber;
this.password = password;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public String getAccountHolderName() {
return accountHolderName;
}
public String getAccountHolderAddress() {
return accountHolderAddress;
}
public String getAccountHolderPhoneNumber() {
return accountHolderPhoneNumber;
}
public String getPassword() {
return password;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance!");
}
}
public void transfer(Account targetAccount, double amount) {
if (amount <= balance) {
balance -= amount;
targetAccount.deposit(amount);
} else {
System.out.println("Insufficient balance!");
}
}
public String getAccountDetails() {
return "Account Number: " + accountNumber + "\n" +
"Account Holder Name: " + accountHolderName + "\n" +
"Account Holder Address: " + accountHolderAddress + "\n" +
"Account Holder Phone Number: " + accountHolderPhoneNumber + "\n" +
"Account Balance: " + balance;
}
public void changePassword(String newPassword) {
password = newPassword;
}
}
```
2. Bank 类的代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class Bank {
private String bankName;
private List<Account> accountsList;
public Bank(String bankName) {
this.bankName = bankName;
this.accountsList = new ArrayList<Account>();
}
public Account createAccount(String accountHolderName, String accountHolderAddress, String accountHolderPhoneNumber, String password) {
String accountNumber = String.valueOf(accountsList.size() + 1);
Account account = new Account(accountNumber, accountHolderName, accountHolderAddress, accountHolderPhoneNumber, password);
accountsList.add(account);
return account;
}
public void deposit(String accountNumber, double amount) {
Account account = getAccountByNumber(accountNumber);
if (account != null) {
account.deposit(amount);
} else {
System.out.println("Account not found!");
}
}
public void withdraw(String accountNumber, double amount) {
Account account = getAccountByNumber(accountNumber);
if (account != null) {
account.withdraw(amount);
} else {
System.out.println("Account not found!");
}
}
public void transfer(String sourceAccountNumber, String targetAccountNumber, double amount) {
Account sourceAccount = getAccountByNumber(sourceAccountNumber);
Account targetAccount = getAccountByNumber(targetAccountNumber);
if (sourceAccount != null && targetAccount != null) {
sourceAccount.transfer(targetAccount, amount);
} else {
System.out.println("Account not found!");
}
}
public String getAccountDetails(String accountNumber) {
Account account = getAccountByNumber(accountNumber);
if (account != null) {
return account.getAccountDetails();
} else {
return "Account not found!";
}
}
public void changePassword(String accountNumber, String newPassword) {
Account account = getAccountByNumber(accountNumber);
if (account != null) {
account.changePassword(newPassword);
} else {
System.out.println("Account not found!");
}
}
public void deleteAccount(String accountNumber) {
Account account = getAccountByNumber(accountNumber);
if (account != null) {
accountsList.remove(account);
} else {
System.out.println("Account not found!");
}
}
private Account getAccountByNumber(String accountNumber) {
for (Account account : accountsList) {
if (account.getAccountNumber().equals(accountNumber)) {
return account;
}
}
return null;
}
}
```
3. 主函数的代码实现:
```java
public static void main(String[] args) {
Bank bank = new Bank("ABC Bank");
// Create an account
Account account1 = bank.createAccount("John Doe", "123 Main St", "555-1234", "password123");
// Deposit money
bank.deposit(account1.getAccountNumber(), 1000.0);
// Withdraw money
bank.withdraw(account1.getAccountNumber(), 500.0);
// Transfer money
Account account2 = bank.createAccount("Jane Doe", "456 Main St", "555-5678", "password456");
bank.transfer(account1.getAccountNumber(), account2.getAccountNumber(), 200.0);
// Get account details
System.out.println(bank.getAccountDetails(account1.getAccountNumber()));
// Change password
bank.changePassword(account1.getAccountNumber(), "newpassword");
// Delete account
bank.deleteAccount(account1.getAccountNumber());
}
```
阅读全文