0

I have a few problems with my code.

After my input for Complete Name it goes to Gender loop right away. My while loop in gender is not working. It's not reading the conditions I have set. It doesn't read a hasNext or hasNextLine in my menu because it reads the nextInt right away. It doesn't read a hasNext or hasNextLine in my ob because it reads the nextDouble right away.

import java.util.Scanner;
import java.time.LocalDate;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BankAccountSystem extends Bank
{
    public static void main(String[] args)
    {
        Scanner ba = new Scanner(System.in);
        Bank BankAccount = new Bank();
        int menu = 0;
        double ob = 0;
        String name, address;
        String gender = "";
        LocalDate dateToday = LocalDate.now();
        Date date = new Date();

        System.out.print("\f");
        do {
            System.out.println("\nWhat are you looking for?"
            +"\n1.) New Accounts"
            +"\n2.) Deposit"
            +"\n3.) Account Inquiry"
            +"\n4.) Account Inquiry by Date"
            +"\n5.) Inquiry by Status"
            +"\n6.) Show All Accounts"  
            +"\n[Enter a num 1-6]");
            
            menu = ba.nextInt();
            switch (menu)
            {
                case 1: System.out.print("Enter complete name: ");
                while (ba.hasNextInt())
                {
                    System.out.print("Enter a valid complete name: ");
                    ba.next();
                }
                name = ba.nextLine();
                
                System.out.print("Enter gender [M-Male or F-Female]: ");
                ba.nextLine();
                while (!gender.equals("M") && !gender.equals("F") || (ba.hasNextInt()))
                {
                    System.out.print("Wrong input. Try again."
                    + "\nEnter gender [M-Male or F-Female]: ");
                    ba.next();
                }
                gender = ba.next();
                
                System.out.print("Enter Address: ");
                ba.next();
                address = ba.nextLine();
                
                System.out.print("Enter opening balance [Min: 500]: ");
                while (ob < 500 && !ba.hasNextDouble())
                {
                    System.out.print("Opening balance must be at least 500: ");
                    ba.next();
                }
                ob = ba.nextDouble();
                
                SimpleDateFormat timeNow = new SimpleDateFormat("hh:mm:ss a");
                System.out.println("\nAccount created successfully!"
                                    +"\nBank Account Number: " + BankAccount.accountNum(111,999)
                                    +"\nDate Opened: " + dateToday + " " + timeNow.format(date));
                break;
            }
        }while(menu != 0);
    }
}
Jezun
  • 1
  • You should use à if instead of a switch – Ruokki Jul 04 '20 at 15:05
  • Why, @Ruokki? I find `switch` just fine and the conventional choice for the purpose. – Ole V.V. Jul 04 '20 at 17:28
  • Not what you’re asking: Don’t mix ancient and modern date-time classes, it leads to confusion and over-complication. Throw away the old and poorly designed `Date` and `SimpleDateFormat` and stick to the classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) including `ZonedDateTime` and `DateTimeFormatter`, and possibly the `LocalDate` that you are already using. – Ole V.V. Jul 06 '20 at 02:31

0 Answers0