At MKO Bank, a customer should be able to deposit money, withdraw money and check his or her balance. You have been approached to develop a software for MKO Bank. Required: a. Create a class called Customer The balance of the customer should be 1500 b. Create methods/functions for the deposit, withdrawal and checking of balance transactions. Use appropriate data members to help you accomplish this. c. Create another class called Bank with the main function. d. Declare an instance of the Customer class called Cust. e. Use the Cust object to check the current balance f. Use JOptionPane to allow a user enter a deposit. The program should display the new balance. g. Use JOptionPane to allow a user withdraw an amount. The program should display the new balance.
Hi Friend,
Try the following code:
import javax.swing.*; class Customer{ int bal; Customer(int bal) { this.bal = bal; } int deposit(int amt) { if (amt < 0) { System.out.println("Invalid Amount"); return 1; } bal = bal + amt; return 0; } int withdraw(int amt) { if (bal < amt) { System.out.println("Not sufficient balance."); return 1; } if (amt < 0) { System.out.println("Invalid Amount"); return 1; } bal = bal - amt; return 0; } void check() { JOptionPane.showMessageDialog(null,"Balance:" + Integer.toString(bal)); } } public class Bank{ public static void main(String[]args){ Customer Cust=new Customer(1500); String st1=JOptionPane.showInputDialog(null,"Enter the amount to deposit:"); int dep=Integer.parseInt(st1); int bal1=Cust.deposit(dep); Cust.check(); String st2=JOptionPane.showInputDialog(null,"Enter the amount to withdraw:"); int with=Integer.parseInt(st2); int bal2=Cust.withdraw(with); Cust.check(); } }
Thanks