已知人民币、美元、日元三者之间的汇率为1美元=10人民币=100日元,请设计一些类,譬如Money{type,value},支持不同货币之间的加减乘除操作,并且可以转换币种(如 rmb.add(usd)=rmb).

解决方案 »

  1.   

    package net.oicp.sunflowerbbs;enum MoneyType {
    jb(1), rmb(10), usd(100);
    private int value; MoneyType(int arg1) {
    value = arg1;
    } public int getValue() {
    return value;
    }
    }public class Money {
    MoneyType moneyType; protected Money add(Money money) {
    return money.moneyType.getValue() < this.moneyType.getValue() ? money
    : this;
    }

    protected double div(Money money){
    return  1.0d* this.moneyType.getValue()/ money.moneyType.getValue();
    }

    protected String getType(){
    return moneyType.toString();
    }

    protected int getValue(){
    return moneyType.getValue();
    } public Money(MoneyType moneyType) {
    this.moneyType = moneyType;
    } public static void main(String[] args) {
    Money usd=new Money(MoneyType.usd);
    Money o=usd.add(new Money(MoneyType.rmb));
            System.out.println(o.getType());
            System.out.println(usd.div(o));
    }}//备注: enum 是jdk 5.0及以上支持
      

  2.   

    如果不用enum,该如何写呢?谢谢!
      

  3.   

    package net.oicp.sunflowerbb;class MoneyType {
    final static int jb = 1; final static int rmb = 10; final static int usd = 100; private int value; public MoneyType(int arg1) throws Throwable {
                switch (arg1) {
                case jb:
                case rmb:
                case usd:
                   value=arg1;
                   break;
                default:
                 throw (new Exception("arg1 is unvalid"));
             } 
    } public int getValue() {
    return value;
    } public String toString() {
    switch (value) {
    case jb:
    return "jb";
    case rmb:
    return "rmb";
    case usd:
    return "usd";
    default:
    return "error";
    } }
    }public class Money {
    MoneyType moneyType; protected Money add(Money money) {
    return money.moneyType.getValue() < this.moneyType.getValue() ? money
    : this;
    } protected double div(Money money) {
    return 1.0d * this.moneyType.getValue() / money.moneyType.getValue();
    } protected String getType() {
    return moneyType.toString();
    } protected int getValue() {
    return moneyType.getValue();
    } public Money(MoneyType moneyType) {
    this.moneyType = moneyType;
    } public static void main(String[] args) throws Throwable {
    Money usd = new Money(new MoneyType(MoneyType.usd));
    Money o = usd.add(new Money(new MoneyType(MoneyType.rmb)));
    System.out.println(o.getType());
    System.out.println(usd.div(o));
    }
    }
      

  4.   

    guorujian() 不明白你的意思
      

  5.   

    就是说你可以把N种货币转换成同一种货币计算(一般来说转换成最大公约数的货币)。返回值可以是转换之前的,然后再写一个统一的方法,将这个值转换成目标货币值!
    比如:把美元+人民币 转换成日元计算(为什么转换成日元不用说了吧,呵呵)那么返回值就是日元,然后用一个统一的方法将日元转换成目标货币!
    2美元+3人民币=200日元+30日元=230日元;然后将230日元转换目标货币
    public int change(int value,String type)//type是目标货币
    {
    //value是日元的值,这样返回的值可以根据type转换!
    }
    OK,只是我的想法,希望提出更好的算法!共同学习
      

  6.   

    终于告一段落了,哈,刚才很忙,给你写一下啊,没经过测试,自己测试一下吧:
    public class Money
    {
        String type;
        int value;
        
        public Money(String type,int value)
        {
            this.type = type;
            this.value = value;
        }
        public Money()
        {
        }
        public String getType()
        {
            return type;
        }
        public void setType(String type)
        {
            this.type = type;
        }
        public int getValue()
        {
            return value;
        }
        public void setValue(int value)
        {
            this.value = value;
        }
        public static int 相加(Money my1,Money my2)
        {
            Money temp1 = new Money();
            Money temp2 = new Money();
            if(!my1.getType().equals("jp"))
                temp1 = Money.变日本Money(my1);
            else
                temp1 = my1;
            if(!my2.getType().equals("jp"))
                temp2 = Money.变日本Money(my2);
            else
                temp2 = my2;
            int num = temp1.getValue()+temp2.getValue();
            return num;//返回小日本的货币值
        }    public static int 汇率(String type)//汇率,以日元为准
        {
            if(type.equals("jp"))
                return 1;
            else if(type.equals("ch"))
                return 10;
            else if(type.equals("us"))
                return 100;
            else return 0;
        }
        public static Money 变日本Money(Money my)
        {
            Money jpmy = new Money();
            if(!my.getType().equals("jp"))
            {
                int value = 0;
                jpmy.setType("jp");
                value = my.getValue()*Money.汇率(my.getType());
                jpmy.setValue(value);
                return jpmy;
            }
            else
                return my;
        }
        public static double 改变货币值(int value,String type)
        {
            if(type.equals("jp"))
                return value;
            else if(type.equals("ch"))
                return value/10.0;
            else if(type.equals("us"))
                return value/100.0;
            else return 0.0;
        }
        public static void main(String[] args)
        {
            //1美元+2日元=(?)人民币;
            Money jp = new Money("jp",2);
            Money us = new Money("us",1);
            int re = Money.相加(jp,us);
            double print = Money.改变货币值(re,"ch");
            System.out.println("ch = "+print);
        }
    }
    里面的值类型你自己改一下,我为了方便直接用int了,呵呵