请完成汇率和金额排序程序。(100分)题目内容: 在国际机场,我们会发现有多个货币兑换的窗口,这是由于各国货币的单位价值是不一样的。下面列出了某日国际货币的汇率表(相对于100人民币的各国货币值)。

题目内容:

在国际机场,我们会发现有多个货币兑换的窗口,这是由于各国货币的单位价值是不一样的。下面列出了某日国际货币的汇率表(相对于100人民币的各国货币值)。

 

货币

CNY

HKD

USD

EUR

汇率

100

118

15

13

 

例如,100元人民币可以兑换118香港币。请利用继承机制与Comparable接口实现不同数目货币对象价值的排序。下面给出了程序的基本框架,请给出相关的函数实现。(假设所有的输入数值和比较都是用整数)

import java.util.Arrays;

import java.util.Scanner;

 

class Currency {

private String name;        //货币名称

private int originalValue;  //原始值

private int value;          //转换为人民币后的值

 

public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" };

public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 };

 

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

 

public int getOriginalValue() {

return originalValue;

}

public void setOriginalValue(int originalValue) {

this.originalValue = originalValue;

}

 

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

}

 

class HKD extends Currency implements Comparable {

// 实现你的构造函数与Comparable中的接口

}

 

class USD extends Currency implements Comparable {

// 实现你的构造函数与Comparable中的接口

}

 

class EUR extends Currency implements Comparable {

// 实现你的构造函数与Comparable中的接口

}

 

public class Main {

public static void main(String[] args) {

Currency[] cs = new Currency[3];

//初始化

Scanner sc = new Scanner(System.in);

                //利用hasNextXXX()判断是否还有下一输入项

                int a = 0;

                int b = 0;

                int c = 0;

                if (sc.hasNext()) {

                    a = sc.nextInt();

                    cs[0] = new HKD(a);

                }

 

                if (sc.hasNext()) {

                    b = sc.nextInt();

                    cs[1] = new USD(b);

                }

        

                if (sc.hasNext()) {

                    c = sc.nextInt();

                    cs[2] = new EUR(c);

                }

 

//初始化结束

 

//请补充排序

 

 

//请补充输出结果

}

}

 

 

输入格式:

输入3个数字。

 

输出格式:

金额价值从小到大

 

输入样例:

100

100

100

 

输出样例:

HKD100

USD100

EUR100

时间限制:500ms内存限制:32000kb

 

答案:

import java.util.Arrays;
import java.util.Scanner;

 class Currency {
	protected String name;       
	private int originalValue;  
	protected int value;          
	
	public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" };
	public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 };
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getOriginalValue() {
		return originalValue;
	}
	public void setOriginalValue(int originalValue) {
		this.originalValue = originalValue;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}

	public Currency(String name, int originalValue, int value) {
		super();
		this.name = name;
		this.originalValue = originalValue;
		this.value = value;
	}
	public Currency() {
		super();
	}
	
}

 class HKD extends Currency implements Comparable<Currency> {
	
	public HKD(int a) {
		this.setName(CURRENCY_NAME[1]);
		this.setOriginalValue(a);
		int i= CURRENCY_RATIO[0];
		int j= CURRENCY_RATIO[1];
		float k=(float)i/j;
		a =(int)(a*k);
		this.setValue(a);
	}


	public int compareTo(Currency another) {
		int i = 0;
		if (i == 0) {
			return value- another.value;
		} else {
			return i; 
		}
	}

}

 class USD extends Currency implements Comparable<Currency> {

	public USD(int b) {
		
		this.setName(CURRENCY_NAME[2]);
		this.setOriginalValue(b);
		int i= CURRENCY_RATIO[0];
		int j= CURRENCY_RATIO[2];
		float k=(float)i/j;
		b =(int)(b*k);
		this.setValue(b);
	}

	public int compareTo(Currency another) {
		int i = 0;
		if (i == 0) {
			return value- another.value;
		} else {
			return i; 
		}
	}


}

 class EUR extends Currency implements Comparable<Currency> {
	public EUR(int c) {
		this.setName(CURRENCY_NAME[3]);
		this.setOriginalValue(c);
		int i= CURRENCY_RATIO[0];
		int j= CURRENCY_RATIO[3];
		float k=(float)i/j;
		c =(int)(c*k);
		this.setValue(c);
	}
	public int compareTo(Currency another) {
		int i = 0;
		if (i == 0) {
			return value- another.value;
		} else {
			return i; 
		}
	}
}

public class Main {
	public static void main(String[] args) {
		Currency[] cs = new Currency[3];
		Scanner sc = new Scanner(System.in);
                int a = 0;
                int b = 0;
                int c = 0;
                if (sc.hasNext()) {
                    a = sc.nextInt();
                    cs[0] = new HKD(a);
                }
                if (sc.hasNext()) {
                    b = sc.nextInt();
                    cs[1] = new USD(b);
                }
                if (sc.hasNext()) {
                    c = sc.nextInt();
                    cs[2] = new EUR(c);
                }
      Arrays.sort(cs);
      for (Currency currency : cs) {
		System.out.println(currency.getName()+currency.getOriginalValue());
      }
	}
}

题目来自于网络 如有侵权请私信本人删除,谢谢

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值