用Java编写某类,可以将外币和本币互兑,并编写测试程序创建一些不同国家货币的对象。
求用JAVA编写ForeignCurrency类。使用该类可以将外币兑换成本国的货币,也可以将本国货币转换成外币;并编写测试程序创建一些可以反映不同国家货币的对象。分不多...
求 用JAVA编写ForeignCurrency类。使用该类可以将外币兑换成本国的货币,也可以将本国
货币转换成外币;并编写测试程序创建一些可以反映不同国家货币的对象。
分不多,但已是倾尽所有,拜托! 展开
货币转换成外币;并编写测试程序创建一些可以反映不同国家货币的对象。
分不多,但已是倾尽所有,拜托! 展开
2个回答
展开全部
//试着写了一个~~
///: Filename: ForeignCurrency.java
public class ForeignCurrency {
/**<font color='blue'>外币兑本币,<B>ca</B> 要兑换的外币金额*/
public double f2RMB(Currency src,double ca){
return ca*src.getExchangeRate();
}
/**<font color='blue'>本币兑外币,<B>RMB</B> 要兑换的本币金额*/
public double RMB2F(Currency dest,double RMB){
return RMB/dest.getExchangeRate();
}
/**<font color='blue'>外币兑外币,<B>cash</B> 要兑换的原外币(src)金额*/
public double f2F(Currency src,Currency dest,double cash){
return src.getExchangeRate()/dest.getExchangeRate()*cash;
}
public static void main(String[] args){
ForeignCurrency x = new ForeignCurrency();
Currency rmb = new Currency(CurType.本币,1);//定义一个RMB类,兑RMB的汇率是 1
Currency us$ = new Currency(CurType.美元,7.3);//定义一个美金类,兑RMB的汇率是 7.3
Currency au$ = new Currency(CurType.澳元,9.65);//定义一个澳元类,兑RMB的汇率是 9.65
Currency jp$ = new Currency(CurType.日元,0.0101);//定义一个日元类,兑RMB的汇率是 0.0101
double RMB = 888;
double US$ = 123;
double AU$ = 234;
double JP$ = 5678.9;
//美元兑换RMB;
double us2rmb = x.f2RMB(us$, US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+rmb.getCURName()+": "+us2rmb+" 汇率:"+us$.getExchangeRate()/rmb.getExchangeRate());
//澳元兑换RMB
double au2rmb = x.f2RMB(au$, AU$);
System.out.println(au$.getCURName()+": "+AU$+" -> "+rmb.getCURName()+": "+au2rmb+" 汇率:"+au$.getExchangeRate()/rmb.getExchangeRate());
//日元兑换RMB
double jp2rmb = x.f2RMB(jp$, JP$);
System.out.println(jp$.getCURName()+": "+JP$+" -> "+rmb.getCURName()+": "+jp2rmb+" 汇率:"+jp$.getExchangeRate()/rmb.getExchangeRate());
//美元兑换澳元
double us2au = x.f2F(us$, au$,US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+au$.getCURName()+": "+us2au+" 汇率:"+us$.getExchangeRate()/au$.getExchangeRate());
//....................
//RMB兑换澳元
double rmb2au = x.RMB2F(au$,RMB);
System.out.println(rmb.getCURName()+": "+RMB+" -> "+au$.getCURName()+": "+rmb2au+" 汇率:"+rmb.getExchangeRate()/au$.getExchangeRate());
//....................
}
}
//货币名称枚举
enum CurType{
本币,美元,澳元,日元,法郎//分别指:人民币,美金,澳元,日元,法郎...
}
//货币类,所有汇率都是以RMB为基准的
class Currency{
private double xr;//对RMB的汇率
private CurType type;//名称,比如US$
/**<font color='red'>参数 CUR 货币名称,double 对RMB的汇率*/
public Currency(CurType c,double xRMB){
xr = xRMB;
type = c;
}
/**<font color='blue'>获取外币对RMB的汇率*/
public double getExchangeRate() {
return xr;
}
/**<font color='blue'>获取货币名称*/
public String getCURName(){return type.name();}
}
///: Filename: ForeignCurrency.java
public class ForeignCurrency {
/**<font color='blue'>外币兑本币,<B>ca</B> 要兑换的外币金额*/
public double f2RMB(Currency src,double ca){
return ca*src.getExchangeRate();
}
/**<font color='blue'>本币兑外币,<B>RMB</B> 要兑换的本币金额*/
public double RMB2F(Currency dest,double RMB){
return RMB/dest.getExchangeRate();
}
/**<font color='blue'>外币兑外币,<B>cash</B> 要兑换的原外币(src)金额*/
public double f2F(Currency src,Currency dest,double cash){
return src.getExchangeRate()/dest.getExchangeRate()*cash;
}
public static void main(String[] args){
ForeignCurrency x = new ForeignCurrency();
Currency rmb = new Currency(CurType.本币,1);//定义一个RMB类,兑RMB的汇率是 1
Currency us$ = new Currency(CurType.美元,7.3);//定义一个美金类,兑RMB的汇率是 7.3
Currency au$ = new Currency(CurType.澳元,9.65);//定义一个澳元类,兑RMB的汇率是 9.65
Currency jp$ = new Currency(CurType.日元,0.0101);//定义一个日元类,兑RMB的汇率是 0.0101
double RMB = 888;
double US$ = 123;
double AU$ = 234;
double JP$ = 5678.9;
//美元兑换RMB;
double us2rmb = x.f2RMB(us$, US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+rmb.getCURName()+": "+us2rmb+" 汇率:"+us$.getExchangeRate()/rmb.getExchangeRate());
//澳元兑换RMB
double au2rmb = x.f2RMB(au$, AU$);
System.out.println(au$.getCURName()+": "+AU$+" -> "+rmb.getCURName()+": "+au2rmb+" 汇率:"+au$.getExchangeRate()/rmb.getExchangeRate());
//日元兑换RMB
double jp2rmb = x.f2RMB(jp$, JP$);
System.out.println(jp$.getCURName()+": "+JP$+" -> "+rmb.getCURName()+": "+jp2rmb+" 汇率:"+jp$.getExchangeRate()/rmb.getExchangeRate());
//美元兑换澳元
double us2au = x.f2F(us$, au$,US$);
System.out.println(us$.getCURName()+": "+US$+" -> "+au$.getCURName()+": "+us2au+" 汇率:"+us$.getExchangeRate()/au$.getExchangeRate());
//....................
//RMB兑换澳元
double rmb2au = x.RMB2F(au$,RMB);
System.out.println(rmb.getCURName()+": "+RMB+" -> "+au$.getCURName()+": "+rmb2au+" 汇率:"+rmb.getExchangeRate()/au$.getExchangeRate());
//....................
}
}
//货币名称枚举
enum CurType{
本币,美元,澳元,日元,法郎//分别指:人民币,美金,澳元,日元,法郎...
}
//货币类,所有汇率都是以RMB为基准的
class Currency{
private double xr;//对RMB的汇率
private CurType type;//名称,比如US$
/**<font color='red'>参数 CUR 货币名称,double 对RMB的汇率*/
public Currency(CurType c,double xRMB){
xr = xRMB;
type = c;
}
/**<font color='blue'>获取外币对RMB的汇率*/
public double getExchangeRate() {
return xr;
}
/**<font color='blue'>获取货币名称*/
public String getCURName(){return type.name();}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主要知道汇率就行了~~
方法应该不难吧,
用Scanner得到输入,再将输入数据根据汇率转换,再输出结果
莫非我理解错了
呵呵~我初学JAVA~~说错了别喷我哈~
方法应该不难吧,
用Scanner得到输入,再将输入数据根据汇率转换,再输出结果
莫非我理解错了
呵呵~我初学JAVA~~说错了别喷我哈~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询