1 2 3 4 5 6 7 8 9 10 11 12 | package t_0904_1; public interface Washer { public void washing(); public void rinsing(); public void dehydration(); public void dry(); public void ironing(); public void washer(String washer); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package t_0904_1; public class Clean implements Washer { public int price = 0; @Override public void washing() { System.out.println("세탁기능 : 200원"); price = 200; } @Override public void rinsing() { System.out.println("헹굼기능 : 500원"); price = 500; } @Override public void dehydration() { System.out.println("탈수기능 : 300원"); price = 300; } @Override public void dry() { System.out.println("건조기능 : 500원"); price = 500; } @Override public void ironing() { System.out.println("다림질기능 : 1000원"); price = 1000; } @Override public void washer(String washer) { System.out.println(washer + "세탁기"); } public int totalPrice(int number,int time) { int totalPrice = 0; switch (number) { case 1: totalPrice = 200 * time; break; case 2 : totalPrice = 500 * time; break; case 3 : totalPrice = 300 * time; break; case 4 : totalPrice = 500 * time; break; case 5 : totalPrice = 1000 * time; break; default : break; } return totalPrice; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package t_0904_1; public class Purity implements Washer { public int price = 0; @Override public void washing() { System.out.println("세탁기능 : 150원"); price = 150; } @Override public void rinsing() { System.out.println("헹굼기능 : 300원"); price = 300; } @Override public void dehydration() { System.out.println("탈수기능 : 200원"); price = 200; } @Override public void dry() { System.out.println("건조기능 : 300원"); price = 300; } @Override public void ironing() { System.out.println("다림질기능 : 사용불가"); } @Override public void washer(String washer) { System.out.println(washer + "세탁기"); } public int totalPrice(int number,int time) { int totalPrice = 0; switch (number) { case 1: totalPrice = 150 * time; break; case 2 : totalPrice = 300 * time; break; case 3 : totalPrice = 200 * time; break; case 4 : totalPrice = 300 * time; break; default : break; } return totalPrice; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package t_0904_1; public class Green implements Washer { public int price = 0; @Override public void washing() { System.out.println("세탁기능 : 100원"); price = 100; } @Override public void rinsing() { System.out.println("헹굼기능 : 200원"); price = 200; } @Override public void dehydration() { System.out.println("탈수기능 : 100원"); price = 100; } @Override public void dry() { System.out.println("건조기능 : 사용불가"); } @Override public void ironing() { System.out.println("다림질기능 : 사용불가"); } @Override public void washer(String washer) { System.out.println(washer + "세탁기"); } public int totalPrice(int number,int time) { int totalPrice = 0; switch (number) { case 1: totalPrice = 100 * time; break; case 2 : totalPrice = 200 * time; break; default : break; } return totalPrice; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package t_0904_1; import java.util.Scanner; public class Run { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] washerName = {"클린","청정","그린"}; Washer[] washer = {new Clean(), new Purity(), new Green()}; Clean clean = new Clean(); for(int i=0; i<washer.length; i++) { System.out.println("** "+washerName[i]+" 세탁기 **"); washer[i].washing(); washer[i].rinsing(); washer[i].dehydration(); washer[i].dry(); washer[i].ironing(); System.out.println(); } System.out.println("-----------------------------------------"); String name; int number; int time = 0; int price = 0; int tot = 0; boolean run = true; while(run) { System.out.println("사용하실 세탁기의 이름을 입력해주세요."); System.out.print("클린 | 청정 | 그린 | 종료 >> "); name = scanner.next(); if(name.equals("클린")) { System.out.println("클린세탁기에서 사용하실 기능의 번호를 입력해주세요."); System.out.print("1.세탁기능 | 2.헹굼기능 | 3.탈수기능 | 4.건조기능 | 5.다림질기능 | 6.종료 >> " ); number = scanner.nextInt(); System.out.print("사용 시간을 입력해주세요 >> "); time = scanner.nextInt(); clean.totalPrice(number, time); System.out.println("내실 금액은 "+clean.totalPrice(number, time)+"원입니다."); tot += price; } else if(name.equals("청정")) { System.out.println("청정세탁기에서 사용하실 기능의 번호를 입력해주세요."); System.out.print("1.세탁기능 | 2.헹굼기능 | 3.탈수기능 | 4.건조기능 | 5.종료 >> " ); number = scanner.nextInt(); System.out.print("사용 시간을 입력해주세요 >> "); time = scanner.nextInt(); clean.totalPrice(number, time); System.out.println("내실 금액은 "+clean.totalPrice(number, time)+"원입니다."); tot += price; } else if(name.equals("그린")) { System.out.println("그린세탁기에서 사용하실 기능의 번호를 입력해주세요."); System.out.print("1.세탁기능 | 2.헹굼기능 | 3.탈수기능 | 4.종료 >> " ); number = scanner.nextInt(); System.out.print("사용 시간을 입력해주세요 >> "); time = scanner.nextInt(); clean.totalPrice(number, time); System.out.println("내실 금액은 "+clean.totalPrice(number, time)+"원입니다."); tot += price; } else if(name.equals("종료")) { System.out.println("총 내실 금액은 " +tot+ "원입니다."); run = false; } else { System.out.println("존재하지 않는 세탁기이름을 입력하셨습니다."); } } scanner.close(); } } | cs |
마지막의 총 금액을 계산하지 못했습니다.
문제 2번
1 2 3 4 5 6 7 8 | package t_0904_2; public interface Person { public void sosok(String sosok); public void personName(String name); public void personAddress(String address); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package t_0904_2; public class SinSang { Person sosok = new Person() { @Override public void sosok(String sosok) { System.out.println("현재 소속 : " + sosok); } @Override public void personName(String name) {} @Override public void personAddress(String address) {} }; public void information() { Person personInfor = new Person() { @Override public void sosok(String sosok) {} @Override public void personName(String name) { System.out.println("이름 : " + name); } @Override public void personAddress(String address) { System.out.println("주소 : " + address); } }; personInfor.personName("홍길동"); personInfor.personAddress("서울"); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 | package t_0904_2; public class Run { public static void main(String[] args) { SinSang sinSang = new SinSang(); sinSang.sosok.sosok("인사과"); sinSang.information(); } } | cs |
첫댓글 1번: 분리된 메소드 처리~ 가독성있어서 좋네요. 각 세탁기별로 해당기능을 인터페이스 구현객체에서 처리하는것도 좋을듯 합니다.
2번: 익명객체 활용 좋습니다.
잘 보았습니다. 수고하셨어요