Write a Java program. Create a BankAccount Class with balance (double) field. Ad
ID: 3851399 • Letter: W
Question
Write a Java program. Create a BankAccount Class with balance (double) field. Add two methods (1) deposit(double amount) and (2) withdraw(double amount).
Create two Runnable classes (1) Deposit and (2)Withdraw.
Write a BankAccountTest prorgam (with main() method) that will use the above classes and ask user to choose Deposit or Withdrawal and the amount or exit program.
Deposit and Withdrawal tasks MUST BE DONE in different threads.
Make sure that the program is thread-safe and one will not overwrite the other in performing the task. You can make the threads sleep between tasks for a random amount of time between 0 and 5000 millisecs.
After each taks display the balance to show that the balance correct.
Sample output is shown below.
---------Correct Sample Output -------------
Depositing 100.0 -> balance = 100.0
Withdrawing 100.0 -> balance = 0.0
Depositing 100.0 -> balance = 100.0
Depositing 100.0 -> balance = 200.0
Depositing 100.0 -> balance = 300.0
Depositing 100.0 - balance = 400.0
Withdrawing 50.0 -> balance = 350.0
Withdrawing 100.0 -> balance = 250.0
Withdrawing 100.0 -> balance = 150.0
Depositing 100.0 -> balance = 250.0
Terminating.....
--------
Explanation / Answer
import java.util.Scanner;
class bankInternal {
float bal=0;
void deposit()
{
float amount;
System.out.println("Enter Amount to be Deposited:");
amount = get.nextFloat();
bal = bal+amount;
System.out.println("Deposited! Account Balance is "+bal);
}
void withdraw()
{
float amount;
System.out.println("Enter Amount to be Withdrawn:");
amount = get.nextFloat();
if(amount<bal)
{
bal = bal-amount;
System.out.println("Amount Withdrawn!! Available Balance: "+bal);
}
else
{
System.out.println("Insufficient funds!!");
}
}
}
public class Bank {
public static void main(String[] args)
{
bankInternal myObj = new bankInternal();
myObj.deposit();
myObj.withdraw();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.