Bitcoin Download

Christina Cacioppo

arraylists, classes, final, compareTo Polish needed


Bitcoin is a new cryptocurrency. It's based on transactions, which are records that record a payment between two parties, and a ledger, which is a list of every payment that's been made.

Bitcoin is similar to cash in that once a bitcoin's been transfered, and the entry is made in the ledger, it can't be reversed or changed: the record will stay there.

We actually know enough Java to implement bitcoin – so let's get to it.


You're given an outline of two classes: Transaction, which stores records in the blockchain, and Ledger, which maintains a list of all the transactions that have occured.

Neither class is quite finished yet; it's up to you to do that. Specifically:

  1. Implement the Transaction constructor and getter, toString, and compareTo methods.
  2. Implement the verify and add methods of the Ledger class.
  3. Bitcoin doesn't allow anyone to remove transactions from the list; you can only add on to the end of it. What can you do to make sure the only way to change the ledger is with the methods you provide?
  4. Finally: in the starter code, Transaction implements Comparable. Why was that useful?

Starter code

class Transaction implements Comparable{
	final String sender;
	final String recipient;
	final double amount;

	/* @param send – sender's name as a String
	 * @param recip – recipient's name as a String
	 * @param amoun – Transaction amount  
     * Constructs a Transaction object
	public Transaction(String send, String recip, double amoun){
		// Construct a transaction object with the given sender, recipient, and amount

	public String getSender(){
		// returns the sender

	public String getRecipient(){
		// returns the recipient

	public double getAmount(){
		// returns the amount

	public String toString(){
		// returns the Transaction object in String form

	public int compareTo(Object o){
		/* Compares two transaction objects and returns:
		   -1 if the first is larger, 0 if they're equal,
		   and 1 if the second is larger.	

class Ledger{
	ArrayList ledger;

	public Ledger(){
		ledger = new ArrayList;

	public boolean verify(Transaction t){
		/* Returns true if Transaction t is in the ledger;
		   returns false otherwise						*/

	public void add(Transaction t){
		/* Adds Transaction t to the ledger array.
		   Make sure to check whether you need to
		   increase the size of the ledge array. */