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

  • 相关阅读:
    表单输入框黏贴自动过滤转义实例
    前端性能优化成神之路-浏览器缓存机制详解
    前端性能优化成神之路-HTTP压缩开启gzip
    前端性能优化成神之路—重绘与回流
    前端性能优化成神之路—图片相关的优化
    ES5-ES6-ES7_集合Set和Map
    ES5-ES6-ES7_解构赋值
    ES5-ES6-ES7_函数的扩展
    ES5-ES6-ES7_数值的扩展
    ES5-ES6-ES7_字符串与JOSN格式的数据相互转换以及深度克隆新对象
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849480.html
Copyright © 2011-2022 走看看