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. SQL Server 索引中include

    SQL Server 索引中include的魅力(具有包含性列的索引) http://www.cnblogs.com/gaizai/archive/2010/01/11/1644358.html 开文 ...

  2. Apache Kafka:下一代分布式消息系统【转载】

    简介 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和可复制的提交 ...

  3. web.xml & web-fragment.xml (Servlet 2.3, 2.4, 2.5 + 3.0)模板

    转自:http://jlcon.iteye.com/blog/890964 web.xml v2.3 <?xml version="1.0" encoding="I ...

  4. idea 创建maven工程(入门)

    转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...

  5. C#获取路由器外网IP,MAC地址

    C#实现的获取路由器MAC地址,路由器外网地址.对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码.至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密 ...

  6. 试用 Eagle 9.1

    试用 Eagle 9.1 有推挤功能 原理图可以设置组装变体. 输出的 CAM 可以自定义,没有 Protel 那么死板. 保存的文件是 xml 文件,可以自由解析.

  7. bzoj 4589 Hard Nim——FWT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4589 一开始异或和为0的话先手必败.有 n 堆,每堆可以填那些数,求最后异或和为0的方案数, ...

  8. postgresql 主从复制并切换

    1 环境 192.168.19.145 Kylin 3.3 mysqlhq  9.5.2  psql_master192.168.19.227 Kylin 3.3 mysql3    9.5.2  p ...

  9. Celery-4.1 用户指南: Task(任务)

    任务是构建 celery 应用的基础块. 任务是可以在任何除可调用对象外的地方创建的一个类.它扮演着双重角色,它定义了一个任务被调用时会发生什么(发送一个消息),以及一个工作单元获取到消息之后将会做什 ...

  10. 2015广州强网杯(Misc)

    单身狗: 下载图片 被一只狗挡住了的二维码,用图片处理软件把上面两个正方形随便一个覆盖狗的地方 我直接用美图秀秀处理一下,扫一下就得到flag