-2

I'm trying to answer these questions.

  1. The New Account option should implement the following:

    • Input client details: name, address, birthday, and contact number
    • Input the initial deposit of not less than PhP5,000
    • Generate a four-digit account number randomly
  2. The Balance Inquiry option should implement the following:

    • Input the account number and validate
    • If the account number is valid, display the client name and current balance

I've tried coding the New Account option with setter methods in it and also generates a four-digit number randomly that I can use to input it in the Balance Inquiry option that has getter methods but it displays empty. I tried debugging it and the variables return empty after exiting the if statement.

Class with the main method, displayMainMenu() for the options, newAccount() and fourRandomNumber().

public class ClientUgang { 
    public static void main(String[] args) {
        displayMainMenu();
    }

    public static void displayMainMenu() {
        SavingsAccountUgang savingsAccount = new SavingsAccountUgang();

        int option = 0;
        while (option != 7) {
            Scanner scan = new Scanner(System.in);

            System.out.println("JBank Main Menu");
            System.out.println("[1] New Account");
            System.out.println("[2] Balance Inquiry");
            System.out.println("[3] Deposit");
            System.out.println("[4] Withdraw");
            System.out.println("[5] Client Profile");
            System.out.println("[6] Close Account");
            System.out.println("[7] Exit");
            option = scan.nextInt();

            if (option == 1) {
                newAccount();
            }
            if (option == 2) {
                savingsAccount.balanceInquiry();
            }
        }
    }

    public static void newAccount() {
        Scanner scan = new Scanner(System.in);
        SavingsAccountUgang savingsAccount = new SavingsAccountUgang();

        System.out.print("Name: ");
        String name = scan.nextLine();
        System.out.print("Address: ");
        String address = scan.nextLine();
        System.out.print("Birthday: ");
        String birthday = scan.nextLine();
        System.out.print("Contact number: ");
        String contactNumber = scan.nextLine();

        savingsAccount.setAccountName(name);
        savingsAccount.setAddress(address);
        savingsAccount.setBirthday(birthday);
        savingsAccount.setContactNumber(contactNumber);

        int deposit = 0;
        while (deposit < 5000) {
            System.out.print("Initial deposit(not less than Php5000): ");
            deposit = scan.nextInt();
        }
        savingsAccount.setBalance(deposit);

        int fourDigitNumber = fourRandomNumber(1000, 9000);
        savingsAccount.setAccountNo(fourDigitNumber);

        System.out.println("Your Account Number: " + fourDigitNumber);
        System.out.println();
    }

    public static int fourRandomNumber(int min, int max) {
        Random rand = new Random();

        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
}

The Class where my balanceInquiry() method is. My setter and getter method for accountName is in BankAccountUgang class.

public class SavingsAccountUgang extends BankAccountUgang {
    private int accountNo;
    private double balance;

    public SavingsAccountUgang() {
    }

    public int getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }


    public void balanceInquiry() {
        Scanner scan = new Scanner(System.in);

        int accountNumber = 0;
        do {
            System.out.print("Enter Account Number: ");
            accountNumber = scan.nextInt());
        } while (accountNumber != getAccountNo());

        System.out.println(getAccountName());
        System.out.println(getBalance());
        System.out.println();
    }
}

I expect the setter methods to work so that I can call the getter methods.

JBank Main Menu
[1] New Account
[2] Balance Inquiry
[3] Deposit
[4] Withdraw
[5] Client Profile
[6] Close Account
[7] Exit
1
Name: John
Address: World
Birthday: Aug 2019
Contact number: 123 1234
Initial deposit(not less than Php5000): 5000
Your Account Number: 6810

JBank Main Menu
[1] New Account
[2] Balance Inquiry
[3] Deposit
[4] Withdraw
[5] Client Profile
[6] Close Account
[7] Exit
2
Enter Account Number: 6810
Enter Account Number: BUILD STOPPED (total time: 27 seconds)
Saswata
  • 1,290
  • 2
  • 14
  • 28
wesju
  • 5
  • 4
  • 3
    Because you're referring to two different `SavingsAccountUgang` objects in your code. You create a new one in your `displayMainMenu()` and a 2nd one in your `newAccount()` methods. They do not refer to the same instance and will have different values – Draken Aug 19 '19 at 05:51

3 Answers3

3

There are a lot of issues with your code.

To fix that 1 way is below:

  1. Return a new saving account from the newAccount method, so change the return type to:
public static SavingsAccountUgang newAccount() {
    // Your existing code
    return savingsAccount;
}
  1. Then in your displayMainMenu() method save this account if the user enters 1 as an input and later use that instance to show the balance:
public static void displayMainMenu() {
    SavingsAccountUgang savingsAccount = null // don't create object here as you are doing
    // Your code
    if (option == 1) {
        savingsAccount = newAccount();
    }
    if (option == 2) {
        if(savingsAccount  == null) {
            // throw exception or whatever you want to do.
        }
        savingsAccount.balanceInquiry();
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0

The SavingsAccountUgang instance in your newAccount() method is a local variable and therefore only visible to this method. If you want to use it outside your method you have to return or declare it outside of your method.

Draken
  • 3,134
  • 13
  • 34
  • 54
Markus
  • 1
  • 2
0

Well, the setBalance() method requires an argument type double, but you send a type int, but it's not a reason for a mistake. Also, you should use this in a statement like this one:

while (accountNumber != this.getAccountNo());
Pingolin
  • 3,161
  • 6
  • 25
  • 40
  • the `int` variable is implicitly cast to `double` here. That's why it shouldn't be any *technical* problem. And even though I agree with you concerning the `this.` keyword - it's generally not necessary when calling a method. Furthermore, since it is a simple `getter` OP could directly access the variable (with `accountNumber != this.accountNo`) instead of the method call. – GameDroids Aug 19 '19 at 08:00