// 抽象クラス BankAccount ã®å®šç¾©
abstract class BankAccount {
private double balance; // 残高
// コンストラクタ
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
// 抽象メソッド deposit
public abstract void deposit(double amount);
// 抽象メソッド withdraw
public abstract void withdraw(double amount);
// 残高をå–å¾—ã™ã‚‹ãƒ¡ã‚½ãƒƒãƒ‰
public double getBalance() {
return balance;
}
}
// 具象クラス SavingsAccount ã®å®šç¾©
class SavingsAccount extends BankAccount {
private double interestRate; // 利率
// コンストラクタ
public SavingsAccount(double initialBalance, double interestRate) {
super(initialBalance);
this.interestRate = interestRate;
}
// deposit メソッドã®å®Ÿè£…
@Override
public void deposit(double amount) {
double interest = amount * interestRate;
super.deposit(amount + interest);
System.out.println("Deposited: $" + amount + ", Interest Earned: $" + interest);
}
// withdraw メソッドã®å®Ÿè£…
@Override
public void withdraw(double amount) {
super.withdraw(amount);
System.out.println("Withdrawn: $" + amount);
}
}
// 具象クラス CheckingAccount ã®å®šç¾©
class CheckingAccount extends BankAccount {
private double overdraftFee; // オーãƒãƒ¼ãƒ‰ãƒ©ãƒ•ト手数料
// コンストラクタ
public CheckingAccount(double initialBalance, double overdraftFee) {
super(initialBalance);
this.overdraftFee = overdraftFee;
}
// deposit メソッドã®å®Ÿè£…
@Override
public void deposit(double amount) {
super.deposit(amount);
System.out.println("Deposited: $" + amount);
}
// withdraw メソッドã®å®Ÿè£…
@Override
public void withdraw(double amount) {
if (amount > getBalance()) {
double totalAmount = amount + overdraftFee;
super.withdraw(totalAmount);
System.out.println("Overdraft Occurred! Withdrawn: $" + amount + ", Overdraft Fee: $" + overdraftFee);
} else {
super.withdraw(amount);
System.out.println("Withdrawn: $" + amount);
}
}
}
// メインクラス
public class Main {
public static void main(String[] args) {
// SavingsAccount ã®åˆ©ç”¨ä¾‹
SavingsAccount savingsAccount = new SavingsAccount(1000, 0.05);
savingsAccount.deposit(500);
savingsAccount.withdraw(200);
System.out.println("Savings Account Balance: $" + savingsAccount.getBalance());
// CheckingAccount ã®åˆ©ç”¨ä¾‹
CheckingAccount checkingAccount = new CheckingAccount(500, 20);
checkingAccount.deposit(300);
checkingAccount.withdraw(800);
System.out.println("Checking Account Balance: $" + checkingAccount.getBalance());
}
}
ã“ã®ä¾‹ã§ã¯ã€BankAccount クラスを抽象クラスã¨ã—ã¦å®šç¾©ã—ã€ãã®ä¸ã« deposit 㨠withdraw ã¨ã„ã†æŠ½è±¡ãƒ¡ã‚½ãƒƒãƒ‰ã‚’å®£è¨€ã—ã¾ã—ãŸã€‚ãれを継承ã™ã‚‹ SavingsAccount 㨠CheckingAccount クラスãŒã€ãれãžã‚Œã®ç‰¹æœ‰ã®æŒ¯ã‚‹èˆžã„を実装ã—ã¦ã„ã¾ã™ã€‚
å‡ºåŠ›çµæžœï¼š
Deposited: $500.0, Interest Earned: $25.0
Withdrawn: $200.0
Savings Account Balance: $1325.0
Deposited: $300.0
Overdraft Occurred! Withdrawn: $800.0, Overdraft Fee: $20.0
Checking Account Balance: $0.0
å„ã‚¢ã‚«ã‚¦ãƒ³ãƒˆã®æ“作ã«å¯¾ã™ã‚‹å‡ºåŠ›ã‚’èª¬æ˜Žã—ã¾ã™ã€‚
SavingsAccount ã®åˆ©ç”¨ä¾‹ï¼š
- 500ドルをデãƒã‚¸ãƒƒãƒˆã—ã€ãã®çµæžœã€åˆ©å25ドルãŒåŠ ç®—ã•れã¦ã„ã¾ã™ã€‚
- 200ドルを引ã出ã—ã¾ã—ãŸã€‚
- Savings Accountã®æ®‹é«˜ã¯ãƒ‡ãƒã‚¸ãƒƒãƒˆã¨åˆ©åã®åˆè¨ˆã§1325ドルã§ã™ã€‚
CheckingAccount ã®åˆ©ç”¨ä¾‹ï¼š
- 300ドルをデãƒã‚¸ãƒƒãƒˆã—ã¾ã—ãŸã€‚
- 800ドルを引ã出ã—ã¾ã—ãŸãŒã€æ®‹é«˜ä¸è¶³ã®ãŸã‚オーãƒãƒ¼ãƒ‰ãƒ©ãƒ•トãŒç™ºç”Ÿã—ã€æ‰‹æ•°æ–™20ドルãŒè¿½åŠ ã•れã¦ã„ã¾ã™ã€‚
- Checking Accountã®æ®‹é«˜ã¯0ドルã§ã™ã€‚
「016 抽象クラスã€å•題集リスト
🎯 実習ã§ç†è§£ã‚’æ·±ã‚よã†
ã“ã®å†…容ã«ã¤ã„ã¦JavaDrillã§å®Ÿéš›ã«æ‰‹ã‚’å‹•ã‹ã—ã¦å¦ç¿’ã§ãã¾ã™


コメント