I am relatively new to Java, and I am trying to write a program that accepts the username and password of a user and outputs basic stuff (coinflip and the date/time, etc). I just moved from a CLI to a GUI and I want everything to be displayed on one window instead of having to press "OK" all of the time. I have tried using AI but there is always an error. Here is my code:



package my_playground;
import java.util.Scanner;
import java.util.Calendar;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.Random;
import javax.swing.JOptionPane;

public class MyPlayground {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
Calendar calendar = Calendar.getInstance();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss, MM/dd/yyyy");
LocalDateTime now = LocalDateTime.now();
Random rand = new Random();

String username = JOptionPane.showInputDialog("Enter a username: ");
String password = JOptionPane.showInputDialog("Enter a password: ");

if (username.equalsIgnoreCase("pete543")) {
if (!password.equals("ILikeJava")) {
JOptionPane.showMessageDialog(null, "Your password is incorrect!");
System.exit(0);
} else {
loginUser(username, rand, calendar, dtf, now);
}
} else if (username.equalsIgnoreCase("admin")) {
if (!password.equals("password")) {
JOptionPane.showMessageDialog(null, "Your password is incorrect!");
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "You have administrator privileges!");
loginUser(username, rand, calendar, dtf, now);
}
} else {
JOptionPane.showMessageDialog(null, "User not found!");
System.exit(0);
}
}

private static void loginUser(String username, Random rand, Calendar calendar, DateTimeFormatter dtf, LocalDateTime now) {
JOptionPane.showMessageDialog(null, "You have successfully been logged into your account, " + username + "!");
int randomint = rand.nextInt(2);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
double number = Math.random() * 10;
JOptionPane.showMessageDialog(null, "A random number between 0 and 10 is " + Math.round(number));
JOptionPane.showMessageDialog(null, "Heads or tails: " + (randomint == 1 ? "Tails" : "Heads"));
JOptionPane.showMessageDialog(null, "The date and time is " + dtf.format(now));

switch (dayOfWeek) {
case Calendar.SUNDAY:
JOptionPane.showMessageDialog(null, "It is Sunday today!");
break;
case Calendar.MONDAY:
JOptionPane.showMessageDialog(null, "It is Monday today!");
break;
case Calendar.TUESDAY:
JOptionPane.showMessageDialog(null, "It is Tuesday today!");
break;
case Calendar.WEDNESDAY:
JOptionPane.showMessageDialog(null, "It is Wednesday today!");
break;
case Calendar.THURSDAY:
JOptionPane.showMessageDialog(null, "It is Thursday today!");
break;
case Calendar.FRIDAY:
JOptionPane.showMessageDialog(null, "It is Friday today!");
break;
case Calendar.SATURDAY:
JOptionPane.showMessageDialog(null, "It is Saturday today!");
break;
}
}
}



Tysm!