Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Bank {
- public static void main(String[] args) {
- BankAccount account = null;
- Scanner scan = new Scanner(System.in);
- // Step 1: Create account
- 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;
- do{
- System.out.println(msg);
- type = scan.nextLine();
- 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);
- System.out.println("Enter your name:");
- String name = scan.nextLine();
- System.out.println("Enter Initial Balance:");
- String initBalance = scan.nextLine();
- double balance = Double.parseDouble(initBalance);
- switch(type){
- case "1":
- while(balance < 2000){
- System.out.println("Enter your balance (minimum 2000 tk):");
- initBalance = scan.nextLine();
- balance = Double.parseDouble(initBalance);
- }
- System.out.println("Enter your maximum withdraw limit:");
- double maxWithLimit = Double.parseDouble(scan.nextLine());
- account = new SavingAccount(name, balance, maxWithLimit);
- break;
- case "2":
- while(balance < 5000){
- System.out.println("Enter your balance (minimum 5000 tk):");
- initBalance = scan.nextLine();
- balance = Double.parseDouble(initBalance);
- }
- System.out.println("Enter your trade License Number:");
- String trLncNum = scan.nextLine();
- account = new CurrentAccount(name, balance, trLncNum);
- break;
- case "3":
- while(balance < 100){
- System.out.println("Enter your balance (minimum 100 tk):");
- initBalance = scan.nextLine();
- balance = Double.parseDouble(initBalance);
- }
- System.out.println("Enter your educational institution's name:");
- String univName = scan.nextLine();
- account = new StudentAccount(name, balance, univName);
- break;
- }
- // Transaction process here
- msg = "Please enter what type of transaction you want to perform: \ndeposit(d), withdraw(w), check balance(c) or exit.";
- System.out.println(msg);
- String selection = scan.nextLine().trim().toLowerCase();
- isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
- boolean Exit = selection.equals("exit");
- Outer:
- while(!Exit){
- while(!isValidtype)
- {
- String updMsg = "Not a valid Option. \nPlease enter what type of transaction you want to perform: \ndeposit(d), withdraw(w), check balance(c) or exit.";
- System.out.println(updMsg);
- selection = scan.nextLine().toLowerCase();
- isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
- if (selection.equals("exit"))
- break Outer;
- }
- double amt;
- String temp;
- double initialbalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
- double newBalance;
- switch(selection){
- case "d":
- System.out.println("Enter the amount you want to deposit:");
- temp = scan.nextLine();
- amt = Double.parseDouble(temp);
- account.deposit(amt);
- newBalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
- if(initialbalance != newBalance)
- System.out.printf("Balance before deposit:%.2f, after deposit:%.2f\n", initialbalance, newBalance);
- break;
- case "w":
- System.out.println("Enter the amount you want ot withdraw:");
- temp = scan.nextLine();
- amt = Double.parseDouble(temp);
- account.withdraw(amt);
- newBalance=type.equals("2")?account.getBalance():((SavingAccount)account).getOriginalBalance();
- if(initialbalance != newBalance)
- System.out.printf("Balance before withdraw:%.2f, after withdraw:%.2f\n", initialbalance, newBalance);
- break;
- case "c":
- if (type.equals("2"))
- System.out.println("Your balance:" + account.getBalance());
- else
- System.out.printf("Your Balance:%.2f and Balance with interest:%.2f\n",((SavingAccount)account).getOriginalBalance(), account.getBalance());
- break;
- }
- System.out.println(msg);
- selection = scan.nextLine().toLowerCase();
- isValidtype = selection.equals("d") || selection.equals("w") || selection.equals("c");
- Exit = selection.equals("exit");
- }
- }
- }
- // BankAccount
- import java.util.Random;
- public class BankAccount {
- private String memberName, accountNumber;
- private double accountBalance, 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 void withdraw(double amount){
- if(accountBalance-amount>minimumBalance)
- accountBalance -= amount;
- else
- System.out.println("You do not have enough to withdraw");
- }
- public double getBalance() {
- return accountBalance;
- }
- public void display()
- {
- System.out.printf("Name:%s, Account Number:%s, Balance:%.2f\n", memberName, accountNumber, accountBalance);
- }
- }
- //StudentAccount
- public class StudentAccount extends SavingAccount {
- private String uniName;
- public StudentAccount(String name, double balance, String university) {
- super(name, balance, 100, 20000);
- uniName = university;
- }
- }
- //CurrentAccount
- public class CurrentAccount extends BankAccount{
- private String tradeLicenseNumber;
- public CurrentAccount(String name, double balance, String tradeLicense) {
- super(name, balance, 5000);
- tradeLicenseNumber = tradeLicense;
- }
- public void getLicenseNumber() {
- System.out.println("Trade License Number: " + tradeLicenseNumber);
- }
- }
- //SavingAccount
- public class SavingAccount extends BankAccount {
- private int interest;
- private double withdrawLimit;
- public SavingAccount(String name, double balance, double wl) {
- super(name, balance, 2000);
- interest = 5;
- withdrawLimit = wl;
- }
- public SavingAccount(String name, double balance, double minBalance, double wl) {
- super(name, balance, minBalance);
- interest = 5;
- withdrawLimit = wl;
- }
- public double getBalance() {
- return super.getBalance() * ( (100.00 + interest) / 100 );
- }
- public void withdraw(double amount) {
- if (amount < withdrawLimit) {
- super.withdraw(amount);
- }
- else {
- System.out.println("THe amount that you want to withdraw exceeds your 'Withdraw Limit'");
- }
- }
- public double getOriginalBalance() {
- return super.getBalance();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement