1

I was having trouble on debugging this certain bug on my program. When I run the program I am shown a menu in which if I choose an account that doesn't exist it proceeds to the method that should return an error. It happens on all of the methods that requires you to choose an account for it can someone help me with this?

I tried to look at the lines the compiler is telling the error was at but I can't seem to fix it

Here is an example of the error enter image description here Main class

import java.util.ArrayList;
import java.util.Scanner;

public class Menu {

    //needed variables
    Scanner keyboard = new Scanner(System.in);

    //bank object
    Bank bank = new Bank();


    //boolean is false
    boolean exit;


    /**
     * printt menu for the ui in the console
     */
    public static void main(String [] args){
        Menu menu = new Menu();
        menu.runMenu();
    }

    /**
     * method for displaying the menu
     */
    public void runMenu(){
        printHeader();

        //whil user doesn't pick the exit choice menu will continue
        while(!exit){
            printMenu();
            int choice = getInput();
            performAction(choice);
        }
    }


    /**
     *
     * @param choice for making an account, exit, withdraw, deposit, balance.
     */
    private void performAction(int choice) {
        switch(choice){
            case 0:
                System.out.println("Thank you for choosing B-Cash");
                System.exit(0);
                break;
            case 1:
                System.out.println("+===================+");
                System.out.println("|      Creating a   |");
                System.out.println("|       B-CASH      |");
                System.out.println("|       Account!    |");
                System.out.println("+===================+");
                createBCashAccount();
                break;
            case 2:
                System.out.println("+===================+");
                System.out.println("|      Making a     |");
                System.out.println("|      B-Deposit    |");
                System.out.println("+===================+");
                makeADeposit();
                break;
            case 3:
                System.out.println("+===================+");
                System.out.println("|      Making a     |");
                System.out.println("|      B-Withdraw   |");
                System.out.println("+===================+");
                makeAWithdrawal();
                break;
            case 4:
                System.out.println("+===================+");
                System.out.println("|      Looking at   |");
                System.out.println("|      B-Listing!   |");
                System.out.println("+===================+");
                listBalances();
                break;
            default:
                System.out.println("Oh no");
        }

    }

    /**
     * List all accounts
     */
    private void listBalances() {
        int account = selectAccount();
        if(account >= 0) {

            System.out.println(bank.getCustomer(account).getAccount());
        }
    }


    /**
     * Withdraw certain amount from accounts
     */
    private void makeAWithdrawal() {
        int account = selectAccount();
        if(account >= 0) {


            System.out.println("How much would you like to withdraw?");
            double amount = 0;
            try {
                amount = Double.parseDouble(keyboard.nextLine());

            } catch (NumberFormatException e) {
                amount = 0;
            }
            bank.getCustomer(account).getAccount().withdraw(amount);
        }
    }

    /**
     * Desposit certain amount from the accounts
     */
    private void makeADeposit() {
        int account = selectAccount();
        if(account >= 0) {


            System.out.println("How much would you like to deposit?");
            double amount = 0;
            try {
                amount = Double.parseDouble(keyboard.nextLine());

            } catch (NumberFormatException e) {
                amount = 0;
            }
            bank.getCustomer(account).getAccount().deposit(amount);
        }
    }


    /**
     *
     * @return Make the user choose from certain accounts from the arraylist
     */
    private int selectAccount() {
        ArrayList<Customer> customers = bank.getCustomers();
        if(customers.size() <= 0){
            System.out.println("No B Customers at the moment");
            return -1;
        }
        System.out.println("Select an account");
        for(int i = 0; i < customers.size(); i++){
            System.out.println((i+1)+ ") " + customers.get(i).basicInfo());

        }
        int account =0;
        System.out.print("Please enter your selection");
        try{
            account = Integer.parseInt(keyboard.nextLine()) -1;
        }catch(NumberFormatException e){
            account = -1;
        }
        if(account < 0 || account > customers.size()){
            System.out.println("Invalid account selected");
            account = -1;
        }
        return account;
    }


    /**
     *
     * @return declare accountype of user
     */
    private String getAccountType(){
        String accountType = "";
        boolean valid = false;
        while(!valid){
            accountType = askQuestion("Please enter an account type. (checking/savings)");
            if(accountType.equalsIgnoreCase("checking") || accountType.equalsIgnoreCase("savings")){
                valid = true;
            }else {
                System.out.println("Invalid account type.");
            }
        }
        return accountType;
    }

        private String askQuestion(String question){
        String response = "";
        Scanner input = new Scanner(System.in);
        System.out.print(question);
        response = input.nextLine();
        return response;

        }
        private double getDeposit(String accountType){
        double initialDeposit = 0;
            boolean valid = false;
            while(!valid){
                System.out.print("Please enter initial deposit");
                try{
                    initialDeposit = Double.parseDouble(keyboard.nextLine());

                }catch(NumberFormatException e){
                    System.out.println("Deposit must be a number.");

                }





                if(accountType.equalsIgnoreCase("checking")){
                    if(initialDeposit < 1000){
                        System.out.println("Checking accounts require a minimum P1000 deposit");

                    }else{
                        valid = true;
                    }

                } else  if(accountType.equalsIgnoreCase("savings")){
                    if(initialDeposit < 500){
                        System.out.println("Savings accounts require a minimum P500 deposit");

                    }else{
                        valid = true;
                    }

                }
            }
            return initialDeposit;
        }

    private void createBCashAccount() {
        String accountType = getAccountType();
        String firstName = askQuestion("Please enter your first name: ");
        String lastName = askQuestion("Please enter your last name: ");
        String ssn = askQuestion("Please enter your Student ID number: ");
        double initialDeposit = getDeposit(accountType);
        // creation of account
        Account account;
        if(accountType.equalsIgnoreCase("checking")){
        account = new Checking(initialDeposit);
        }else{
            account = new Savings(initialDeposit);
        }
        Customer customer = new Customer(firstName, lastName, ssn, account);
        bank.addCustomer(customer);
    }

    private int getInput() {
        int choice = -1;
    do {
    System.out.println("Enter your choice");

        try {
            choice = Integer.parseInt(keyboard.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("Invalid Selection Bro, Numbers only");
        }
        if (choice < 0 || choice > 4) {
            System.out.println("Pick only from 0-4");
        }
    }while(choice < 0 || choice > 4);
        return choice;
    }

    private void printMenu() {
        System.out.println("+===================+");
        System.out.println("|      B- CASH!     |");
        System.out.println("+===================+");
        System.out.println("Make a selection from the list");
        System.out.println(" 1) Create a New B-Cash account");
        System.out.println(" 2) Make a B deposit");
        System.out.println(" 3) Make a B withdrawal");
        System.out.println(" 4) List B-account balance");
        System.out.println(" 0) Exit B-Cash");
    }

    private void printHeader() {
        System.out.println("+===================+");
        System.out.println("|     Welcome to    |");
        System.out.println("|       B-CASH      |");
        System.out.println("+===================+");
        try {
            System.out.println(" ");
            Thread.sleep(4000);
            System.out.println(" ");
            System.out.println(" ");
            System.out.println(" ");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



}


Bank class

import java.util.ArrayList;

/**
 * Bank : Sotrage of customer data, adding data, and returning said data.
 */
public class Bank {


    //print out all customers on a empty arraylist
    ArrayList<Customer> customers = new ArrayList<Customer>();
    void addCustomer(Customer customer){
        customers.add(customer);
    }


    //get data from customer class
    Customer getCustomer(int account) {
        return customers.get(account);
    }


    //return customer data
    ArrayList<Customer> getCustomers(){
        return customers;
    }
}

Customer class

public class Customer {
    /**
     * Inits of
     * First name
     * Last nam
     * ssn
     * account
     */
    private final String firstName;
    private final String lastName;
    private final String ssn;
    private final Account account;

    /**
     *
     * @param firstName First name of the object
     * @param lastName Last name of the object
     * @param ssn Number inputted by the object
     * @param account account of the object
     */
    Customer(String firstName, String lastName, String ssn, Account account){
        this.firstName = firstName;
        this.lastName = lastName;
        this.ssn = ssn;
        this.account = account;
    }

    /**
     *
     * @return Printing of data from customer
     */
    @Override
    public String toString(){
        return "\nCustomer Information \n" +
                "First name: " + firstName +  "\n" +
                "Last name: " + lastName + "\n" +
                "Student ID Number" + ssn + "\n" +
                account;

    }
    public String basicInfo(){
        return " First name: " + firstName +
                " Last name: " + lastName +
                " Student ID Number: " + ssn +
                " Account Number: " + account.getAccountNumber();

    }
    Account getAccount(){
        return account;
    }
}

Account class

public class Account {
    //inits
    private double balance = 0;
    private double interest = 0.2;
    private int accountNumber;
    private static int numberOfAccounts = 10000000;


    /**
     * Making of unique id of the customer
     */
    Account() {
        accountNumber = numberOfAccounts++;

    }

    /**
     * Get balance of the user
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     *
     * @param balance Setting the balance of the user
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**
     *
     * @return interest times 100 for percentage value
     */
    public double getInterest() {
        return interest * 100;
    }

    /**
     *
     * @param interest setting the interest of accounts
     */
    public void setInterest(double interest) {
        this.interest = interest;
    }

    /**
     *
     * @return Account number of user
     */
    public int getAccountNumber() {
        return accountNumber;
    }


    /**
     *
     * @param amount Amount of desired withdraw
     */
    public void withdraw(double amount){
        if(amount + 5 > balance){
            System.out.println("Insufficient B Balance.");
            return;
        }
        //printing of withdraw
        balance -= amount + 5;
        checkInterest(0);
        System.out.println("You have withdrawn P" + amount + " Pesos and a incurred fee of P5 ");
        System.out.println("You now have a balance of P " + balance);
    }


    /**
     *
     * @param amount Amount of deisred deposit
     */
    public void deposit(double amount){
    if(amount <= 0){
        System.out.println("You cannot deposit nothing");
                return;
    }
    //printing of deposited amount
    checkInterest(amount);
    amount = amount + amount * interest;
    balance += amount;
        System.out.println("You have deposited P" + amount + " Pesos with an interest rate of " + (interest*100) + "%");
        System.out.println("You now have a balance of P " + balance);
    }


    /**
     *
     * @param amount Check if what interest rate is your account
     */
    public  void checkInterest(double amount){
        if(balance + amount > 10000){
            interest = 0.05;
        }else{
            interest = 0.02;
        }
    }

}
  • 1
    `if(account < 0 || account > customers.size()){ System.out.println("Invalid account selected"); ... }` <- your condition should be `account >= customers.size()` because the first index is `0` a List with size of 1 does not have an index 1. – OH GOD SPIDERS Mar 03 '21 at 10:18
  • @OHGODSPIDERS they ask for a 1. to X. based list on the command line. They should check `account < 1`, or, subtract one first, and use `>=`. – rzwitserloot Mar 03 '21 at 10:29
  • @rzwitserloot The code does subtract 1 from the entered index in the `selectAccount()` method – OH GOD SPIDERS Mar 03 '21 at 10:39
  • Whoops - overlooked that somehow. – rzwitserloot Mar 03 '21 at 13:47

0 Answers0