0

I have a few problems with my code.

  1. After my input for Complete Name it goes to Gender loop right away.
  2. My while loop in gender is not working. It's not reading the conditions I have set.
  3. It doesn't read a hasNext or hasNextLine in my menu because it reads the nextInt right away.
  4. It doesn't read a hasNext or hasNextLine in my ob because it reads the nextDouble right away.
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();
                //gender loop
                System.out.print("Enter gender [M-Male or F-Female]: ");
                ba.next();
                while (!(gender == "M" && gender == "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);
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
Jezun
  • 1
  • You are comparing Strings wrong. Don't use `==` or `!=` but rather `.equals(...)` or `!foo.equals(...)` – DontKnowMuchBut Getting Better Jul 04 '20 at 13:36
  • 1
    By the way, you are on the right track by using `LocalDate`, a class from the `java.time` package, however, you should also stop using `SimpleDateFormat` and `Date`. – MC Emperor Jul 04 '20 at 13:37
  • @DontKnowMuchButGettingBetter now I am using this ```while (!(gender.equals("M") && gender.equals("F")) || ba.hasNextInt())``` it's the same – Jezun Jul 04 '20 at 14:04
  • @MCEmperor why should I not use SimpleDateFormat and Date? I wanted to use the DateFormatter and LocalDateTime but it didn't work.. ```LocalDateTime myDateObj = LocalDateTime.now(); DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formattedDate = myDateObj.format(myFormatObj); System.out.println("After formatting: " + formattedDate); } }``` – Jezun Jul 04 '20 at 14:05
  • @Jezun https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api, to start with. – MC Emperor Jul 04 '20 at 15:14
  • @Jezun Second, what exactly didn't work? – MC Emperor Jul 04 '20 at 15:17
  • @MCEmperor the conditions here are not read ```while (!(gender == "M" && gender == "F") || ba.hasNextInt())``` ```while (ob < 500 && !ba.hasNextDouble())``` – Jezun Jul 04 '20 at 17:28
  • @Jezun The linked question already provides an answer to that. But you said: *"I wanted to use the DateFormatter and LocalDateTime but it didn't work"* – what about those two classes did not work? – MC Emperor Jul 04 '20 at 17:55

0 Answers0