import java.util.Scanner;
public abstract class Converter {
abstract protected double convert(double src);
abstract protected String getSrcString();
abstract protected String getDestString();
protected double ratio;
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다.");
System.out.print(getSrcString()+"을 입력하세요>> ");
double val = scanner.nextDouble();
double res = convert(val);
System.out.println("변환 결과: "+res+getDestString()+"입니다");
scanner.close();
}
}
public class Won2Dollar extends Converter {
private int ratio;
public Won2Dollar(int ratio) {
// TODO Auto-generated constructor stub
this.ratio = ratio;
}
@Override
protected double convert(double src) {
// TODO Auto-generated method stub
return src/ratio;
}
@Override
protected String getSrcString() {
// TODO Auto-generated method stub
return "원";
}
@Override
protected String getDestString() {
// TODO Auto-generated method stub
return "달러";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Won2Dollar toDollar = new Won2Dollar(1200);
toDollar.run();
}
}
결과
원을 달러로 바꿉니다.
원을 입력하세요>> 24000
변환 결과: 20.0달러입니다
'명품JAVA프로그래밍 > 5장 상속' 카테고리의 다른 글
[명품JAVA프로그래밍] 5장 실습문제 5번 (0) | 2022.01.12 |
---|---|
[명품JAVA프로그래밍] 5장 실습문제 4번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 2번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 실습문제 1번 (0) | 2022.01.12 |
[명품JAVA프로그래밍] 5장 Open Challenge (0) | 2022.01.12 |