문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
프로그램:java:ㄴhomework:ㄴhomework [2022/02/03 15:03] clayeryan@gmail.com [과제 88] |
프로그램:java:ㄴhomework:ㄴhomework [2025/06/27 16:04] (현재) |
||
|---|---|---|---|
| 줄 3: | 줄 3: | ||
| [[https:// | [[https:// | ||
| - | [[programmer: | + | [[프로그램: |
| - | [[programmer: | + | [[프로그램: |
| - | [[programmer: | + | [[프로그램: |
| ===== Hello World 출력 ===== | ===== Hello World 출력 ===== | ||
| 줄 8425: | 줄 8425: | ||
| ++++ | ++++ | ||
| - | ==== 과제 89 ==== | + | ==== 엘리베이터 프로젝트 (J2SE 버전, 품질10단계, |
| - | ++++title| | + | |
| + | ++++IElevator| | ||
| <code java> | <code java> | ||
| - | java code | + | 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, | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 90 ==== | + | |
| - | ++++title| | + | ++++ElevatorImpl| |
| <code java> | <code java> | ||
| - | java code | + | 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(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 91 ==== | + | |
| - | ++++title| | + | ---- |
| + | |||
| + | **Thread inheritance class** | ||
| + | |||
| + | ++++CloseDoorThread| | ||
| <code java> | <code java> | ||
| - | java code | + | 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(" | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| ++++ | ++++ | ||
| - | ==== 과제 92 ==== | + | |
| + | ++++OpenDoorThread| | ||
| + | <code java> | ||
| + | public class OpenDoorThread extends Thread { | ||
| + | private ElevatorVO vo; | ||
| + | |||
| + | OpenDoorThread(ElevatorVO vo){ | ||
| + | this.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 | ||
| + | } | ||
| + | |||
| + | @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 | ||
| + | } | ||
| + | |||
| + | @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 | ||
| + | private final static int MIN_FLOORS | ||
| + | private int currentFloor | ||
| + | private boolean doorOpen | ||
| + | private int desiredFloor; | ||
| + | |||
| + | public boolean isDoorOpen() { | ||
| + | return doorOpen; | ||
| + | } | ||
| + | public void setDoorOpen(boolean doorOpen) { | ||
| + | this.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