Hi, I am having to create a bank account program were there are inputs and withdraws from the account. The inputs only come in every so often, and the same with withdraws. For instance weekly wage is 2000, which comes in once a week. But a heating oil cost withdraws £250 every 3 weeks. I have to use multi threading to show the updated balance, and have to use 1 second as equal to 1 week. When I run the program I am not getting the updated balance.
Wage Received should deposit £2000 into account every week (1 second)
Oil Bill should withdraw £250 every 3 weeks (3 seconds)
Food Bill should withdraw £600 every week (1 second)
Electricity Bill should withdraw 50 every week (1 second)
Entertainment Bill should withdraw £400 every week (4 seconds)
package BankAccount; /** * * A small class to handle deposits and withdrawls from a bank account * */ //A bank account has a balance that can be changed by deposits and withdrawals. public class BankAccount { private double balance; //Constructs a bank account with a zero balance. public BankAccount() { balance = 0; } //Constructs a bank account with a given balance. public BankAccount(double initialBalance) { balance = initialBalance; } //Deposits money into the bank account. public void deposit(int addAmount, String name) { double newBalance = balance + addAmount; balance = newBalance; System.out.println("Balance is now " + balance); } //Withdraws money from the bank account. public void withdraw(int takeAmount, String name) { double newBalance = balance - takeAmount; balance = newBalance; System.out.println("Balance is now " + balance); } //Gets the current balance of the bank account. public double getBalance() { return balance; } }
package BankAccount; import java.util.Random ; public class Utility implements Runnable { private int transAmt; private String transType; private String typeOfUtility ; private int howOften; BankAccount bankacc = new BankAccount(); public Utility(String name, int frequency, String type, int amount) { typeOfUtility = name ; howOften = frequency; transType = type; transAmt = amount; } public void run() { //do (true) { try { while(true){ if(transType == "Deposit"){ bankacc.deposit(transAmt, typeOfUtility); } else if (transType == "Withdrawl"){ bankacc.withdraw(transAmt, typeOfUtility); } // System.out.println(typeOfUtility + " going to work for " + workingTime + " milliseconds"); // if deposit call deposit(transamt, typeofutility) // else call withdrawl Thread.sleep(howOften) ; // End of try block catch(InterruptedException ex) { System.out.println(typeOfUtility + " Terminated early") ; } // End of Catch clause } // System.out.println(typeOfUtility + " has finished") ; //} } } }
package BankAccount; import java.lang.Thread ; public class main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here System.out.println("Bank Account Scenario"); // Create each of the threads Thread wageReceived = new Thread(new Utility("Wage Received", 1000, "Deposit", 2000)); Thread oilBill = new Thread(new Utility("Oil Bill", 3000, "Withdrawl", 250)) ; Thread foodBill = new Thread(new Utility("Food Bill", 1000, "Withdrawl", 600)); Thread electricityBill = new Thread(new Utility("Electricity Bill", 1000, "Withdrawl", 50)); Thread entertainmentBill = new Thread(new Utility("Entertainment Bill", 1000, "Withdrawl", 400)); // Get the threads going wageReceived.start() ; oilBill.start() ; foodBill.start(); electricityBill.start(); entertainmentBill.start(); System.out.println("Week is now underway") ; } }
For my output I am not getting the correct balance. When it adds 2000 the balance will be 2000 which is correct, then it should take off food bill and electricity but instead of taking them off the 2000 it will output a minus number.
Anyone got any tips?
Thanks