Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class Bank {
- public static void main(String[] args) {
- MethodClass methodClass = new MethodClass();
- methodClass.createAccount();
- methodClass.transaction();
- }
- }
- import java.util.Random;
- import javax.swing.JOptionPane;
- public abstract class BankAccount {
- private String memberName, accountNumber;
- private double accountBalance, minimumBalance;
- public double getAccountBalance() {
- return accountBalance;
- }
- public void setAccountBalance(double accountBalance) {
- this.accountBalance = accountBalance;
- }
- public double getMinimumBalance() {
- return minimumBalance;
- }
- public void setMinimumBalance(double minimumBalance) {
- this.minimumBalance = minimumBalance;
- }
- public BankAccount(String n, double b, double minB){
- memberName = n;
- accountBalance = b;
- minimumBalance = minB;
- Random rand = new Random();
- accountNumber ="" + rand.nextInt(10) + rand.nextInt(10)+ rand.nextInt(10)+ rand.nextInt(10)+ rand.nextInt(10);
- }
- public void deposit(double amount) {
- accountBalance += amount;
- }
- public abstract void withdraw(double amount);
- public double getBalance() {
- return accountBalance;
- }
- public void display()
- {
- System.out.printf("Name:%s, Account Number:%s, Balance:%.2f\n", memberName, accountNumber, accountBalance);
- }
- }
- import javax.swing.*;
- public class CurrentAccount extends BankAccount{
- public String tradeLicenseNumber;
- CurrentAccount(String n, double b, String tradeLicenseNumber ){
- super(n,b,5000.00);
- this.tradeLicenseNumber = tradeLicenseNumber;
- }
- @Override
- public void withdraw(double amount) {
- if(super.getAccountBalance()-amount>super.getMinimumBalance()){
- setAccountBalance(super.getAccountBalance()-amount);
- }
- else
- JOptionPane.showMessageDialog(null, "You do not have enough to withdraw");
- }
- }
- import javax.swing.*;
- public class SavingsAccount extends BankAccount {
- public int interest = 5;
- public double maxWithLimit;
- SavingsAccount(String n, double b, double maxWithLimit){
- super(n,b,2000.00);
- this.maxWithLimit = maxWithLimit;
- }
- public double getBalance(){
- double interestAddBalance ;
- interestAddBalance = getOriginalBalance()*(interest/100.0);
- return interestAddBalance+super.getBalance();
- }
- public double getOriginalBalance(){
- return super.getBalance();
- }
- public void withdraw(double amount) {
- if (amount < this.maxWithLimit) {
- if(super.getAccountBalance()-amount>super.getMinimumBalance()){
- setAccountBalance(getAccountBalance()-amount);
- }
- else
- JOptionPane.showMessageDialog(null, "You do not have enough to withdraw");
- }
- }
- }
- public class StudentAccount extends SavingsAccount {
- public String institutionName;
- StudentAccount(String n, double b, String institutionName) {
- super(n,b,20000);
- this.institutionName = institutionName;
- }
- }
- import javax.swing.*;
- public class MethodClass {
- String msg = "Please Enter \n 1 to create Savings account\n 2 for Current Account \n 3 or Student Account.";
- String type = null;
- boolean isValidtype = false;
- BankAccount account = null;
- public void method(){
- createAccount();
- transaction();
- }
- public void transaction(){
- msg = "Please enter what type of transaction you want to perform: \ndeposit(d), withdraw(w), check balance(c) or exit.";
- String trans = JOptionPane.showInputDialog(msg).replace(" ", "").toLowerCase();
- isValidtype = trans.equals("d") || trans.equals("w") || trans.equals("c");
- boolean wantToQuit = trans.equals("exit");
- Outer:
- while(!wantToQuit){ // loop until user enter "exit"
- while(!isValidtype) // loop until a valid option enter
- {
- String updMsg = "Not a valid Option. \n" + msg;
- trans = JOptionPane.showInputDialog(updMsg).replace(" ", "").toLowerCase();
- isValidtype = trans.equals("d") || trans.equals("w") || trans.equals("c");
- if (trans.equals("exit"))
- break Outer; // Or System.exit(0);
- }
- double amt;
- String temp;
- double initialbalance=type.equals("2")?account.getBalance():((SavingsAccount)account).getOriginalBalance();
- double newBalance;
- switch(trans){
- case "d": // deposit
- temp = JOptionPane.showInputDialog("Enter the amount you want ot deposit:");
- amt = Double.parseDouble(temp);
- account.deposit(amt);
- newBalance=type.equals("2")?account.getBalance():((SavingsAccount)account).getOriginalBalance();
- if(initialbalance != newBalance)
- JOptionPane.showMessageDialog(null, String.format("Balance before deposit:%.2f, after deposit:%.2f\n", initialbalance, newBalance));
- break;
- case "w": // withdraw
- temp = JOptionPane.showInputDialog("Enter the amount you want ot withdraw:");
- amt = Double.parseDouble(temp);
- account.withdraw(amt);
- newBalance=type.equals("2")?account.getBalance():((SavingsAccount)account).getOriginalBalance();
- if(initialbalance != newBalance)
- JOptionPane.showMessageDialog(null, String.format("Balance before withdraw:%.2f, after withdraw:%.2f\n", initialbalance, newBalance));
- break;
- case "c": // check balance
- if (type.equals("2"))
- JOptionPane.showMessageDialog(null, "Your balance:" + account.getBalance());
- else
- JOptionPane.showMessageDialog(null, "Your Balance:" + ((SavingsAccount)account).getOriginalBalance() + " and Balance with interest:"+ String.format("%.2f",((SavingsAccount)account).getBalance()));
- break;
- }
- trans = JOptionPane.showInputDialog(msg).replace(" ", "").toLowerCase();
- isValidtype = trans.equals("d") || trans.equals("w") || trans.equals("c");
- wantToQuit = trans.equals("exit");
- }
- }
- public void createAccount(){
- // Step 1: Create account
- // Ask user for the type of account he wants to create
- do{
- type = JOptionPane.showInputDialog(msg);
- isValidtype = type.equals("1") || type.equals("2") || type.equals("3");
- msg = "Not a valid Option. /n Please enter 1 to create Savings account, 2 for Current Account\n 3 or Student Account.";
- }while(!isValidtype);
- // Enter info needed to create a specific type of account
- String name = JOptionPane.showInputDialog("Enter your name:");
- String initBalance = JOptionPane.showInputDialog("Enter your balance:");
- double balance = Double.parseDouble(initBalance);
- switch(type){
- case "1": // Saving account
- while(balance < 2000){
- initBalance = JOptionPane.showInputDialog("Enter your balance (minimum 2000 tk):");
- balance = Double.parseDouble(initBalance);
- }
- double maxWithLimit = Double.parseDouble(JOptionPane.showInputDialog("Enter your maximum withdraw limit:"));
- account = new SavingsAccount(name, balance, maxWithLimit);
- break;
- case "2": // Current account
- while(balance < 5000){
- initBalance = JOptionPane.showInputDialog("Enter your balance (minimum 5000 tk):");
- balance = Double.parseDouble(initBalance);
- }
- String trLncNum = JOptionPane.showInputDialog("Enter your trade License Number:");
- account = new CurrentAccount(name, balance, trLncNum);
- break;
- case "3": //Student account
- while(balance < 100){
- initBalance = JOptionPane.showInputDialog("Enter your balance (minimum 100 tk):");
- balance = Double.parseDouble(initBalance);
- }
- String univName = JOptionPane.showInputDialog("Enter your educational institution's name:");
- account = new StudentAccount(name, balance, univName);
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment