give me a better version of code with proper comments added on each line and ple
ID: 3869107 • Letter: G
Question
give me a better version of code with proper comments added on each line and please tell me what is wrong with my code
//I have to use synchronized keyword everything according to my point of view is running correct but don't know why customer 2 get executed before customer 1 when withdraw method is called can any-one help me.I want to know why marry is called always first and how to fix it
import java.util.Scanner;
public class Account {
private int balance;
public Account(int balance) {
this.balance = balance;
}
public boolean isSufficientBalance(int withAmt) {
if (withAmt <= balance) {
return (true);
} else {
return (false);
}
}
//deposit method
synchronized void deposit(int depAmt) {
this.balance = depAmt + balance;
System.out.println("Deposit sucessful");
System.out.println("New balance is " + this.balance);
}
// this is my withdraw method
synchronized void withdraw(int witAmt) {
this.balance
= balance - witAmt;
System.out.println("Withdraw sucessfully");
System.out.println("Your updated balance is" + this.balance);
System.out.println("ThankYou");
}
}
class Customer implements Runnable {
private String name;
private Account account;
public Customer(Account account, String n) {
this.account = account;
name = n;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(name + " Enter the amount to withdraw");
int witAmt = sc.nextInt();
synchronized (account) {
if (account.isSufficientBalance(witAmt)) {
account.withdraw(witAmt);
} else {
System.out.println("Insufficient balance to make this transaction");
}
}
Scanner sc1 = new Scanner(System.in);
System.out.println(name + " Enter the amount to deposit");
int depAmt = sc1.nextInt();
account.deposite(depAmt);
{
}
}
}
public static void main(String[] args) {
Account ac = new Account(1000);
Customer cr = new Customer(ac, " jhon");
Customer cr1 = new Customer(ac, " marry");
Thread t1 = new Thread(cr);
Thread t2 = new Thread(cr1);
try {
t1.join(100);
t2.join(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t1.start();
t2.start();
}
}
Explanation / Answer
import java.util.Scanner;
public class Account {
private int balance;
public Account(int balance) {
this.balance = balance;
}
public boolean isSufficientBalance(int withAmt) {
if (withAmt <= balance) {
return (true);
} else {
return (false);
}
}
synchronized void deposit(int depAmt) {
this.balance = depAmt + balance;
System.out.println("Deposit sucessful");
System.out.println("New balance is " + this.balance);
}
synchronized void withdraw(int witAmt) {
this.balance
= balance - witAmt;
System.out.println("Withdraw sucessfully");
System.out.println("Your updated balance is" + this.balance);
System.out.println("ThankYou");
}
}
class Customer implements Runnable {
private String name;
private Account account;
public Customer(Account account, String n) {
this.account = account;
name = n;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
System.out.println(name + " Enter the amount to withdraw");
int witAmt = sc.nextInt();
synchronized (account) {
if (account.isSufficientBalance(witAmt)) {
account.withdraw(witAmt);
} else {
System.out.println("Insufficient balance to make this transaction");
}
}
Scanner sc1 = new Scanner(System.in);
System.out.println(name + " Enter the amount to deposit");
int depAmt = sc1.nextInt();
account.deposite(depAmt);
{
}
}
}
public static void main(String[] args) {
Account ac = new Account(1000);
Customer cr = new Customer(ac, " jhon");
Customer cr1 = new Customer(ac, " marry");
Thread t1 = new Thread(cr);
Thread t2 = new Thread(cr1);
try {
t1.join(100);
t2.join(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.start();
t2.start();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.