1.

 package ADT;

 /******************************************************************************
* Compilation: javac Transaction.java
* Execution: java Transaction
* Dependencies: StdOut.java
*
* Data type for commercial transactions.
*
******************************************************************************/ import java.util.Arrays;
import java.util.Comparator; import algorithms.util.StdOut; /**
* The <tt>Transaction</tt> class is an immutable data type to encapsulate a
* commercial transaction with a customer name, date, and amount.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Transaction implements Comparable<Transaction> {
private final String who; // customer
private final Date when; // date
private final double amount; // amount /**
* Initializes a new transaction from the given arguments.
*
* @param who the person involved in this transaction
* @param when the date of this transaction
* @param amount the amount of this transaction
* @throws IllegalArgumentException if <tt>amount</tt>
* is <tt>Double.NaN</tt>, <tt>Double.POSITIVE_INFINITY</tt>,
* or <tt>Double.NEGATIVE_INFINITY</tt>
*/
public Transaction(String who, Date when, double amount) {
if (Double.isNaN(amount) || Double.isInfinite(amount))
throw new IllegalArgumentException("Amount cannot be NaN or infinite");
this.who = who;
this.when = when;
if (amount == 0.0) this.amount = 0.0; // to handle -0.0
else this.amount = amount;
} /**
* Initializes a new transaction by parsing a string of the form NAME DATE AMOUNT.
*
* @param transaction the string to parse
* @throws IllegalArgumentException if <tt>amount</tt>
* is <tt>Double.NaN</tt>, <tt>Double.POSITIVE_INFINITY</tt>,
* or <tt>Double.NEGATIVE_INFINITY</tt>
*/
public Transaction(String transaction) {
String[] a = transaction.split("\\s+");
who = a[0];
when = new Date(a[1]);
double value = Double.parseDouble(a[2]);
if (value == 0.0) amount = 0.0; // convert -0.0 0.0
else amount = value;
if (Double.isNaN(amount) || Double.isInfinite(amount))
throw new IllegalArgumentException("Amount cannot be NaN or infinite");
} /**
* Returns the name of the customer involved in this transaction.
*
* @return the name of the customer involved in this transaction
*/
public String who() {
return who;
} /**
* Returns the date of this transaction.
*
* @return the date of this transaction
*/
public Date when() {
return when;
} /**
* Returns the amount of this transaction.
*
* @return the amount of this transaction
*/
public double amount() {
return amount;
} /**
* Returns a string representation of this transaction.
*
* @return a string representation of this transaction
*/
@Override
public String toString() {
return String.format("%-10s %10s %8.2f", who, when, amount);
} /**
* Compares two transactions by amount.
*
* @param that the other transaction
* @return { a negative integer, zero, a positive integer}, depending
* on whether the amount of this transaction is { less than,
* equal to, or greater than } the amount of that transaction
*/
public int compareTo(Transaction that) {
if (this.amount < that.amount) return -1;
else if (this.amount > that.amount) return +1;
else return 0;
} /**
* Compares this transaction to the specified object.
*
* @param other the other transaction
* @return true if this transaction is equal to <tt>other</tt>; false otherwise
*/
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Transaction that = (Transaction) other;
return (this.amount == that.amount) && (this.who.equals(that.who))
&& (this.when.equals(that.when));
} /**
* Returns a hash code for this transaction.
*
* @return a hash code for this transaction
*/
public int hashCode() {
int hash = 17;
hash = 31*hash + who.hashCode();
hash = 31*hash + when.hashCode();
hash = 31*hash + ((Double) amount).hashCode();
return hash;
} /**
* Compares two transactions by customer name.
*/
public static class WhoOrder implements Comparator<Transaction> { @Override
public int compare(Transaction v, Transaction w) {
return v.who.compareTo(w.who);
}
} /**
* Compares two transactions by date.
*/
public static class WhenOrder implements Comparator<Transaction> { @Override
public int compare(Transaction v, Transaction w) {
return v.when.compareTo(w.when);
}
} /**
* Compares two transactions by amount.
*/
public static class HowMuchOrder implements Comparator<Transaction> { @Override
public int compare(Transaction v, Transaction w) {
if (v.amount < w.amount) return -1;
else if (v.amount > w.amount) return +1;
else return 0;
}
} /**
* Unit tests the <tt>Transaction</tt> data type.
*/
public static void main(String[] args) {
Transaction[] a = new Transaction[4];
a[0] = new Transaction("Turing 6/17/1990 644.08");
a[1] = new Transaction("Tarjan 3/26/2002 4121.85");
a[2] = new Transaction("Knuth 6/14/1999 288.34");
a[3] = new Transaction("Dijkstra 8/22/2007 2678.40"); StdOut.println("Unsorted");
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
StdOut.println(); StdOut.println("Sort by date");
Arrays.sort(a, new Transaction.WhenOrder());
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
StdOut.println(); StdOut.println("Sort by customer");
Arrays.sort(a, new Transaction.WhoOrder());
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
StdOut.println(); StdOut.println("Sort by amount");
Arrays.sort(a, new Transaction.HowMuchOrder());
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
StdOut.println();
} }

算法Sedgewick第四版-第1章基础-004一封装交易对象的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-006一封装输出(文件)

    1. package algorithms.util; /*********************************************************************** ...

  2. 算法Sedgewick第四版-第1章基础-005一封装输入(可以文件,jar包里的文件或网址)

    1. package algorithms.util; /*********************************************************************** ...

  3. 算法Sedgewick第四版-第1章基础-003一封装日期

    1. package ADT; import algorithms.util.StdOut; /**************************************************** ...

  4. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

随机推荐

  1. Eclipse插件开发_学习_00_资源帖

    一.官方资料 1.eclipse api 2.GEF Developer's Guide 二. 精选资料 1.开发 Eclipse 插件 2.Eclipse, RCP, Plugin and OSGi ...

  2. hdoj-1004-Let the Balloon Rise(map排序)

    map按照value排序 #include <iostream> #include <algorithm> #include <cstring> #include ...

  3. 20165210 学习基础和C语言基础调查

    20165210 学习基础和C语言基础调查 一.技能学习过程和心得 读了娄老师<做中学>自己还是深有感受的,对于运动.音乐.棋牌都会一点,我觉得做中学可以概括为三点:做,学,学做结合,所谓 ...

  4. HTML实用案例(1)—— 左侧菜单,右侧内容的布局(带左侧菜单点击隐藏显示效果)

    效果图 代码部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  5. 自动部署基于Maven的war文件到远程Tomcat

    转载自:https://www.cnblogs.com/jtlgb/p/7018705.html Tomcat 7发布URL = http://localhost:8080/manager/text命 ...

  6. 场景中GameObject无法用代码隐藏问题(setActive为false)

    GameObject不受代码控制隐藏问题:代码中若对某个gameobject.setActive(false),发现会不起作用,总结下来发现是和object所在模型组的Animator组件的anima ...

  7. GC(Garbage Collection)垃圾回收机制

    1.在垃圾回收器中,程序员没有执行权,只有通知它的权利. 2.程序员可以通过System.gc().通知GC运行,但是Java规范并不能保证立刻运行. 3.finalize()方法,是java提供给程 ...

  8. webpack 配置简单说几句 ?

    前言 这几天在准备一个单页面应用, 准备试试webpack神器,在准备webpack下的知识点,顺便记录下一些使用的心得. webpack 的配置说明 在近来的前端开发中,业务逻辑复杂化,层次多样化, ...

  9. 如何用nodejs启一个前端服务

    1.新建文件夹,如 notice 2.新建页面和js文件,如 index.html server.js 3.index.html页面内容随你写,如: <!DOCTYPE html> < ...

  10. 自编jQuery插件实现模拟alert和confirm

    现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了自己定制一个的想法...... 啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的al ...