문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:ㄴhomework [2022/02/03 15:23] clayeryan@gmail.com [과제 90] |
프로그램:java:ㄴhomework:ㄴhomework [2025/06/27 16:04] (현재) |
||
---|---|---|---|
줄 3: | 줄 3: | ||
[[https:// | [[https:// | ||
- | [[programmer: | + | [[프로그램: |
- | [[programmer: | + | [[프로그램: |
- | [[programmer: | + | [[프로그램: |
===== Hello World 출력 ===== | ===== Hello World 출력 ===== | ||
줄 8863: | 줄 8863: | ||
++++ | ++++ | ||
- | ==== 과제 91 ==== | + | ==== Banking Project (J2SE 버전, 품질1단계) |
- | ++++title| | + | |
+ | **예제** | ||
+ | |||
+ | ++++Account| | ||
<code java> | <code java> | ||
- | java code | + | package com.mybank.domain; |
+ | public interface Account { | ||
+ | public double getBalance() ; | ||
+ | public boolean deposit(double amt) ; | ||
+ | public boolean withdraw(double amt) ; | ||
+ | } | ||
</ | </ | ||
++++ | ++++ | ||
- | ==== 과제 92 ==== | + | |
++++title| | ++++title| | ||
<code java> | <code java> | ||
- | java code | + | package com.mybank.domain; |
+ | public class SavingsAccount implements Account { | ||
+ | protected double balance; | ||
+ | protected String name; | ||
+ | private double interestRate; | ||
+ | |||
+ | public SavingsAccount(double initBalance, | ||
+ | balance = initBalance; | ||
+ | this.interestRate = interestRate; | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | public boolean deposit(double amt) { | ||
+ | balance = balance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public boolean withdraw(double amt) { | ||
+ | | ||
+ | if ( amt <= balance ) { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++CheckingAccount| | ||
+ | < | ||
+ | package com.mybank.domain; | ||
+ | public class CheckingAccount implements Account { | ||
+ | protected double balance; | ||
+ | protected String name; | ||
+ | private double overdraftAmount = 0; | ||
+ | |||
+ | public CheckingAccount(double initBalance, | ||
+ | balance = initBalance; | ||
+ | this.overdraftAmount = overdraftAmount; | ||
+ | } | ||
+ | |||
+ | public CheckingAccount(double initBalance) { | ||
+ | this(initBalance, | ||
+ | } | ||
+ | |||
+ | public CheckingAccount(String name){ | ||
+ | balance = 0; | ||
+ | this.name = name; | ||
+ | } | ||
+ | |||
+ | | ||
+ | boolean result = true; | ||
+ | |||
+ | if ( balance < amount ) { | ||
+ | double overdraftNeeded = amount - balance; | ||
+ | if ( overdraftAmount < overdraftNeeded ) { | ||
+ | result = false; | ||
+ | } else { | ||
+ | balance = 0.0; | ||
+ | overdraftAmount -= overdraftNeeded; | ||
+ | } | ||
+ | } else { | ||
+ | balance = balance - amount; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | public double getBalance() { | ||
+ | return balance; | ||
+ | } | ||
+ | |||
+ | public boolean deposit(double amt) { | ||
+ | balance = balance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ++++ | ||
+ | |||
+ | ++++TestTypeSafety| | ||
+ | <code java> | ||
+ | import com.mybank.domain.*; | ||
+ | import java.util.*; | ||
+ | |||
+ | public class TestTypeSafety { | ||
+ | public static void main(String[] args) { | ||
+ | List< | ||
+ | |||
+ | lc.add(new CheckingAccount(" | ||
+ | lc.add(new SavingsAccount(" | ||
+ | |||
+ | // therefore... | ||
+ | CheckingAccount ca = lc.get(0); | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ++++TestAccount| | ||
+ | <code java> | ||
+ | public class TestAccount { | ||
+ | |||
+ | private double sBalance; | ||
+ | private String sName; | ||
+ | private double interestRate; | ||
+ | |||
+ | private double cBalance; | ||
+ | private String cName; | ||
+ | private double overdraftAmount = 0; | ||
+ | |||
+ | public boolean sDeposit(double amt) { | ||
+ | sBalance = sBalance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public boolean sWithdraw(double amt) { | ||
+ | boolean result = false; | ||
+ | if(amt <= sBalance) { | ||
+ | sBalance = sBalance - amt; | ||
+ | result = true; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | public boolean cDeposit(double amt) { | ||
+ | cBalance = cBalance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public boolean cWithdraw(double amount) { | ||
+ | boolean result = true; | ||
+ | |||
+ | if(cBalance < amount) { | ||
+ | double overdraftNeeded = amount - cBalance; | ||
+ | if(overdraftAmount < overdraftNeeded) { | ||
+ | result = false; | ||
+ | } else { | ||
+ | cBalance = 0.0; | ||
+ | overdraftAmount -= overdraftNeeded; | ||
+ | } | ||
+ | } else { | ||
+ | cBalance = cBalance - amount; | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | TestAccount ts = new TestAccount(); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ==== Banking Project (J2SE 버전, 품질2단계) ==== | ||
+ | |||
+ | ++++SavingsAccount| | ||
+ | <code java> | ||
+ | public class SavingsAccount { | ||
+ | private double balance; | ||
+ | private String name; | ||
+ | private double interestRate; | ||
+ | |||
+ | public SavingsAccount(double initBalance, | ||
+ | balance = initBalance; | ||
+ | this.interestRate = interestRate; | ||
+ | } | ||
+ | |||
+ | public SavingsAccount(String name) { | ||
+ | balance = 0; | ||
+ | this.name = name; | ||
+ | this.interestRate = 5.0; | ||
+ | } | ||
+ | |||
+ | public double getBalance() { | ||
+ | return balance; | ||
+ | } | ||
+ | |||
+ | public boolean deposit(double amt) { | ||
+ | balance = balance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public boolean withdraw(double amt) { | ||
+ | boolean result = false; | ||
+ | if(amt <= balance) { | ||
+ | balance = balance - amt; | ||
+ | result = true; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++CheckingAccount| | ||
+ | <code java> | ||
+ | public class CheckingAccount { | ||
+ | private double balance; | ||
+ | private String name; | ||
+ | private double overdraftAmount = 0; | ||
+ | |||
+ | public CheckingAccount(double initBalance, | ||
+ | balance = initBalance; | ||
+ | this.overdraftAmount = overdraftAmount; | ||
+ | } | ||
+ | |||
+ | public CheckingAccount(double initBalance) { | ||
+ | this(initBalance, | ||
+ | } | ||
+ | |||
+ | public CheckingAccount(String name) { | ||
+ | balance = 0; | ||
+ | this.name = name; | ||
+ | } | ||
+ | |||
+ | public double getBalance() { | ||
+ | return balance; | ||
+ | } | ||
+ | |||
+ | public boolean deposit(double amt) { | ||
+ | balance = balance + amt; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public boolean withdraw(double amount) { | ||
+ | boolean result = true; | ||
+ | |||
+ | if(balance < amount) { | ||
+ | double overdraftNeeded = amount - balance; | ||
+ | if(overdraftAmount < overdraftNeeded) { | ||
+ | result = false; | ||
+ | } else { | ||
+ | balance = 0.0; | ||
+ | overdraftAmount -= overdraftNeeded; | ||
+ | } | ||
+ | } else { | ||
+ | balance = balance - amount; | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++TestSafety| | ||
+ | <code java> | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class TestSafety { | ||
+ | public static void main(String[] args) { | ||
+ | List< | ||
+ | List< | ||
+ | |||
+ | lc.add(new CheckingAccount(" | ||
+ | ls.add(new SavingsAccount(" | ||
+ | |||
+ | CheckingAccount ca = lc.get(0); | ||
+ | SavingsAccount sa = ls.get(0); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
</ | </ | ||
++++ | ++++ |