(Q) 다음 데이터를 요구사항에 맞게 구조화하고 출력하세요
- 약정기간을 설정한 경우에는 월 할부금을 계산해서 출력
- 월 할부금은 판매가격을 약정기간동안 나눠서 낸다고 가정하고 계산
- 약정기간을 설정하지 않은 경우에는 가격 옆에 (약정 없음) 을 출력
- 약정기간을 설정하지 않은 경우에는 5% 할인된 가격으로 출력
Phone 클래스
package day09_oop.method5;
public class Phone {
// 멤버 필드
String name;
String tel;
int price;
int duration;
// 세팅 멤소드
void setting(String name, String tel, int price, int duration) {
this.name = name;
this.tel = tel;
this.price = price;
this.duration = duration;
}
// 출력 메소드
void print() {
System.out.println("이름 : " + this.name);
System.out.println("통신사 : " + this.tel);
if(this.duration == 0) {
System.out.println("가격 : " + (this.price * (100 - 5) / 100) + " (약정없음)");
}
else {
int installment = this.price / this.duration;
System.out.println("가격 : " + this.price + " (예상할부금 : " + installment + ")");
}
System.out.println("약정 개월 : " + this.duration);
System.out.println();
}
}
Test01
package day09_oop.method5;
public class Test01 {
public static void main(String[] args) {
// 객체 생성
Phone a = new Phone();
a.setting("갤럭시22s", "SK", 1800000, 0);
Phone b = new Phone();
b.setting("갤럭시22s", "KT", 1750000, 24);
Phone c = new Phone();
c.setting("아이폰13", "LG", 2000000, 30);
Phone d = new Phone();
d.setting("아이폰13", "SK", 1990000, 0);
// 출력
a.print();
b.print();
c.print();
}
}
'국비교육 > 국비교육 복습' 카테고리의 다른 글
Day09_oop.method7 : 메소드 연습 (6) (0) | 2022.08.07 |
---|---|
Day09_oop.method6 : 메소드 연습 (5) (0) | 2022.08.07 |
Day09_oop.method4 : 메소드 연습 (3) (0) | 2022.08.07 |
Day09_oop.method3 : 메소드 연습 (2) (0) | 2022.08.07 |
Day09_oop.method2 : 메소드 연습 (1) (0) | 2022.08.07 |