根据类图,实现java代码:@startuml class BankAccount { -accountNumber : String -balance : double -ownerName : String +BankAccount(accountNumber:String, ownerName:String) +getAccountNumber():String +getBalance():double +getOwnerName():String +deposit(amount:double):void +withdraw(amount:double):void } class BankAccountDAO { -accounts : Map<String, BankAccount> -fileName : String +BankAccountDAO(fileName:String) +getAccount(accountNumber:String):BankAccount +getAllAccounts():List<BankAccount> +addAccount(account:BankAccount):void +updateAccount(account:BankAccount):void +deleteAccount(accountNumber:String):void -saveToFile():void } BankAccount -> BankAccountDAO @enduml
时间: 2024-02-01 10:13:46 浏览: 74
```
public class BankAccount {
private String accountNumber;
private double balance;
private String ownerName;
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = 0.0;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public String getOwnerName() {
return ownerName;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
}
import java.io.*;
import java.util.*;
public class BankAccountDAO {
private Map<String, BankAccount> accounts;
private String fileName;
public BankAccountDAO(String fileName) {
this.fileName = fileName;
accounts = new HashMap<String, BankAccount>();
loadFromFile();
}
public BankAccount getAccount(String accountNumber) {
return accounts.get(accountNumber);
}
public List<BankAccount> getAllAccounts() {
return new ArrayList<BankAccount>(accounts.values());
}
public void addAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void updateAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void deleteAccount(String accountNumber) {
accounts.remove(accountNumber);
saveToFile();
}
private void loadFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String accountNumber = parts[0];
String ownerName = parts[1];
double balance = Double.parseDouble(parts[2]);
BankAccount account = new BankAccount(accountNumber, ownerName);
account.deposit(balance);
accounts.put(accountNumber, account);
}
reader.close();
} catch (IOException e) {
System.out.println("Failed to load accounts from file: " + e.getMessage());
}
}
private void saveToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (BankAccount account : accounts.values()) {
String line = account.getAccountNumber() + "," + account.getOwnerName() + "," + account.getBalance();
writer.write(line);
writer.newLine();
}
writer.close();
} catch (IOException e) {
System.out.println("Failed to save accounts to file: " + e.getMessage());
}
}
}
```
阅读全文