So I have a task to create a program, that has 1 bank account with a starting balance. Then I create a set of cards that can access that account, deposit or withdraw an amount, but not at the same time. I have created a number of threads and then use these threads to access the bank account, withdraw or deposit a random amount, then exit.
My card class:
public class card implements Runnable {
private int id;
private account account;
private int count = 1;
card(int id, account acc1){
this.id = id;
this.account = acc1;
}
public int getid(){
return this.id;
}
public int localBalance(){
return account.getBalance();
}
public void run() {
for(count = 0; count <= 5; count++) { //withdraw and deposit random amounts
int transactionamount = (int)(Math.random()*10);
if (Math.random() > 0.5) {
System.out.printf("%-12s%-12s%-12s%-12s\n","card id:(" + getid() + ")"," ",transactionamount, account.getBalance());
account.deposit(transactionamount);
} else {
System.out.printf("%-12s%-12s%-12s%-12s\n","card id:(" + getid() + ")",transactionamount," ",account.getBalance());
account.withdraw(transactionamount);
}
}
}
}
my account class:
public class account {
private int balance = 5000;
public account(int balance){
this.balance = balance;
}
public synchronized void withdraw(int amount) {
balance = getBalance() - amount;
}
public synchronized void deposit(int amount) {
balance = getBalance() + amount;
}
public synchronized int getBalance() {
return this.balance;
}
public synchronized void setNewBalance(int localBalance) { //no longer in use
balance = localBalance;
}
}
my program class
public class Program {
public static void main(String[] args ) throws InterruptedException {
// TODO Auto-generated method stub
int numberofcards = 5;
int accountbalance = 5000;
card cardArray[] = new card[numberofcards];
account acc1 = new account(accountbalance); //pass into account the starting balance.
System.out.printf("\n%-12s%-12s%-12s%-12s\n","Transaction","Withdrawal","Deposit","Balance");
System.out.printf("%-12s%-12s%-12s%-12s\n"," "," "," ",accountbalance + "\n");
Thread[] threads = new Thread[numberofcards];
for(int i = 1; i < numberofcards; i++ ){
cardArray[i] = new card(i, acc1);
threads[i] = new Thread(cardArray[i]);
threads[i].start();
}
for(int i = 1; i < threads.length; i++ ){
threads[i].join();
}
System.out.printf("\n%-12s%-12s%-12s%-12s\n","finished"," "," ","Final Balance: " + acc1.getBalance());
}
}
example of output:
http://puu.sh/lvoE5/05ebcd4c74.png
As you can see, it works however for each different card it goes back to the 5000 and then deposits or withdraws a random amount. I cant seem to figure out why my code doesnt constantly update the amount. Any help would be amazing, im amazed ive got this far by myself anyway.
edit: got rid of the not needed new thread .join code & got rid of synchronized in my run method, output is still the same however, please keep pointing out the problems though, teaching myself about threads is really difficult so ill remember. thank you in advance
edit 2: still not working, directly using withdraw and deposit now. output attached in link