Washer2.java
| 1 |
// package cse.oop2.ch13_3;
|
|---|---|
| 2 |
|
| 3 |
|
| 4 |
public class Washer2 { |
| 5 |
|
| 6 |
private int userInput; |
| 7 |
|
| 8 |
public Washer2(int userInput) { |
| 9 |
this.userInput = userInput;
|
| 10 |
} |
| 11 |
|
| 12 |
public void foo() { |
| 13 |
Laundry laundry = new Laundry(userInput);
|
| 14 |
try {
|
| 15 |
laundry.doLaundry(); |
| 16 |
System.out.println("세탁이 완료되었습니다!"); |
| 17 |
} catch (ClothingException ex) {
|
| 18 |
System.out.printf("예외: %s%n", ex.getMessage()); |
| 19 |
System.out.println("세탁이 되지 않았습니다!"); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public static void main(String[] args) throws ClothingException { |
| 24 |
int userInput = -1; |
| 25 |
if (args.length >= 1) { |
| 26 |
if (args[0].matches("^-?\\d+$")) { |
| 27 |
System.out.println("숫자가 맞습니다."); |
| 28 |
userInput = Integer.parseInt(args[0]); |
| 29 |
} else {
|
| 30 |
System.out.println("숫자가 아닙니다."); |
| 31 |
} |
| 32 |
} |
| 33 |
System.out.printf("입력 값은 %d입니다.%n", userInput); |
| 34 |
|
| 35 |
Washer2 w = new Washer2(userInput);
|
| 36 |
w.foo(); |
| 37 |
} |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
class Laundry { |
| 43 |
|
| 44 |
private int userInput; |
| 45 |
|
| 46 |
public Laundry(int userInput) { |
| 47 |
this.userInput = userInput;
|
| 48 |
} |
| 49 |
|
| 50 |
public void doLaundry() throws ClothingException { |
| 51 |
if (userInput < 0) { |
| 52 |
throw new ClothingException(); |
| 53 |
} else {
|
| 54 |
System.out.println("세탁을 합니다."); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
class ClothingException extends Exception { |
| 62 |
|
| 63 |
public ClothingException() {
|
| 64 |
super("ClothingException"); |
| 65 |
} |
| 66 |
|
| 67 |
} |