Michael Cheng
Michael Cheng

Reputation: 73

Method isnt using the correct parsed input

I have received an assignment which I am almost complete, however i have ran into a major issue which i don't know how to resolve. My problem is When the user inputs a balance and a name it accepts it, but then when the deposit and the withdraw methods run they don't use that data instead they start at the default of 0

package assignment1;

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

public class TestBank {    
    public static void main(String[] args) {
        Bank bank = new Bank();

        bank.enterCustomers();

        System.out.println();
        System.out.println("==========================");
        System.out.println("Opening account balance");

        bank.printBalances();

        System.out.println();

        bank.banking();

        System.out.println();
        System.out.println("==========================");
        System.out.println("Closing account balance");
        bank.printBalances();
    }
}

class Bank {
    String accounts;
    Scanner scanner = new Scanner(System.in);
    Account account = new Account();

    public void enterCustomers() {
        ArrayList CustomerDetails = new ArrayList();
        while (true) {
            System.out.print("Enter Name: ");
            account.name = scanner.next();

            if (account.name.equals("q")) {
                break;
            } else {
                Input.promptText("Enter balance: ");
                account.balance = scanner.nextDouble();

                CustomerDetails.add(account.name);
                CustomerDetails.add(account.balance);

                System.out.println("Enter customer names or q to quit entering names");
            }
        }
    }

    public void printBalances() {
        System.out.println(account.name + " " + Math.round(account.balance * 100 / 100));
    }

    public void banking() {
        Account account = new Account();
        account.deposit();
        account.withdraw();

        int Newbalance = (int) (Math.round(account.balance));

        switch (Newbalance) {
            case 1:
                account.deposit();
                break;
            case 2:
                account.withdraw();
                break;
            case 0:
                System.exit(0);
            default:
                break;
        }
    }

    class Account {
        private String name;
        private double balance;

        public Account() {
            this.name = name;
            this.balance = balance;
        }

        public String getName() {
            return this.name;
        }

        public double getBalance() {
            return this.balance;
        }

        public double deposit() {
            Input.promptText("Enter a valid deposit: ");
            double amount = scanner.nextDouble();

            balance = balance + amount;

            Input.promptText("your balance after your deposit is: " + balance);
            return balance;
        }

        public double withdraw() {
            System.out.println();
            Input.promptText("Enter a valid withdraw: ");
            double amount = scanner.nextDouble();

            if (amount > balance); else {
                balance = balance - amount;
            }

            System.out.println("your balance after your withdrawal is: " + balance);
            return balance;
        }
    }
}

class Input {
    private static Scanner scanner = new Scanner(System.in);

    public static String getText() {
        return scanner.nextLine();
    }

    public static String getText(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    public static String getString() {
        return scanner.nextLine();
    }

    public static String getString(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    public static double getDouble() {
        return scanner.nextDouble();
    }

    public static double getDouble(String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void promptText(String text) {
        System.out.printf("%s", text);
    }

    public static void outputText(String text) {
        System.out.printf("%s\n", text);
    }

    public static void outputInteger(String text, int value) {
        System.out.printf("%s%d\n", text, value);
    }

    public static void outputDouble(String text, double value) {
        outputDouble(text, value, 2);
    }

    public static void outputDouble(String text, double value, int decimalPlaces) {
        System.out.printf("%s%." + decimalPlaces + "f\n", text, value);
    }

    public static void outputBoolean(String text, boolean value) {
        System.out.printf("%s%b\n", text, value);
    }
}

also how could i make it so, for example, the entered name is John and the input is 300. And another name is added, say Adam with input 400, so that it prints Johns balance = 300, Adams input = 400

Upvotes: 1

Views: 59

Answers (1)

Emre Savcı
Emre Savcı

Reputation: 3070

Your mistake is in banking() method. You are creating new Account and operate on it, not on previously created account.

public void banking() {

    // remove the below line
    // Account account = new Account();

    account.deposit();
    account.withdraw();

    int Newbalance = (int) (Math.round(account.balance));

    switch (Newbalance) {
        case 1:
            account.deposit();
            break;
        case 2:
            account.withdraw();
            break;
        case 0:
            System.exit(0);
        default:
            break;
    }
}

Here is the output :

Enter Name: test
Enter balance: 500
Enter customer names or q to quit entering names
Enter Name: q

==========================
Opening account balance
q 500

Enter a valid deposit: 100
your balance after your deposit is: 600.0
Enter a valid withdraw: 50
your balance after your withdrawal is: 550.0

==========================
Closing account balance
q 550

Another mistake is you are take accounts from input while account name != q but you are operate on last account. You are storing only one account not all. In the above, it should operate on account test but instead it operated with account q.

So for fixing it you should fix your enterCustomers() method :

public void enterCustomers() {

        ArrayList CustomerDetails = new ArrayList();

        while (true) {

            System.out.print("Enter Name: ");
            // account.name = scanner.next();
            // don't set input directly to account
            String inputName = scanner.next();
            // check given input
            if (inputName.equals("q")) {

                break;

            } else {
                Input.promptText("Enter balance: ");
                account.name = inputName;
                account.balance = scanner.nextDouble();

                CustomerDetails.add(account.name);
                CustomerDetails.add(account.balance);

                System.out.println("Enter customer names or q to quit entering names");

            }

        }

    }

Now output is :

Enter Name: test
Enter balance: 500
Enter customer names or q to quit entering names
Enter Name: q

==========================
Opening account balance
test 500

Enter a valid deposit: 100
your balance after your deposit is: 600.0
Enter a valid withdraw: 50
your balance after your withdrawal is: 550.0

==========================
Closing account balance
test 550

Upvotes: 1

Related Questions