课外天地 李树青学习天地Java程序语言课件 → [推荐]第三次上机作业的说明——货币处理


  共有14529人关注过本帖树形打印复制链接

主题:[推荐]第三次上机作业的说明——货币处理

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
[推荐]第三次上机作业的说明——货币处理  发帖心情 Post By:2008/4/29 6:04:20 [只看该作者]

1、简单实现方法之一
interface ICurrency
{
        public void setAmount(double a);
        public double getAmount();
        public String toString();
}

abstract class Currency implements ICurrency
{
        protected double amount=0;
        
        public void setAmount(double a)
        {
                amount=a;
        }
        
        public double getAmount()
        {
                return amount;
        }
        
        public String toString()
        {
                return "";
        }
}

class Yuan extends Currency
{
        public String toString()
        {
                return "Y"+amount;
        }
}

class Dollar extends Currency
{
        public String toString()
        {
                return "$"+amount;
        }
}

class Action
{
        public void TransCurrency(ICurrency y,double amount)
        {
                y.setAmount(amount);
                System.out.println(y);          
        }
}

public class Application
{
        public static void main(String[] args)
        {
                Action a=new Action();
                a.TransCurrency(new Yuan(),1234);
                a.TransCurrency(new Dollar(),2345);
        }
}

2、简单实现方法之二
interface ICurrency
{
        public void setAmount(double a);
        public double getAmount();
        public String toString();
}

abstract class Currency implements ICurrency
{
        protected double amount=0;
        protected char sign = ' ';
        
        public void setAmount(double a)
        {
                amount=a;
        }
        
        public double getAmount()
        {
                return amount;
        }
        
        public String toString()
        {
                return sign+String.valueOf(amount);
        }
}

class Yuan extends Currency
{
        public Yuan()
        {
                sign='Y';
        }
}

class Dollar extends Currency
{
        public Dollar()
        {
                sign='$';
        }
}

class Action
{
        public void TransCurrency(ICurrency y,double amount)
        {
                y.setAmount(amount);
                System.out.println(y);          
        }
}

public class Application
{
        public static void main(String[] args)
        {
                Action a=new Action();
                a.TransCurrency(new Yuan(),1234);
                a.TransCurrency(new Dollar(),2345);
        }
}

3、带有工厂处理方法的程序
interface ICurrency
{
        public void setAmount(double a);
        public double getAmount();
        public String toString();
}

abstract class Currency implements ICurrency
{
        protected double amount=0;
        protected char sign = ' ';
        
        public void setAmount(double a)
        {
                amount=a;
        }
        
        public double getAmount()
        {
                return amount;
        }
        
        public String toString()
        {
                return sign+String.valueOf(amount);
        }
}

class Yuan extends Currency
{
        public Yuan()
        {
                sign='Y';
        }
}

class Dollar extends Currency
{
        public Dollar()
        {
                sign='$';
        }
}

class Action
{
        public void TransCurrency(char type, double amount)
        {
                ICurrency im=null;
                switch(Character.toUpperCase(type))
                {
                        case 'Y':
                                im=new Yuan();break;
                        case 'D':
                                im=new Dollar();break;
                }
                im.setAmount(amount);
                System.out.println(im);        
        }
}

public class Application
{
        public static void main(String[] args)
        {
                Action a=new Action();
                a.TransCurrency('Y',1234);
                a.TransCurrency('D',2345);
        }
}


4、采用java.text.DecimalFormat类实现的格式化输出
interface ICurrency
{
        public void setAmount(double a);
        public double getAmount();
        public String toString();
}

abstract class Currency implements ICurrency
{
        protected double amount=0;
        protected java.util.Locale sign =null;
        
        public void setAmount(double a)
        {
                amount=a;
        }
        
        public double getAmount()
        {
                return amount;
        }
        
        public String toString()
        {
                java.text.DecimalFormat nf=new java.text.DecimalFormat("¤.0000");
                nf.setCurrency(java.util.Currency.getInstance(sign));
                return nf.format(amount);              
        }
}

class Yuan extends Currency
{
        public Yuan()
        {
                sign=java.util.Locale.CHINA;
        }      
}

class Dollar extends Currency
{
        public Dollar()
        {
                sign=java.util.Locale.US;
        }
}

class Action
{
        public void TransCurrency(char type, double amount)
        {
                ICurrency im=null;
                switch(Character.toUpperCase(type))
                {
                        case 'Y':
                                im=new Yuan();break;
                        case 'D':
                                im=new Dollar();break;
                }
                im.setAmount(amount);
                System.out.println(im);        
        }
}

public class Application
{
        public static void main(String[] args)
        {
                Action a=new Action();
                a.TransCurrency('Y',1234);
                a.TransCurrency('D',2345);
        }
}

[此贴子已经被作者于2010-12-12 08:24:33编辑过]

 回到顶部