i have an issue with my output for my destination menu, when the user enters a lowercase
Question:
i have an issue with my output for my destination menu, when the user enters a lowercase x it exits the to the main menu but the only char is uppercase X is the only character to do so. here is my code:
Main menu
public static void main(String[] args) {
Agency agency = new Agency();
agency.login();
agency.use();
}
private void use() {
char choice;
while ((choice = MainMenu()) != 'X') {
switch (choice) {
case '1':
flightsMenu();
break;
case '2':
destinationsMenu();
break;
case '3':
bookTrips();
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
System.out.println("Thanks for using the Prog2 Travel Agency");
}
private char MainMenu() {
Scanner scanner = new Scanner(System.in);
char choice;
do {
System.out.println("1. Explore Flights");
System.out.println("2. Explore Destinations");
System.out.println("3. Book a Trip");
System.out.println("X. Exit the System");
System.out.print("Please enter an option: ");
String input = scanner.nextLine().trim();
if (input.length() == 1) {
choice = input.charAt(0);
if (choice >= 'a' && choice <= 'z') {
System.out.println("Please enter a valid choice, or press X to exit.");
choice = ' ';
} else {
choice = Character.toUpperCase(choice);
if (choice != '1' && choice != '2' && choice != '3' && choice != 'X') {
System.out.println("Please enter a valid choice, or press X to exit.");
choice = ' ';
}
}
} else {
System.out.println("Please enter a valid choice, or press X to exit.");
choice = ' ';
}
} while (choice != '1' && choice != '2' && choice != '3' && choice != 'X');
return choice;
}
My destinations Menu
private void destinationsMenu() {
Scanner scanner = new Scanner(System.in);
char choice;
System.out.println("Welcome to the Destinations section " + loggedInUser.getName() + ", Please make a selection from the menu:");
do {
System.out.println("1. View All Destinations");
System.out.println("2. View Destinations by Country");
System.out.println("3. Add a Destination");
System.out.println("4. Remove a Destination");
System.out.println("X. Return to Main Menu");
System.out.print("Please enter an option: ");
String input = scanner.nextLine();
if (!input.isEmpty()) {
choice = input.toUpperCase().charAt(0);
} else {
choice = ' ';
}
switch (choice) {
case '1':
destinations.getDestinations();
break;
case '2':
System.out.print("Enter the country: ");
String country = scanner.nextLine();
destinations.displayByCountry(country);
break;
case '3':
System.out.print("Enter the name of the destination: ");
String name = scanner.nextLine();
System.out.print("Enter the country of the destination: ");
country = scanner.nextLine();
destinations.addDestination(name, country);
break;
case '4':
System.out.print("Enter the name of the destination to remove: ");
name = scanner.nextLine();
System.out.print("Enter the country of the destination to remove: ");
country = scanner.nextLine();
destinations.removeDestination(name, country);
break;
case 'X':
System.out.println("Welcome to the Prog2 Travel Agency " + loggedInUser.getName() + ", Please make a selection from the menu: ");
use();
break;
case 'x':
System.out.println("Please enter a valid choice, or press X to exit.");
break;
default:
System.out.println("Please enter a valid choice, or press X to exit.");
}
} while (choice != 'X');
}
Please help me solve to make X the only char to exit to the main menu and program.