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