Session 4 - Blockchain Data Structures

In this session, after making a quick recap of Session 3, we continue understanding blockchain systems.

Simple Standalone Bank Application in Java (continued)

During Session 3, you have written a simple standalone (working only in one computer) bank application in Java.

A possible solution can be as the following.

The Account class:

private final String firstName;
private final String lastName;
private double balance;
private static int uid = 0;

public Account(String firstName, String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
    this.balance = 100.0;
    this.uid++;
}

public String getFirstName(){
    return this.firstName;
}
public String getLastName(){
    return this.lastName;
}
public double getBalance(){
    return this.balance;
}
public void setBalance(double newBalance) {
    this.balance = newBalance;
}
public int getID(){
    return this.uid;
}

@Override
public String toString(){
    return "Name: " + getFirstName() + "\n" +
            "Last name: " +getLastName() +"\n" +
            "Balance: " + getBalance() + "\n" +
            "ID: " + getID();
}

The Bank class:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

private final List<Account> bankAccounts;
private final Scanner sc;
private double totalRevenue;

public Bank() {
    bankAccounts = new ArrayList<>();
    sc = new Scanner(System.in);
    totalRevenue = 0.0;
}

public Account get(int accountID) {
    for (Account account : bankAccounts) {
        if (account.uid == accountID) {
            return account;
        }
    }
    System.out.println("One of the details is incorrect");
    return null;
}

public boolean addAccount(String firstName, String lastName, double initialBalance) {
    for (Account account : bankAccounts) {
        if (account.getFirstName().equals(firstName) && account.getLastName().equals(lastName)) {
            return false;
        }
    }
    
    Account newAccount = new Account(firstName, lastName);
    newAccount.setBalance(initialBalance);
    bankAccounts.add(newAccount);
    return true;
}

public void registerAccount() {
    System.out.println("First name?");
    String firstName = sc.next();
    System.out.println("Last name?");
    String lastName = sc.next();
    System.out.println("Initial balance?");
    String initialBalance = sc.next();
    
    addAccount(firstName, lastName, Double.parseDouble(initialBalance));
}

public void transferMoney(int fromAccountID, int toAccountID, double amount, double fee){
    Account fromAccount = get(fromAccountID);
    Account toAccount = get(toAccountID);
    
    if(fromAccount.getBalance() >= amount+fee) {
        toAccount.setBalance(toAccount.getBalance() + amount);
        fromAccount.setBalance(fromAccount.getBalance() - amount - fee);
        totalRevenue += fee;
    } else {
        System.out.println(fromAccount.getFirstName() + " does not have enough funds.");
    }
}

Part II

Modify your project using the Transaction and UUIDAddress classes found in the max.datatype.ledger and max.datatype.com projects.

  • Use UUIDAddress for representing account id,
  • Use Transaction to transform transferMoney(Account fromAccountID, Account toAccountID, double amount, double fee) into transferMoney(Transaction transaction).

Part III

After finishing Part II successfully, do the following modifications:

  • Remove the balance field from the Account class,
  • Keep transactions in an array list called confirmedTransactions inside the Bank class and
  • Update the getBalance() method accordingly to calculate the balance using confirmed transactions.

Hint: You need to move the getBalance() method into the Bank class and you do not need the setBalance() method anymore.

Part IV

Modify your project using the Block and Blockchain classes found in the max.datatype.ledger project.

  • Keep unconfirmed transactions in an array list called unconfirmedTransactions,
  • Keep confirmed transactions in the blockchain,
  • When transactions are received, they will be put inside unconfirmedTransactions and if there are 3 transactions in this list, a new block with these transaction will be created, and then this block will be added to the blockchain.

Do not forget to remove the transaction you put inside the blockchain from the unconfirmedTransactions list.

  • Update the getBalance() method accordingly to calculate the balance using the blockchain.

Part V

In the Main class, rather than creating one Bank, now create two Bank objects.

  • Add functionality to choose which bank to use for the money transfer.
  • Add functionality to update the transactions and blocks between banks.

When a user makes a money transfer from one bank, and after a second user uses the other bank to show the balances, the second user should be able to see the synchronized updated balance value.

Final

Great! If you have made so far, it means that you have implemented a basic blockchain system. Congratulations.

  • Prepare a presentation explaining how you made your project together with the difficulties you encountered.
  • Make a demo about how your application is used.
Previous