Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bankaccount;
- public class BankAccount {
- private double balance;
- //Constructor
- public BankAccount() {
- balance = 0.0;
- }
- //deposit method
- public void deposit(double amount) {
- balance = balance + amount;
- }
- //withdraw method
- public void withdraw(double amount) throws InsufficientFundException {
- if(amount> balance) {
- throw new InsufficientFundException("Insufficient balance. " + "Withdraw process could not be updated.");
- }
- balance = balance - amount;
- }
- //getter
- public double getBalance() {
- return balance;
- }
- }
- =================================================================================================================================
- package bankaccount;
- public class InsufficientFundException extends Exception {
- //must extend the parent class
- private String message;
- //Constructor
- public InsufficientFundException(String message) {
- this.message = message;
- }
- public String getMessage() {
- return message;
- }
- }
- =================================================================================================================================
- package bankaccount;
- import java.util.Scanner;
- public class BankAccountTester {
- public static void main(String[] args) {
- BankAccount account = new BankAccount();
- Scanner input = new Scanner(System.in);
- int choice;
- do {
- System.out.println("------BANK ACCOUNT MENU------");
- System.out.println("1-Deposit");
- System.out.println("2-Withdraw");
- System.out.println("3-Show current balance");
- System.out.println("4-Quit");
- System.out.println("Select an option: ");
- choice = input.nextInt();
- switch (choice) {
- case 1:
- System.out.println("Deposit Amount: ");
- account.deposit(input.nextInt());
- System.out.println("Your current balance is: " + account.getBalance());
- break;
- case 2:
- System.out.println("Withdraw Amount: ");
- try {
- account.withdraw(input.nextInt());
- System.out.println("Your current balance is: " + account.getBalance());
- } catch (InsufficientFundException e) {
- System.out.println(e.toString());
- }
- break;
- case 3:
- System.out.println("Current balance: " + account.getBalance());
- break;
- }
- } while(choice!=4);
- System.out.println("Thank you for using our service. Goodbye!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement