문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
프로그램:java:ㄴhomework:ㄴhomework [2022/02/02 23:07] clayeryan@gmail.com [함수적 인터페이스] |
프로그램:java:ㄴhomework:ㄴhomework [2025/06/27 16:04] (현재) |
||
|---|---|---|---|
| 줄 3: | 줄 3: | ||
| [[https:// | [[https:// | ||
| - | [[programmer: | + | [[프로그램: |
| - | [[programmer: | + | [[프로그램: |
| - | [[programmer: | + | [[프로그램: |
| ===== Hello World 출력 ===== | ===== Hello World 출력 ===== | ||
| 줄 7223: | 줄 7223: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ====함수적 인터페이스 | + | ====함수적 인터페이스 |
| ++++LambdaEx4| | ++++LambdaEx4| | ||
| 줄 7261: | 줄 7261: | ||
| ++++ | ++++ | ||
| - | ==== 과제 81 ==== | + | ==== 함수적 인터페이스 5 ==== |
| ++++LambdaEx5| | ++++LambdaEx5| | ||
| <code java> | <code java> | ||
| 줄 7292: | 줄 7292: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 82 ==== | + | ==== Banking Project 버전 1 ==== |
| ++++Account| | ++++Account| | ||
| <code java> | <code java> | ||
| 줄 7408: | 줄 7408: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 83 ==== | + | ==== Banking Project 버전 2 ==== |
| - | Banking Project 버전2 | + | |
| ++++Account| | ++++Account| | ||
| 줄 7552: | 줄 7551: | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ---- |
| + | |||
| + | ++++Account| | ||
| <code java> | <code java> | ||
| - | java code | + | interface Account { |
| + | public double getBalance(); | ||
| + | public boolean deposit(double amt); | ||
| + | public boolean withdraw(double amt); | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ++++CheckingAccount |
| <code java> | <code java> | ||
| - | java code | + | 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; | ||
| + | } | ||
| + | |||
| + | 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; | ||
| + | } | ||
| + | |||
| + | public double getBalance() { | ||
| + | return balance; | ||
| + | } | ||
| + | |||
| + | public boolean deposit(double amt) { | ||
| + | balance = balance + amt; | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ++++SavingsAccount| |
| <code java> | <code java> | ||
| - | java code | + | public class SavingsAccount implements Account { |
| + | protected double balance; | ||
| + | protected String name; | ||
| + | private double interestRate; | ||
| + | |||
| + | protected 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; // assume operation failure | ||
| + | if(amt <= balance) { | ||
| + | balance = balance - amt; | ||
| + | result = true; // operation succeds | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ++++TestSafety| |
| <code java> | <code java> | ||
| - | java code | + | import |
| + | |||
| + | public class TestSafety { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | List< | ||
| + | |||
| + | lc.add(new CheckingAccount(" | ||
| + | // | ||
| + | |||
| + | // therefore... | ||
| + | CheckingAccount ca = lc.get(0); | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ==== Banking Project 버전 3 ==== |
| + | |||
| + | ++++Account| | ||
| <code java> | <code java> | ||
| - | java code | + | |
| + | |||
| + | public interface Account { | ||
| + | |||
| + | public double getBalance() ; | ||
| + | |||
| + | public boolean deposit(double amt) ; | ||
| + | |||
| + | public boolean withdraw(double amt) ; | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ++++title| | + | ++++SavingsAccount| |
| <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 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; // operation succeeds | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| + | ++++CheckingAccount| | ||
| + | <code java> | ||
| + | package com.mybank.domain; | ||
| - | ==== 과제 84 ==== | + | public class CheckingAccount implements Account { |
| - | ++++title| | + | protected double balance; |
| + | protected String name; | ||
| + | private double overdraftAmount | ||
| + | |||
| + | public CheckingAccount(double initBalance, | ||
| + | balance | ||
| + | this.overdraftAmount | ||
| + | } | ||
| + | |||
| + | public CheckingAccount(double initBalance) { | ||
| + | this(initBalance, | ||
| + | } | ||
| + | |||
| + | public CheckingAccount(String name){ | ||
| + | balance | ||
| + | this.name | ||
| + | } | ||
| + | |||
| + | public boolean withdraw(double amount) { | ||
| + | boolean result | ||
| + | |||
| + | if ( balance < amount ) { | ||
| + | double overdraftNeeded | ||
| + | if ( overdraftAmount < overdraftNeeded ) { | ||
| + | result | ||
| + | } 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; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++TestTypeSafety| | ||
| <code java> | <code java> | ||
| - | java code | + | import com.mybank.domain.*; |
| + | import | ||
| + | |||
| + | 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(" | ||
| + | } | ||
| + | } | ||
| + | | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 85 ==== | + | |
| - | ++++title| | + | ---- |
| + | |||
| + | ++++Account| | ||
| <code java> | <code java> | ||
| - | java code | + | interface Account { |
| + | public double getBalance(); | ||
| + | public boolean deposit(double amt); | ||
| + | public boolean withdraw(double amt); | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 86 ==== | + | |
| - | ++++title| | + | ++++CheckingAccount| |
| <code java> | <code java> | ||
| - | java code | + | 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; | ||
| + | } | ||
| + | |||
| + | 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; | ||
| + | } | ||
| + | |||
| + | public double getBalance() { | ||
| + | return balance; | ||
| + | } | ||
| + | |||
| + | public boolean deposit(double amt) { | ||
| + | balance = balance + amt; | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 87 ==== | + | |
| - | ++++title| | + | |
| + | ++++SavingsAccount| | ||
| <code java> | <code java> | ||
| - | java code | + | public class SavingsAccount implements Account { |
| + | protected double balance; | ||
| + | protected String name; | ||
| + | private double interestRate; | ||
| + | |||
| + | protected 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; // assume operation failure | ||
| + | if(amt <= balance) { | ||
| + | balance = balance - amt; | ||
| + | result = true; // operation succeds | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 88 ==== | + | |
| - | ++++title| | + | |
| + | ++++TestSafety| | ||
| <code java> | <code java> | ||
| - | java code | + | import |
| + | |||
| + | public class TestSafety { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | List< | ||
| + | |||
| + | lc.add(new CheckingAccount(" | ||
| + | // | ||
| + | |||
| + | // therefore... | ||
| + | CheckingAccount ca = lc.get(0); | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 89 ==== | + | |
| + | ==== Banking Project | ||
| ++++title| | ++++title| | ||
| <code java> | <code java> | ||
| 줄 7625: | 줄 7953: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 90 ==== | + | ==== Banking Project (Class Diagram) |
| - | ++++title| | + | |
| + | {{: | ||
| + | ==== 계산기 프로젝트 (J2SE 버전, 품질10단계, | ||
| + | |||
| + | ++++ICal| | ||
| <code java> | <code java> | ||
| - | java code | + | interface ICal { |
| + | public void doService() throws ArithmeticException, | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 91 ==== | + | |
| - | ++++title| | + | ++++CalImpl| |
| <code java> | <code java> | ||
| - | java code | + | public class CalImpl implements ICal { |
| + | private CalVO vo; | ||
| + | |||
| + | CalImpl(CalVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void doService() throws ArithmeticException, | ||
| + | AddZeroException, | ||
| + | |||
| + | int op1 = Integer.parseInt(vo.getOp1()); | ||
| + | String op = vo.getOp(); | ||
| + | int op2 = Integer.parseInt(vo.getOp2()); | ||
| + | |||
| + | if(op.equals(CalVO.ADD)) { | ||
| + | if(op1 == 0 && op2 == 0) { | ||
| + | throw new AddZeroException(" | ||
| + | } else if(op1 == 0) { | ||
| + | throw new AddZeroException(" | ||
| + | } else if(op2 == 0) { | ||
| + | throw new AddZeroException(" | ||
| + | } | ||
| + | } else if(op.equals(CalVO.SUB)) { | ||
| + | if(op1 == 0 && op2 == 0) { | ||
| + | throw new SubZeroException(" | ||
| + | } else if(op2 == 0) { | ||
| + | throw new SubZeroException(" | ||
| + | } | ||
| + | } else if(op.equals(CalVO.MUL)) { | ||
| + | if(op1 == 1 && op2 == 1) { | ||
| + | throw new MulOneException(" | ||
| + | } else if(op1 == 1) { | ||
| + | throw new MulOneException(" | ||
| + | } else if(op2 == 1) { | ||
| + | throw new MulOneException(" | ||
| + | } | ||
| + | } else if(op.equals(CalVO.DIV)) { | ||
| + | if(op1 == 1 && op2 == 1) { | ||
| + | throw new DivOneException("/ | ||
| + | } else if(op2 == 1) { | ||
| + | throw new DivOneException("/ | ||
| + | } | ||
| + | } | ||
| + | CalThread cct = new CalThread(vo); | ||
| + | cct.start(); | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 92 ==== | + | |
| + | ++++CalThread | | ||
| + | <code java> | ||
| + | public class CalThread extends Thread { | ||
| + | private CalVO vo; | ||
| + | |||
| + | CalThread(CalVO vo){ | ||
| + | this.vo | ||
| + | } | ||
| + | |||
| + | public void run() { | ||
| + | try { | ||
| + | int op1 = Integer.parseInt(vo.getOp1()); | ||
| + | String op = vo.getOp(); | ||
| + | int op2 = Integer.parseInt(vo.getOp2()); | ||
| + | |||
| + | if(op.equals(CalVO.ADD)) {vo.setResult(op1 + op2);} | ||
| + | else if(op.equals(CalVO.SUB)) {vo.setResult(op1 - op2);} | ||
| + | else if(op.equals(CalVO.MUL)) {vo.setResult(op1 * op2);} | ||
| + | else if(op.equals(CalVO.DIV)) {vo.setResult(op1 / op2);} | ||
| + | |||
| + | System.out.println(vo.getResult()); | ||
| + | |||
| + | } catch(ArithmeticException e) { | ||
| + | System.out.println(" | ||
| + | } catch(NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++CalVO| | ||
| + | <code java> | ||
| + | public class CalVO { | ||
| + | public static final String ADD = " | ||
| + | public static final String SUB = " | ||
| + | public static final String MUL = " | ||
| + | public static final String DIV = " | ||
| + | private String op1; | ||
| + | private String op; | ||
| + | private String op2; | ||
| + | private int result; | ||
| + | |||
| + | CalVO(String op1, String op, String op2) { | ||
| + | this.op1 = op1; | ||
| + | this.op = op; | ||
| + | this.op2 = op2; | ||
| + | } | ||
| + | |||
| + | public String getOp1() { | ||
| + | return op1; | ||
| + | } | ||
| + | public void setOp1(String op1) { | ||
| + | this.op1 = op1; | ||
| + | } | ||
| + | |||
| + | public String getOp() { | ||
| + | return op; | ||
| + | } | ||
| + | public void setOp(String op) { | ||
| + | this.op = op; | ||
| + | } | ||
| + | |||
| + | public String getOp2() { | ||
| + | return op2; | ||
| + | } | ||
| + | public void setOp2(String op2) { | ||
| + | this.op2 = op2; | ||
| + | } | ||
| + | |||
| + | public int getResult() { | ||
| + | return result; | ||
| + | } | ||
| + | public void setResult(int result) { | ||
| + | this.result = result; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++Cal| | ||
| + | <code java> | ||
| + | public class Cal { | ||
| + | private ICal ic; | ||
| + | |||
| + | Cal(CalVO vo){ | ||
| + | ic = new CalImpl(vo); | ||
| + | } | ||
| + | |||
| + | void doService() { | ||
| + | try { | ||
| + | ic.doService(); | ||
| + | } catch (AddZeroException e) { | ||
| + | System.out.println(e.getMessage()+", | ||
| + | } catch (SubZeroException e) { | ||
| + | System.out.println(e.getMessage()+", | ||
| + | } catch (MulOneException e) { | ||
| + | System.out.println(e.getMessage()+", | ||
| + | } catch (DivOneException e) { | ||
| + | System.out.println(e.getMessage()+", | ||
| + | } catch (ArithmeticException e) { | ||
| + | System.out.println(" | ||
| + | } catch (NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } catch (Exception e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++TestCal| | ||
| + | <code java> | ||
| + | public class TestCal { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | // 입력 | ||
| + | // 처리 | ||
| + | CalVO vo = new CalVO(args[0], | ||
| + | Cal c = new Cal(vo); | ||
| + | |||
| + | // 출력 | ||
| + | c.doService(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++User Defined Exception| | ||
| + | <code java> | ||
| + | public class AddZeroException extends Exception { | ||
| + | private int result; | ||
| + | |||
| + | AddZeroException(String msg, int result){ | ||
| + | super(msg); | ||
| + | this.result = result; | ||
| + | } | ||
| + | |||
| + | public int getResult() { | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++SubZeroException| | ||
| + | <code java> | ||
| + | public class SubZeroException extends Exception { | ||
| + | private int result; | ||
| + | |||
| + | SubZeroException(String msg, int result){ | ||
| + | super(msg); | ||
| + | this.result = result; | ||
| + | } | ||
| + | |||
| + | public int getResult() { | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++MulOneException| | ||
| + | <code java> | ||
| + | public class MulOneException extends Exception | ||
| + | private int result; | ||
| + | |||
| + | MulOneException(String msg, int result){ | ||
| + | super(msg); | ||
| + | this.result = result; | ||
| + | } | ||
| + | |||
| + | public int getResult() { | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++DivOneException| | ||
| + | <code java> | ||
| + | public class DivOneException extends Exception | ||
| + | private int result; | ||
| + | |||
| + | DivOneException(String msg, int result){ | ||
| + | super(msg); | ||
| + | this.result = result; | ||
| + | } | ||
| + | |||
| + | public int getResult() { | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== 성적처리 프로젝트 (J2SE 버전, 품질10단계, | ||
| + | |||
| + | ++++IScore| | ||
| + | <code java> | ||
| + | interface IScore { | ||
| + | void doService(); | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ScoreImpl| | ||
| + | <code java> | ||
| + | public class ScoreImpl implements IScore{ | ||
| + | private ScoreVO vo; | ||
| + | |||
| + | ScoreImpl(ScoreVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | public void doService() { | ||
| + | ScoreThread st = new ScoreThread(vo); | ||
| + | st.start(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ScoreThread| | ||
| + | <code java> | ||
| + | public class ScoreThread extends Thread { | ||
| + | private ScoreVO vo; | ||
| + | private String[][] score; | ||
| + | |||
| + | ScoreThread(ScoreVO vo){ | ||
| + | this.vo = vo; | ||
| + | score = vo.getScore(); | ||
| + | } | ||
| + | |||
| + | public void run() { | ||
| + | try { | ||
| + | sum(); | ||
| + | avg(); | ||
| + | rank(); | ||
| + | total(); | ||
| + | print(); | ||
| + | } catch(ScoreSizeOutOfBoundException e) { | ||
| + | System.out.println(" | ||
| + | } catch(ArrayIndexOutOfBoundsException e) { | ||
| + | System.out.println(" | ||
| + | } catch(NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } catch(NullPointerException e) { | ||
| + | System.out.println(" | ||
| + | } catch(Exception e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private void sum() throws ScoreSizeOutOfBoundException, | ||
| + | for(int i = 0; i < score.length-1; | ||
| + | int num1 = Integer.parseInt(score[i][1]); | ||
| + | int num2 = Integer.parseInt(score[i][2]); | ||
| + | int num3 = Integer.parseInt(score[i][3]); | ||
| + | if(num1 < ScoreVO.MIN_SCORE || num2 < ScoreVO.MIN_SCORE || num3 < ScoreVO.MIN_SCORE) { | ||
| + | throw new ScoreSizeOutOfBoundException(" | ||
| + | } else if(num1 > ScoreVO.MAX_SCORE || num2 > ScoreVO.MAX_SCORE || num3 > ScoreVO.MAX_SCORE) { | ||
| + | throw new ScoreSizeOutOfBoundException(" | ||
| + | } | ||
| + | int sum = num1 + num2 + num3; | ||
| + | score[i][4] = String.valueOf(sum); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private void avg() throws ArrayIndexOutOfBoundsException, | ||
| + | for(int i = 0; i < score.length-1; | ||
| + | double avg = (double)Integer.parseInt(score[i][4]) / 3; | ||
| + | avg = ((int)((avg*10) +0.5) / 10d); | ||
| + | score[i][5] = String.valueOf(avg); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private void rank() throws ArrayIndexOutOfBoundsException, | ||
| + | for(int i = 0; i < score.length-1; | ||
| + | int rank = score.length-1; | ||
| + | for(int j = 0; j < score.length-1; | ||
| + | if(i != j && Integer.parseInt(score[j][4]) <= Integer.parseInt(score[i][4])) { | ||
| + | rank--; | ||
| + | } | ||
| + | } | ||
| + | score[i][6] = String.valueOf(rank); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private void total() throws ArrayIndexOutOfBoundsException, | ||
| + | int totalSum = 0; | ||
| + | int sum1 = 0; | ||
| + | int sum2 = 0; | ||
| + | int sum3 = 0; | ||
| + | for(int i = 0; i < score.length-1; | ||
| + | sum1 += Integer.parseInt(score[i][1]); | ||
| + | sum2 += Integer.parseInt(score[i][2]); | ||
| + | sum3 += Integer.parseInt(score[i][3]); | ||
| + | } | ||
| + | totalSum = sum1 + sum2 + sum3; | ||
| + | score[score.length-1][1] = String.valueOf(sum1); | ||
| + | score[score.length-1][2] = String.valueOf(sum2); | ||
| + | score[score.length-1][3] = String.valueOf(sum3); | ||
| + | score[score.length-1][4] = String.valueOf(totalSum); | ||
| + | } | ||
| + | |||
| + | private void print() throws ArrayIndexOutOfBoundsException, | ||
| + | System.out.println(ScoreVO.LINE); | ||
| + | System.out.println(ScoreVO.TITLE); | ||
| + | System.out.println(ScoreVO.LINE); | ||
| + | for(int i = 0; i < score.length; | ||
| + | if(i == score.length-1) {System.out.println(ScoreVO.LINE); | ||
| + | for(int j = 0; j < score[i].length; | ||
| + | System.out.printf(" | ||
| + | } | ||
| + | System.out.println(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ScoreVO| | ||
| + | <code java> | ||
| + | public class ScoreVO { | ||
| + | public final static int MAX_SCORE = 100; | ||
| + | public final static int MIN_SCORE = 0; | ||
| + | public final static String LINE = " | ||
| + | public final static String TITLE = " 이름 | ||
| + | private String[][] score; | ||
| + | |||
| + | ScoreVO(String[][] score){ | ||
| + | this.score = score; | ||
| + | } | ||
| + | |||
| + | public String[][] getScore() { | ||
| + | return score; | ||
| + | } | ||
| + | |||
| + | public void setScore(String[][] score) { | ||
| + | this.score = score; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++Score| | ||
| + | <code java> | ||
| + | public class Score { | ||
| + | private IScore is; | ||
| + | |||
| + | Score(ScoreVO vo){ | ||
| + | is = new ScoreImpl(vo); | ||
| + | } | ||
| + | |||
| + | void doService() { | ||
| + | is.doService(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++TestScore| | ||
| + | <code java> | ||
| + | public class TestScore { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | // 1. 입력 | ||
| + | String[][] score = {{" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | }; | ||
| + | // 2. 처리 | ||
| + | ScoreVO vo = new ScoreVO(score); | ||
| + | Score s = new Score(vo); | ||
| + | // 3. 결과 | ||
| + | s.doService(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++User Defined Exception| | ||
| + | <code java> | ||
| + | public class ScoreSizeOutOfBoundException extends Exception { | ||
| + | ScoreSizeOutOfBoundException(String msg){ | ||
| + | super(msg); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== 엘리베이터 프로젝트 (J2SE 버전, 품질10단계, | ||
| + | |||
| + | ++++IElevator| | ||
| + | <code java> | ||
| + | interface IElevator { | ||
| + | public void openDoor(); | ||
| + | public void closeDoor(); | ||
| + | public void goUp() throws CannotGoUpException; | ||
| + | public void goDown() throws CannotGoDownException; | ||
| + | public void setFloor(String desiredFloor) throws ArrayIndexOutOfBoundsException, | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ElevatorImpl| | ||
| + | <code java> | ||
| + | public class ElevatorImpl implements IElevator { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | ElevatorImpl(ElevatorVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | public void openDoor() { | ||
| + | OpenDoorThread od = new OpenDoorThread(vo); | ||
| + | od.start(); | ||
| + | try { | ||
| + | od.join(); | ||
| + | } catch (InterruptedException e) {} | ||
| + | } | ||
| + | public void closeDoor() { | ||
| + | CloseDoorThread cd = new CloseDoorThread(vo); | ||
| + | cd.start(); | ||
| + | try { | ||
| + | cd.join(); | ||
| + | } catch (InterruptedException e) {} | ||
| + | } | ||
| + | public void goUp() throws CannotGoUpException { | ||
| + | if(vo.getCurrentFloor() == vo.getTOP_FLOOR()) { | ||
| + | throw new CannotGoUpException(" | ||
| + | } else { | ||
| + | GoUpThread gu = new GoUpThread(vo); | ||
| + | gu.start(); | ||
| + | try { | ||
| + | gu.join(); | ||
| + | } catch (InterruptedException e) {} | ||
| + | } | ||
| + | } | ||
| + | public void goDown() throws CannotGoDownException { | ||
| + | if(vo.getCurrentFloor() == vo.getMIN_FLOORS()) { | ||
| + | throw new CannotGoDownException(" | ||
| + | } else { | ||
| + | GoDownThread gd = new GoDownThread(vo); | ||
| + | gd.start(); | ||
| + | try { | ||
| + | gd.join(); | ||
| + | } catch (InterruptedException e) {} | ||
| + | } | ||
| + | } | ||
| + | public void setFloor(String PdesiredFloor) throws NumberFormatException, | ||
| + | vo.setDesiredFloor(Integer.parseInt(PdesiredFloor)); | ||
| + | while(vo.getCurrentFloor() != vo.getDesiredFloor()) { | ||
| + | if(vo.getCurrentFloor() < vo.getDesiredFloor()) { | ||
| + | goUp(); | ||
| + | } else { | ||
| + | goDown(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | **Thread inheritance class** | ||
| + | |||
| + | ++++CloseDoorThread| | ||
| + | <code java> | ||
| + | public class CloseDoorThread extends Thread { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | CloseDoorThread(ElevatorVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | System.out.println(" | ||
| + | vo.setDoorOpen(false); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++OpenDoorThread| | ||
| + | <code java> | ||
| + | public class OpenDoorThread extends Thread { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | OpenDoorThread(ElevatorVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | System.out.println(" | ||
| + | vo.setDoorOpen(true); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++GoUpThread| | ||
| + | <code java> | ||
| + | public class GoUpThread extends Thread { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | GoUpThread(ElevatorVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | System.out.println(" | ||
| + | vo.setCurrentFloor(vo.getCurrentFloor()+1); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++GoDownThread | | ||
| + | <code java> | ||
| + | public class GoDownThread extends Thread { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | GoDownThread(ElevatorVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | System.out.println(" | ||
| + | vo.setCurrentFloor(vo.getCurrentFloor()-1); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ElevatorVO| | ||
| + | <code java> | ||
| + | public class ElevatorVO { | ||
| + | private final static int TOP_FLOOR = 10; | ||
| + | private final static int MIN_FLOORS = 1; | ||
| + | private int currentFloor = 1; | ||
| + | private boolean doorOpen = false; | ||
| + | private int desiredFloor; | ||
| + | |||
| + | public boolean isDoorOpen() { | ||
| + | return doorOpen; | ||
| + | } | ||
| + | public void setDoorOpen(boolean doorOpen) { | ||
| + | this.doorOpen = doorOpen; | ||
| + | } | ||
| + | public int getCurrentFloor() { | ||
| + | return currentFloor; | ||
| + | } | ||
| + | public void setCurrentFloor(int currentFloor) { | ||
| + | this.currentFloor = currentFloor; | ||
| + | } | ||
| + | public int getTOP_FLOOR() { | ||
| + | return TOP_FLOOR; | ||
| + | } | ||
| + | public int getMIN_FLOORS() { | ||
| + | return MIN_FLOORS; | ||
| + | } | ||
| + | public int getDesiredFloor() { | ||
| + | return desiredFloor; | ||
| + | } | ||
| + | public void setDesiredFloor(int desiredFloor) { | ||
| + | this.desiredFloor = desiredFloor; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++Elevator| | ||
| + | <code java> | ||
| + | public class Elevator { | ||
| + | private IElevator ie; | ||
| + | |||
| + | Elevator(ElevatorVO vo){ | ||
| + | ie = new ElevatorImpl(vo); | ||
| + | } | ||
| + | public void openDoor() { | ||
| + | ie.openDoor(); | ||
| + | } | ||
| + | public void closeDoor() { | ||
| + | ie.closeDoor(); | ||
| + | } | ||
| + | public void goUp() { | ||
| + | try { | ||
| + | ie.goUp(); | ||
| + | } catch(CannotGoUpException e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | public void goDown() { | ||
| + | try { | ||
| + | ie.goDown(); | ||
| + | } catch(CannotGoDownException e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | public void setFloor(String desiredFloor) { | ||
| + | try { | ||
| + | ie.setFloor(desiredFloor); | ||
| + | } catch(NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } catch(CannotGoUpException e) { | ||
| + | System.out.println(" | ||
| + | } catch(CannotGoDownException e) { | ||
| + | System.out.println(" | ||
| + | } catch(Exception e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ElevatorTest| | ||
| + | <code java> | ||
| + | public class ElevatorTest{ | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | ElevatorVO vo = new ElevatorVO(); | ||
| + | Elevator myElevator = new Elevator(vo); | ||
| + | String desiredFloor = args[0]; | ||
| + | |||
| + | myElevator.openDoor(); | ||
| + | myElevator.closeDoor(); | ||
| + | myElevator.goUp(); | ||
| + | myElevator.goUp(); | ||
| + | myElevator.goUp(); | ||
| + | myElevator.goDown(); | ||
| + | myElevator.setFloor(desiredFloor); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | **User Defined Exception** | ||
| + | |||
| + | ++++CannotGoUpException| | ||
| + | <code java> | ||
| + | public class CannotGoUpException extends Exception { | ||
| + | CannotGoUpException(String msg){ | ||
| + | super(msg); | ||
| + | } | ||
| + | } | ||
| + | public class CannotGoDownException extends Exception { | ||
| + | CannotGoDownException(String msg){ | ||
| + | super(msg); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | ==== FooBarBaz 프로젝트 (J2SE 버전, 품질10단계, | ||
| + | |||
| + | |||
| + | ++++IFooBarBaz| | ||
| + | <code java> | ||
| + | interface IFooBarBaz { | ||
| + | void printFooBarBaz() throws NumberFormatException; | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++FooBarBazImpl| | ||
| + | <code java> | ||
| + | public class FooBarBazImpl implements IFooBarBaz { | ||
| + | FooBarBazVO vo; | ||
| + | |||
| + | FooBarBazImpl(FooBarBazVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void printFooBarBaz() throws NumberFormatException { | ||
| + | int start = Integer.parseInt(vo.getStart()); | ||
| + | int end = Integer.parseInt(vo.getEnd()); | ||
| + | |||
| + | FooBarBazThread fbbt = new FooBarBazThread(vo); | ||
| + | fbbt.start(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++FooBarBazThread| | ||
| + | <code java> | ||
| + | public class FooBarBazThread extends Thread { | ||
| + | FooBarBazVO vo; | ||
| + | |||
| + | FooBarBazThread(FooBarBazVO vo){ | ||
| + | this.vo = vo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void run() { | ||
| + | try{ | ||
| + | int start = Integer.parseInt(vo.getStart()); | ||
| + | int end = Integer.parseInt(vo.getEnd()); | ||
| + | |||
| + | for(int i = start; i < end; i++) { | ||
| + | System.out.print(i); | ||
| + | if(i % FooBarBazVO.THREE == 0) { | ||
| + | System.out.print(" | ||
| + | } | ||
| + | if(i % FooBarBazVO.FIVE == 0) { | ||
| + | System.out.print(" | ||
| + | } | ||
| + | if(i % FooBarBazVO.SEVEN == 0) { | ||
| + | System.out.print(" | ||
| + | } | ||
| + | System.out.println(); | ||
| + | if((i % FooBarBazVO.THREE == 0) && (i % FooBarBazVO.FIVE == 0) && (i % FooBarBazVO.SEVEN == 0)) { | ||
| + | throw new FooBarBazException(" | ||
| + | } | ||
| + | } | ||
| + | System.out.println(); | ||
| + | System.out.print(" | ||
| + | } catch(NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } catch(FooBarBazException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } catch(Exception e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++FooBarBazVO| | ||
| + | <code java> | ||
| + | public class FooBarBazVO { | ||
| + | public final static String FOO = " | ||
| + | public final static String BAR = " | ||
| + | public final static String BAZ = " | ||
| + | public final static String FOO_BAR_BAZ = "foo bar baz"; | ||
| + | public final static int THREE = 3; | ||
| + | public final static int FIVE = 5; | ||
| + | public final static int SEVEN = 7; | ||
| + | private String start; | ||
| + | private String end; | ||
| + | |||
| + | public String getStart() { | ||
| + | return start; | ||
| + | } | ||
| + | public void setStart(String start) { | ||
| + | this.start = start; | ||
| + | } | ||
| + | public String getEnd() { | ||
| + | return end; | ||
| + | } | ||
| + | public void setEnd(String end) { | ||
| + | this.end = end; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | |||
| + | ++++FooBarBaz| | ||
| + | <code java> | ||
| + | public class FooBarBaz { | ||
| + | IFooBarBaz ifbb; | ||
| + | |||
| + | FooBarBaz(FooBarBazVO vo){ | ||
| + | ifbb = new FooBarBazImpl(vo); | ||
| + | } | ||
| + | |||
| + | void printFooBarBaz() { | ||
| + | try { | ||
| + | ifbb.printFooBarBaz(); | ||
| + | } catch(NumberFormatException e) { | ||
| + | System.out.println(" | ||
| + | } catch(Exception e) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | TestFooBarBaz | ||
| + | |||
| + | public class TestFooBarBaz { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | FooBarBazVO vo = new FooBarBazVO(); | ||
| + | vo.setStart(" | ||
| + | vo.setEnd(" | ||
| + | FooBarBaz fbb = new FooBarBaz(vo); | ||
| + | fbb.printFooBarBaz(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | **User Defined Exception** | ||
| + | |||
| + | ++++FooBarBazException| | ||
| + | <code java> | ||
| + | public class FooBarBazException extends Exception { | ||
| + | FooBarBazException(String msg){ | ||
| + | super(msg); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== Banking Project (J2SE 버전, 품질1단계) ==== | ||
| + | |||
| + | **예제** | ||
| + | |||
| + | ++++Account| | ||
| + | <code java> | ||
| + | package com.mybank.domain; | ||
| + | public interface Account { | ||
| + | public double getBalance() ; | ||
| + | public boolean deposit(double amt) ; | ||
| + | public boolean withdraw(double amt) ; | ||
| + | } | ||
| + | </ | ||
| + | ++++ | ||
| ++++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(" | ||
| + | } | ||
| + | |||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
별도로 명시하지 않을 경우, 이 위키의 내용은 다음 라이선스에 따라 사용할 수 있습니다: CC Attribution-Share Alike 4.0 International