以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  Java程序语言课件  (http://www.njcie.com/bbs/list.asp?boardid=17)
----  [推荐]第三次上机作业的说明——货币处理  (http://www.njcie.com/bbs/dispbbs.asp?boardid=17&id=474)

--  作者:admin
--  发布时间: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编辑过]