Cloud Stack Ninja

Main Class

public class bank {
    
        public static void main(String[] args) {
        
            Customer customer1 = new Customer(": A2019273", ": Hussain");
            //customer1.setIdCardNumber(JOptionPane.showInputDialog("Please write id card no", args));
            
            //System.out.println(customer1.toString());
           // customer1.setName(JOptionPane.showInputDialog("Please write customer Name", args));
            
            System.out.println(customer1 .toString());
            
            
    
    
            
                
            
            
            BankAccount CurrentAccount = new BankAccount ("7703475404002", 10000.00, customer1);
    
            System.out.println(CurrentAccount .toString());
            
                   System.out.print("Accunt balance = ");
                  // System.out.println("MVR" + account.getBalance());
                   
                 
    
                   
                   
                }
    
        }

BankAccount Class

 public class BankAccount {
    
        
        
        private String AccountNumber;
        public double AccountBalance;
        private Customer customer;
        
        
        
    
    
    
        public BankAccount(String accountNumber, double accountBalance, Customer customer) {
            
            this.AccountNumber = accountNumber;
            this.AccountBalance = accountBalance;
            this.customer = customer;
        }
    
    
    
        public String getAccountNumber() {
            return AccountNumber;
        }
    
    
    
        public void setAccountNumber(String accountNumber) {
            AccountNumber = accountNumber;
        }
    
    
    
        public double getAccountBalance() {
            return AccountBalance;
        }
    
    
    
        public void setAccountBalance(double accountBalance) {
            AccountBalance = accountBalance;
        }
    
    
    
        public Customer getCustomer() {
            return customer;
        }
    
    
    
        public void setCustomer(Customer customer) {
            this.customer = customer;
        }
    
    
    
        public synchronized void deposit(double amt) {
              double temp = AccountBalance;
              temp = temp + amt;
              try {
                 Thread.sleep(300); // simulate production time
              } catch (InterruptedException ie) {
                 System.err.println(ie.getMessage());
              }
              System.out.println("after deposit balance = MVR" + temp);
              AccountBalance = temp;
              notify();
           }
           public synchronized void withdraw(double amt) {
              while (AccountBalance< amt) {
                 try {
                    wait(); // wait for funds
                 } catch (InterruptedException ie) {
                    System.err.println(ie.getMessage());
                 }
              }
              double temp = AccountBalance;
              temp = temp - amt;
              try {
                 Thread.sleep(200); // simulate consumption time
              } catch (InterruptedException ie) {
                 System.err.println(ie.getMessage());
              }
              System.out.println("after withdrawl balance = MVR" + temp);
              AccountBalance = temp;
           }
    
    
    
    
            @Override
            public String toString() {
                
                return  "BankAccounnt{" + "AccountNumber = " + AccountNumber + ", accountblance=" + AccountBalance + "customer= " + customer.toString() + ')';
                
            }
         
        
        
    }

Customer Class

public class Customer extends Thread {
private String idCardNumber;
private String Name;



public Customer(String idCardNumber, String name) {
    
    this.idCardNumber = idCardNumber;
    this.Name = name;
}



public String getIdCardNumber() {
    return idCardNumber;
}



public void setIdCardNumber(String idCardNumber) {
    this.idCardNumber = idCardNumber;
}






@Override
public String toString() {
    
    return "Customer (" + "idcardnumber "+ idCardNumber +", CustomerName" + Name + ')';
    
}
private BankAccount account;
public void run() {
    for(int i = 0; i < 5; i++) {
       account.withdraw(10);
    }
 }


}

Producer Class

class Producer extends Thread {
       private BankAccount account;
       public Producer(BankAccount acct) { account = acct; }
       public void run() {
          for(int i = 0; i < 1; i++) {
             account.withdraw(10);
             account.deposit(100);
          }
         
       }
    }
                      

I am doing an assignment in Java multi-threading and my Lecturer said Create a method called carryOutTransactions() in the Transaction class, that does the following: • Retrieve account balance and assign to balance variable • Modify account balance by deducting the withdrawal amount, and adding the deposit amount
In the run() method of the Transaction class, the method carryOutTransactions() will be called.


In the main function of your project, carry out the following:

• Create an example customer with a name and ID number • Create a bank account for this customer. • Create two transactions: i. Withdraw $ 10 from the customer’s account ii. Deposit $ 100 to customer’s account • Create two threads to carry out these two transactions.

how do I do that?




Read more here: https://stackoverflow.com/questions/64395308/i-am-making-a-java-multi-thread-application

Content Attribution

This content was originally published by Ali Shareef at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: