I have been given a task to do for class however we have only been taught to a certain extent and this has left a knowledge gap. we have been asked to complete the remaining code to some we had done in class, the tutor has given a description of what needs doing but not how, and the time since i last did any independent Java work has been about 7 months so i am a bit rusty below is the code that we wrote with the tutors guidance and further below the description of the task.
public class CardHolder implements Runnable {
private int id;
private Account account;
final static int numIterations = 20;
public CardHolder(int id, Account account) {
this.id = id;
this.account = account;
}
/*
* run method is what is executed when you start a Thread that
* is initialised with an instance of this class.
* You will need to add code to keep track of local balance (cash
* in hand) and report this when the thread completes.
*/
public void run() {
for (int i = 0; i < numIterations; i++) {
// Generate a random amount from 1-10
int amount = (int)(Math.random()*10)+1;
// Then with 50/50 chance, either deposit or withdraw it
if (Math.random() > 0.5) {
account.withdraw(id, amount);
} else {
account.deposit(id, amount);
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("THREAD "+ id + " finished");
}
}
import java.util.ArrayList;
public class Account {
private String name;
/**
* @param args
*/
public static void main(String[] args) {
// Check to make sure program has been called with correct number of
// command line arguments
if (args.length != 3) {
System.err.println("Error: program should take exactly three command line arguments:");
System.err.println("\t<No. of accounts> <main acct starting bal.> <backup acct. starting bal.>");
System.exit(0);
}
// And then make sure that those args are all integers
try {
int numCards = Integer.parseInt(args[0]);
Account account = new Account("Main", Integer.parseInt(args[1]));
Account backup = new Account("Backup", Integer.parseInt(args[2]));
// Your code to create and manage the threads should go here.
} catch (NumberFormatException e) {
System.err.println("All three arguments should be integers");
System.err.println("\t<No. of accounts> <main acct starting bal.> <backup acct. starting bal.>");
}
}
// Create an account - initalisation goes in here
public Account(String name, int startingBalance) {
}
// Deposit <amount> into the account
public void deposit(int id, int amount) {
}
// Withdraw <amount> from the account
public void withdraw(int id, int amount) {
}
// Print out the statement of transactions
public void printStatement() {
System.out.println("Account \"" + name + "\":");
}
}
The arguments contained in the run config in eclipse are 50 1000 1000
The Task
Many banking systems allow multiple cardholders to access a single account (e.g. a business
account). The consequence of this is that is possible for more than one person to attempt to
withdraw or deposit money from a single account simultaneously. If the access to the
account is implemented naïvely (ignoring the principles of concurrent programming), this
can lead to race conditions, and the potential for the following fraud to be successful:
Multiple cardholders may collude and attempt to carry out a timing attack. In such
an attack, multiple cardholders withdraw money from the account simultaneously,
with the aim of only one deduction to the account balance being made.
Your task is to write a program (detailed specifications below) that will simulate the
operation of ATM-enabled linked bank accounts with multiple linked cards. In other words,
there will be several individuals who can access each account, each with his/her own card ().
You program should demonstrate the principles of concurrent programming, making it
impossible for the above fraud to be successful
Any help would be greatly appreciated
Thanks