Slide 16
Slide 16 text
public class Account {
private String accountNumber;
private double balance;
public Account(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println(amount + "が預金されました。");
} else {
System.out.println("正の金額を入力してください。");
}
}
public void withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println(amount + "が引き出されました。");
} else {
System.out.println("引き出し額が不正か、残高不足です。
");
}
}
public void displayBalance() {
System.out.println("口座番号: " + accountNumber + " の残高: "
+ balance);
}
}
import java.util.HashMap;
import java.util.Map;
public class Bank {
private Map accounts;
public Bank() {
accounts = new HashMap<>();
}
public void createAccount(String accountNumber) {
if (!accounts.containsKey(accountNumber)) {
Account newAccount = new Account(accountNumber);
accounts.put(accountNumber, newAccount);
System.out.println("口座 " + accountNumber + " が作成されました。");
} else {
System.out.println("この口座番号は既に存在します。");
}
}
public void depositToAccount(String accountNumber, double amount) {
Account account = accounts.get(accountNumber);
if (account != null) {
account.deposit(amount);
} else {
System.out.println("指定された口座が見つかりません。");
}
}
public void withdrawFromAccount(String accountNumber, double amount) {
Account account = accounts.get(accountNumber);
if (account != null) {
account.withdraw(amount);
} else {
System.out.println("指定された口座が見つかりません。");
}
}
public void showBalance(String accountNumber) {
Account account = accounts.get(accountNumber);
if (account != null) {
account.displayBalance();
} else {
System.out.println("指定された口座が見つかりません。");
}
}
}
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
bank.createAccount("12345");
bank.depositToAccount("12345", 1000);
bank.withdrawFromAccount("12345", 500);
bank.showBalance("12345");
}
}
21. 【詳細設計】アクティビティ図(フローチャート)