Package edu.princeton.cs.algs4
Class Transaction
- Object
-
- edu.princeton.cs.algs4.Transaction
-
- All Implemented Interfaces:
Comparable<Transaction>
public class Transaction extends Object implements Comparable<Transaction>
TheTransaction
class is an immutable data type to encapsulate a commercial transaction with a customer name, date, and amount.For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Transaction.HowMuchOrder
Compares two transactions by amount.static class
Transaction.WhenOrder
Compares two transactions by date.static class
Transaction.WhoOrder
Compares two transactions by customer name.
-
Constructor Summary
Constructors Constructor Description Transaction(String transaction)
Initializes a new transaction by parsing a string of the form NAME DATE AMOUNT.Transaction(String who, Date when, double amount)
Initializes a new transaction from the given arguments.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
amount()
Returns the amount of this transaction.int
compareTo(Transaction that)
Compares two transactions by amount.boolean
equals(Object other)
Compares this transaction to the specified object.int
hashCode()
Returns a hash code for this transaction.static void
main(String[] args)
Unit tests theTransaction
data type.String
toString()
Returns a string representation of this transaction.Date
when()
Returns the date of this transaction.String
who()
Returns the name of the customer involved in this transaction.
-
-
-
Constructor Detail
-
Transaction
public Transaction(String who, Date when, double amount)
Initializes a new transaction from the given arguments.- Parameters:
who
- the person involved in this transactionwhen
- the date of this transactionamount
- the amount of this transaction- Throws:
IllegalArgumentException
- ifamount
isDouble.NaN
,Double.POSITIVE_INFINITY
, orDouble.NEGATIVE_INFINITY
-
Transaction
public Transaction(String transaction)
Initializes a new transaction by parsing a string of the form NAME DATE AMOUNT.- Parameters:
transaction
- the string to parse- Throws:
IllegalArgumentException
- ifamount
isDouble.NaN
,Double.POSITIVE_INFINITY
, orDouble.NEGATIVE_INFINITY
-
-
Method Detail
-
who
public String who()
Returns the name of the customer involved in this transaction.- Returns:
- the name of the customer involved in this transaction
-
when
public Date when()
Returns the date of this transaction.- Returns:
- the date of this transaction
-
amount
public double amount()
Returns the amount of this transaction.- Returns:
- the amount of this transaction
-
toString
public String toString()
Returns a string representation of this transaction.
-
compareTo
public int compareTo(Transaction that)
Compares two transactions by amount.- Specified by:
compareTo
in interfaceComparable<Transaction>
- Parameters:
that
- the other transaction- Returns:
- { 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
-
equals
public boolean equals(Object other)
Compares this transaction to the specified object.
-
hashCode
public int hashCode()
Returns a hash code for this transaction.
-
main
public static void main(String[] args)
Unit tests theTransaction
data type.- Parameters:
args
- the command-line arguments
-
-