zoukankan      html  css  js  c++  java
  • Algs4-1.3.17从文件中读取信息生成Transaction对象数组

     1.3.17为Transaction类完成练习1.3.16。
     答:public static Transaction[] readTransactions(String name)
    图片
    import java.util.Date;
    public class Transaction
    {
        private final String myWho;
        private final SmartDate myWhen;
        private final double myAmount;
       
        public Transaction(String who,SmartDate when,double amount)
        {
            myWho=who;
            myWhen=when;
            myAmount=amount;
          }
       
        public Transaction(String transaction)
        {
            String[] words=transaction.split(" ");
            myWho=words[0];
            myWhen=new SmartDate(words[1]);
            myAmount=Double.parseDouble(words[2]);
        }
       
        public String who()
        {
            return myWho;
        }
       
        public SmartDate when()
        {
            return myWhen;
        }
       
        public double amount()
        {
            return myAmount;
        }
       
        public boolean equals(Object x)
        {
            if(this==x) return true;
            if(x==null) return false;
            if(this.getClass()!=x.getClass())  return false;
            Transaction that=(Transaction) x;
            if(!this.myWho.equals(that.who())) return false;
            if(!this.myWhen.equals(that.when())) return false;
            if(this.myAmount!=that.amount()) return false;
            return true;
        }
       
        public String toString()
        {
            return myWho+" " +myWhen.toString() +" " +myAmount;
        }
       
       public static Transaction[] readTransactions(String name)
        {
            In in=new In(name);
            Queue<String> q=new Queue<String>();
            while(!in.isEmpty())
                q.enqueue(in.readString());

            int N=q.size()/3;
            Transaction[] a=new Transaction[N];
            for(int i=0;i<N;i++)
               a[i]=new Transaction (q.dequeue()+" " + q.dequeue()+" "+q.dequeue());
               
            return a;
        }
           
        public static void main(String[] args)
        {
             Transaction[] b=Transaction.readTransactions(args[0]);
            for(int i=0;i<b.length;i++)
                StdOut.printf("%s ",b[i].toString());
        }
    }

  • 相关阅读:
    AndroidManifest.xml详细分析
    GoogleMap-------解决不能使用问题
    GoogleMap-------manifest文件配置
    GoogleMap-------Google Play services SDK的下载和配置
    css_兼容IE和FF的写法
    dede如何新建一个ajax服务端输出文件
    js用ajax和不同页面的php互相传值的方法
    js获取多个标签元素的内容,并根据元素的内容修改标签的属性
    网页上记录鼠标的点击次数和一段有用的php代码,自己学习使用
    PHP弹出提示框并跳转到新页面即重定向到新页面
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849480.html
Copyright © 2011-2022 走看看