Algs4-1.2.13实现Transaction类型

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());
    }
}


转载于:https://www.cnblogs.com/longjin2018/p/9848910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值