Package dsa
Class Transaction
java.lang.Object
dsa.Transaction
- All Implemented Interfaces:
Comparable<Transaction>
An immutable data type to represent a commercial transaction with a name, date, and amount.
-
Constructor Summary
ConstructorDescriptionConstructs a transaction from a strings
of the form"name date amount"
.Transaction
(String name, Date date, double amount) Constructs a transaction from aname
,date
, andamount
. -
Method Summary
Modifier and TypeMethodDescriptiondouble
amount()
Returns the amount of this transaction.int
compareTo
(Transaction other) Returns a comparison of this transaction withother
, by amount.date()
Returns the date of this transaction.static Comparator
<Transaction> Returns a comparator for comparing two transactions by date.int
hashCode()
Returns a hash code for this transaction.static void
Unit tests the data type.name()
Returns the name of the person involved in this transaction.static Comparator
<Transaction> Returns a comparator for comparing two transactions by name.toString()
Returns a string representation of this transaction.
-
Constructor Details
-
Transaction
Constructs a transaction from aname
,date
, andamount
.- Parameters:
name
- name of the person involved in this transaction.date
- date of this transaction.amount
- amount of this transaction.
-
Transaction
Constructs a transaction from a strings
of the form"name date amount"
.- Parameters:
s
- the string.
-
-
Method Details
-
name
Returns the name of the person involved in this transaction.- Returns:
- the name of the person involved in this transaction.
-
date
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.
-
hashCode
public int hashCode()Returns a hash code for this transaction. -
toString
Returns a string representation of this transaction. -
compareTo
Returns a comparison of this transaction withother
, by amount.- Specified by:
compareTo
in interfaceComparable<Transaction>
- Parameters:
other
- the other transaction.- Returns:
- a comparison of this transaction with
other
, by amount.
-
nameOrder
Returns a comparator for comparing two transactions by name.- Returns:
- a comparator for comparing two transactions by name.
-
dateOrder
Returns a comparator for comparing two transactions by date.- Returns:
- a comparator for comparing two transactions by date.
-
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-