首页 > 其他 > 详细

Algs4-1.2.13实现Transaction类型

时间:2018-10-25 13:06:30      阅读:138      评论:0      收藏:0      [点我收藏+]
1.2.13用我们对Date的实现(请见表1.2.12)作为模板实现Transaction类型。
答:
技术分享图片
code:
import java.util.Date;
public class Transaction
{
    private final String myWho;
    private final Date myWhen;
    private final double myAmount;
   
    public Transaction(String who,Date when,double amount)
    {
        myWho=who;
        myWhen=when;
        myAmount=amount;
      }
   
    public Transaction(String transaction)
    {
        String[] words=transaction.split("  ");
        myWho=words[0];
        myWhen=new Date(words[1]);
        myAmount=Double.parseDouble(words[2]);
    }
   
    public String who()
    {
        return myWho;
    }
   
    public Date when()
    {
        return myWhen;
    }
   
    public double amount()
    {
        return myAmount;
    }
   
    public static void main(String[] args)
    {
        Transaction t1=new Transaction("Jack",new Date("19/9/2016 13:00:00"),1000000);
        StdOut.printf("t1.who=%s,when=%s,amount=%.2f\n",t1.who(),t1.when(),t1.amount());
       
        Transaction t2=new Transaction("LiLi  19/9/2016 13:25:00  1000002");
        StdOut.printf("\nt2.who=%s,when=%s,amount=%.2f\n",t2.who(),t2.when(),t2.amount());
    }
}


Algs4-1.2.13实现Transaction类型

原文:https://www.cnblogs.com/longjin2018/p/9848910.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!