nme.kr

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
프로그램:java:ㄴhomework:ㄴhomework [2022/02/03 01:55]
clayeryan@gmail.com [과제 87]
프로그램:java:ㄴhomework:ㄴhomework [2023/07/13 17:29] (현재)
clayeryan@gmail.com ↷ 링크가 이동 작업으로 인해 적응했습니다
줄 3: 줄 3:
 [[https://hashcode.co.kr/code_runners?language=java|자바 웹 컴파일러]] [[https://hashcode.co.kr/code_runners?language=java|자바 웹 컴파일러]]
  
-[[programmer:java:ㄴhomework:quiz|ㄴ과제 퀴즈]]+[[프로그램:java:ㄴhomework:quiz|ㄴ과제 퀴즈]]
  
-[[programmer:java:ㄴhomework:doc|ㄴ개념정리:목차]]+[[프로그램:java:ㄴhomework:doc|ㄴ개념정리:목차]]
  
-[[programmer:java:ㄴhomework:book_source|ㄴ북소스]]+[[프로그램:java:ㄴhomework:book_source|ㄴ북소스]]
 ===== Hello World 출력 ===== ===== Hello World 출력 =====
  
줄 8222: 줄 8222:
 ++++ ++++
  
-==== 과제 88 ==== +==== 성적처리 프로젝트 (J2SE 버전, 품질10단계, 멀티쓰레드) ==== 
-++++title|+ 
 +++++IScore|
 <code java> <code java>
-java code+interface IScore { 
 + void doService(); 
 +}
 </code> </code>
 ++++ ++++
-==== 과제 89 ==== + 
-++++title|+++++ScoreImpl|
 <code java> <code java>
-java code+public class ScoreImpl implements IScore{ 
 + private ScoreVO vo; 
 +  
 + ScoreImpl(ScoreVO vo){ 
 + this.vo = vo; 
 +
 +  
 + public void doService() { 
 + ScoreThread st = new ScoreThread(vo); 
 + st.start(); 
 +
 +  
 +}
 </code> </code>
 ++++ ++++
-==== 과제 90 ==== + 
-++++title|+++++ScoreThread|
 <code java> <code java>
-java code+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("ScoreSizeOutOfBoundException : "+e.getMessage()); 
 + } catch(ArrayIndexOutOfBoundsException e) { 
 + System.out.println("ArrayIndexOutOfBoundsException!!"); 
 + } catch(NumberFormatException e) { 
 + System.out.println("NumberFormatException!!"); 
 + } catch(NullPointerException e) { 
 + System.out.println("NullPointerException!!"); 
 + } catch(Exception e) { 
 + System.out.println("알 수 없는 예외 발생!"); 
 +
 +
 + 
 + private void sum() throws ScoreSizeOutOfBoundException, ArrayIndexOutOfBoundsException, NumberFormatException { 
 + for(int i = 0; i < score.length-1; i++) { 
 + 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, NumberFormatException { 
 + for(int i = 0; i < score.length-1; i++) { 
 + 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, NumberFormatException { 
 + for(int i = 0; i < score.length-1; i++) { 
 + int rank = score.length-1; 
 + for(int j = 0; j < score.length-1; j++) { 
 + 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, NumberFormatException { 
 + int totalSum = 0; 
 + int sum1 = 0; 
 + int sum2 = 0; 
 + int sum3 = 0; 
 + for(int i = 0; i < score.length-1; i++) { 
 + 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, NullPointerException { 
 + System.out.println(ScoreVO.LINE); 
 + System.out.println(ScoreVO.TITLE); 
 + System.out.println(ScoreVO.LINE); 
 + for(int i = 0; i < score.length; i++) { 
 + if(i == score.length-1) {System.out.println(ScoreVO.LINE);
 + for(int j = 0; j < score[i].length; j++) { 
 + System.out.printf("%-4s   ", score[i][j]); 
 +
 + System.out.println(); 
 +
 +
 +}
 </code> </code>
 ++++ ++++
-==== 과제 91 ==== + 
-++++title|+++++ScoreVO|
 <code java> <code java>
-java code+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 = " 이름    C#     JAVA   HTML5   합     평균   석차"; 
 + private String[][] score; 
 + 
 + ScoreVO(String[][] score){ 
 + this.score = score; 
 +
 +  
 + public String[][] getScore() { 
 + return score; 
 +
 + 
 + public void setScore(String[][] score) { 
 + this.score = score; 
 +
 +  
 +}
 </code> </code>
 ++++ ++++
-==== 과제 92 ====+ 
 +++++Score| 
 +<code java> 
 +public class Score { 
 + private IScore is; 
 +  
 + Score(ScoreVO vo){ 
 + is new ScoreImpl(vo); 
 +
 +  
 + void doService() { 
 + is.doService(); 
 +
 +  
 +
 +</code> 
 +++++ 
 + 
 +++++TestScore| 
 +<code java> 
 +public class TestScore { 
 + 
 + public static void main(String[] args) { 
 + // 1. 입력 
 + String[][] score {{"홍길동", "90", "80", "70", "?", "?", "?"}, 
 + {"강감찬", "80", "90", "70", "?", "?", "?"}, 
 + {"유관순", "80", "30", "60", "?", "?", "?"}, 
 + {"이순신", "80", "20", "60", "?", "?", "?"}, 
 + {"김갑순", "70", "90", "60", "?", "?", "?"}, 
 + {"총 점", "?", "?", "?", "?", "-", "-"}, 
 + }; 
 + // 2. 처리 
 + ScoreVO vo new ScoreVO(score); 
 + Score s new Score(vo); 
 + // 3. 결 
 + s.doService(); 
 +
 + 
 +
 +</code> 
 +++++ 
 + 
 +++++User Defined Exception| 
 +<code java> 
 +public class ScoreSizeOutOfBoundException extends Exception { 
 + ScoreSizeOutOfBoundException(String msg){ 
 + super(msg); 
 +
 +
 +</code> 
 +++++ 
 + 
 +==== 엘리베이터 프로젝트 (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, NumberFormatException, CannotGoUpException, CannotGoDownException; 
 +
 +</code> 
 +++++ 
 + 
 +++++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("Cannot Go up"); 
 + } 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("Cannot Go Down"); 
 + } else { 
 + GoDownThread gd = new GoDownThread(vo); 
 + gd.start(); 
 + try { 
 + gd.join(); 
 + } catch (InterruptedException e) {} 
 +
 +
 + public void setFloor(String PdesiredFloor) throws NumberFormatException, CannotGoUpException, CannotGoDownException { 
 + vo.setDesiredFloor(Integer.parseInt(PdesiredFloor)); 
 + while(vo.getCurrentFloor() != vo.getDesiredFloor()) { 
 + if(vo.getCurrentFloor() < vo.getDesiredFloor()) { 
 + goUp(); 
 + } else { 
 + goDown(); 
 +
 +
 +
 +
 +</code> 
 +++++ 
 + 
 +---- 
 + 
 +**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("Closing door"); 
 + vo.setDoorOpen(false); 
 + System.out.println("Door is closed"); 
 +
 +
 +</code> 
 +++++ 
 + 
 +++++OpenDoorThread| 
 +<code java> 
 +public class OpenDoorThread extends Thread { 
 + private ElevatorVO vo; 
 +  
 + OpenDoorThread(ElevatorVO vo){ 
 + this.vo = vo; 
 +
 +  
 + @Override 
 + public void run() { 
 + System.out.println("Opening door"); 
 + vo.setDoorOpen(true); 
 + System.out.println("Door is open"); 
 +
 +
 +</code> 
 +++++ 
 + 
 +++++GoUpThread| 
 +<code java> 
 +public class GoUpThread extends Thread { 
 + private ElevatorVO vo; 
 +  
 + GoUpThread(ElevatorVO vo){ 
 + this.vo = vo; 
 +
 +  
 + @Override 
 + public void run() { 
 + System.out.println("Going up one floor"); 
 + vo.setCurrentFloor(vo.getCurrentFloor()+1); 
 + System.out.println("Floor: "+vo.getCurrentFloor());  
 +
 +
 +</code> 
 +++++ 
 + 
 +++++GoDownThread | 
 +<code java> 
 +public class GoDownThread extends Thread { 
 + private ElevatorVO vo; 
 +  
 + GoDownThread(ElevatorVO vo){ 
 + this.vo = vo; 
 +
 +  
 + @Override 
 + public void run() { 
 + System.out.println("Going down one floor"); 
 + vo.setCurrentFloor(vo.getCurrentFloor()-1); 
 + System.out.println("Floor: "+vo.getCurrentFloor());  
 +
 +
 +</code> 
 +++++ 
 + 
 +++++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; 
 +
 +  
 +
 +</code> 
 +++++ 
 + 
 +++++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("CannotGoUpException : "+e.getMessage()); 
 +
 +  
 +
 + public void goDown() { 
 + try { 
 + ie.goDown();  
 + } catch(CannotGoDownException e) { 
 + System.out.println("CannotGoDownException : "+e.getMessage()); 
 +
 +  
 +
 + public void setFloor(String desiredFloor) { 
 + try { 
 + ie.setFloor(desiredFloor);  
 + } catch(NumberFormatException e) { 
 + System.out.println("NumberFormatException !!"); 
 + } catch(CannotGoUpException e) { 
 + System.out.println("CannotGoUpException : "+e.getMessage()); 
 + } catch(CannotGoDownException e) { 
 + System.out.println("CannotGoDownException : "+e.getMessage()); 
 + } catch(Exception e) { 
 + System.out.println("알 수 없는 예외 발생 !!"); 
 +
 +  
 +
 +
 +</code> 
 +++++ 
 + 
 +++++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); 
 +
 +  
 +
 +</code> 
 +++++ 
 + 
 +---- 
 + 
 +**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); 
 +
 +
 +</code> 
 +++++ 
 +==== FooBarBaz 프로젝트 (J2SE 버전, 품질10단계, 멀티쓰레드) ==== 
 + 
 + 
 +++++IFooBarBaz| 
 +<code java> 
 +interface IFooBarBaz { 
 + void printFooBarBaz() throws NumberFormatException; 
 +
 +</code> 
 +++++ 
 + 
 + 
 +++++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(); 
 +
 +
 +</code> 
 +++++ 
 + 
 + 
 +++++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(" "+FooBarBazVO.FOO); 
 +
 + if(i % FooBarBazVO.FIVE == 0) { 
 + System.out.print(" "+FooBarBazVO.BAR); 
 +
 + if(i % FooBarBazVO.SEVEN == 0) { 
 + System.out.print(" "+FooBarBazVO.BAZ); 
 +
 + System.out.println(); 
 + if((i % FooBarBazVO.THREE == 0) && (i % FooBarBazVO.FIVE == 0) && (i % FooBarBazVO.SEVEN == 0)) { 
 + throw new FooBarBazException("축하합니다! "+ FooBarBazVO.FOO_BAR_BAZ +"입니다!"); 
 +
 +
 + System.out.println(); 
 + System.out.print("and so on."); 
 + } catch(NumberFormatException e) { 
 + System.out.println("입력된 값이 숫자 포맷이 아닙니다."); 
 + } catch(FooBarBazException e) { 
 + System.out.println(e.getMessage()); 
 + } catch(Exception e) { 
 + System.out.println("알 수 없는 예외 발생!"); 
 +
 +
 +
 +</code> 
 +++++ 
 + 
 + 
 +++++FooBarBazVO| 
 +<code java> 
 +public class FooBarBazVO { 
 + public final static String FOO = "foo"; 
 + public final static String BAR = "bar"; 
 + public final static String BAZ = "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; 
 +
 +
 +</code> 
 +++++ 
 + 
 + 
 + 
 +++++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("NumberFormatException! "); 
 + } catch(Exception e) { 
 + System.out.println("알 수 없는 예외 발생!"); 
 +
 +
 +
 +TestFooBarBaz  
 + 
 +public class TestFooBarBaz { 
 + 
 + public static void main(String[] args) { 
 + FooBarBazVO vo = new FooBarBazVO(); 
 + vo.setStart("1"); 
 + vo.setEnd("150"); 
 + FooBarBaz fbb = new FooBarBaz(vo); 
 + fbb.printFooBarBaz(); 
 +
 +
 +</code> 
 +++++ 
 + 
 +---- 
 + 
 +**User Defined Exception** 
 + 
 +++++FooBarBazException| 
 +<code java> 
 +public class FooBarBazException extends Exception { 
 + FooBarBazException(String msg){ 
 + super(msg); 
 +
 +
 +</code> 
 +++++ 
 + 
 +==== 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) ; 
 +
 +</code> 
 +++++ 
 ++++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, double interestRate) { 
 +     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 succeeds 
 +                 } 
 +          return result; 
 +          } 
 +
 +</code> 
 +++++ 
 + 
 +++++CheckingAccount| 
 +<code java
 +package com.mybank.domain; 
 +public class CheckingAccount implements Account { 
 +        protected double balance; 
 +      protected String name; 
 +     private double overdraftAmount = 0; 
 +     
 +     public CheckingAccount(double initBalance, double overdraftAmount) { 
 +     balance = initBalance; 
 +     this.overdraftAmount = overdraftAmount; 
 +     } 
 + 
 +     public CheckingAccount(double initBalance) { 
 +     this(initBalance, 0.0); 
 +     } 
 +     
 +     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; 
 +     }     
 +
 +</code
 +++++ 
 + 
 +++++TestTypeSafety| 
 +<code java> 
 +import com.mybank.domain.*; 
 +import java.util.*; 
 + 
 +public class TestTypeSafety { 
 + public static void main(String[] args) { 
 + List<CheckingAccount> lc = new ArrayList<CheckingAccount>(); 
 +   
 + lc.add(new CheckingAccount("Fred")); // OK 
 + lc.add(new SavingsAccount("Fred"));  // Compile error! 
 +   
 + // therefore...  
 + CheckingAccount ca = lc.get(0);      // Safe, no cast required  
 + System.out.println("Withdraw 150.00 : " + ca.withdraw(150.00)); 
 + System.out.println("Deposit 22.50 : " + ca.deposit(22.50)); 
 + System.out.println("Withdraw 47.62 : " + ca.withdraw(47.62)); 
 + System.out.println("Withdraw 400.00 : " + ca.withdraw(400.00)); 
 + System.out.println("Balance : " + ca.getBalance());  
 +
 +
 +</code> 
 +++++ 
 + 
 +---- 
 + 
 +++++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("Withdraw 150.00 : " + ts.cWithdraw(150.00)); 
 + System.out.println("Deposit 22.50 : " + ts.cDeposit(22.50)); 
 + System.out.println("Withdraw 47.62 : " + ts.cWithdraw(47.62)); 
 + System.out.println("Withdraw 400.00 : " + ts.cWithdraw(400.00)); 
 + System.out.println("Balance : " + ts.cBalance); 
 +  
 + System.out.println("========================================="); 
 +  
 + System.out.println("Withdraw 150.00 : " + ts.sWithdraw(150.00)); 
 + System.out.println("Deposit 22.50 : " + ts.sDeposit(22.50)); 
 + System.out.println("Withdraw 47.62 : " + ts.sWithdraw(47.62)); 
 + System.out.println("Withdraw 400.00 : " + ts.sWithdraw(400.00)); 
 + System.out.println("Balance : " + ts.sBalance); 
 +
 + 
 +
 +</code> 
 +++++ 
 + 
 +==== Banking Project (J2SE 버전, 품질2단계) ==== 
 + 
 +++++SavingsAccount| 
 +<code java> 
 +public class SavingsAccount { 
 + private double balance; 
 + private String name; 
 + private double interestRate; 
 +  
 + public SavingsAccount(double initBalance, double interestRate) { 
 + 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; 
 +
 +
 +</code> 
 +++++ 
 + 
 +++++CheckingAccount| 
 +<code java> 
 +public class CheckingAccount { 
 + private double balance; 
 + private String name; 
 + private double overdraftAmount = 0; 
 +  
 + public CheckingAccount(double initBalance, double overdraftAmount) { 
 + balance = initBalance; 
 + this.overdraftAmount = overdraftAmount; 
 +
 +  
 + public CheckingAccount(double initBalance) { 
 + this(initBalance, 0.0); 
 +
 +  
 + 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; 
 +
 +  
 +
 +</code> 
 +++++ 
 + 
 +++++TestSafety| 
 +<code java> 
 +import java.util.ArrayList; 
 +import java.util.List; 
 + 
 +public class TestSafety { 
 + public static void main(String[] args) { 
 + List<CheckingAccount> lc = new ArrayList<CheckingAccount>(); 
 + List<SavingsAccount> ls = new ArrayList<SavingsAccount>(); 
 +  
 + lc.add(new CheckingAccount("Fred")); 
 + ls.add(new SavingsAccount("Fred")); 
 +  
 + CheckingAccount ca = lc.get(0); 
 + SavingsAccount sa = ls.get(0); 
 +  
 + System.out.println("Withdraw 150.00 : " + ca.withdraw(150.00)); 
 + System.out.println("Deposit 22.50 : " + ca.deposit(22.50)); 
 + System.out.println("Withdraw 47.62 : " + ca.withdraw(47.62)); 
 + System.out.println("Withdraw 400.00 : " + ca.withdraw(400.00)); 
 + System.out.println("Balance : " + ca.getBalance()); 
 +  
 + System.out.println("========================================="); 
 +  
 + System.out.println("Withdraw 150.00 : " + sa.withdraw(150.00)); 
 + System.out.println("Deposit 22.50 : " + sa.deposit(22.50)); 
 + System.out.println("Withdraw 47.62 : " + sa.withdraw(47.62)); 
 + System.out.println("Withdraw 400.00 : " + sa.withdraw(400.00)); 
 + System.out.println("Balance : " + sa.getBalance()); 
 +
 + 
 +}
 </code> </code>
 ++++ ++++