public class Driver { /** * @param args */ static Scanner keyboard = new Scanner(System.in); publicstaticvoid main(String[] args) { Mailbox[] mailboxes = new Mailbox[5]; Mailbox mailbox1 = new Mailbox("gmail"); Mailbox mailbox2 = new Mailbox("outlook"); Mailbox mailbox3 = new Mailbox("icloud"); mailboxes[0] = mailbox1; mailboxes[1] = mailbox2; mailboxes[2] = mailbox3; Email email1 = new Email(); email1.setDate(new Date(2022, 11, 5)); email1.setSubject("Exam2 guidelines "); Email email2 = new Email(new Date(2021, 3, 5), true, "Quiz3 in Ch7"); Email email3 = new Email(); email3.setDate(new Date(2020, 11, 3)); email3.setSubject("Late submission policy"); mailbox2.addEmail(email1); mailbox2.addEmail(email2); mailbox2.addEmail(email3); // System.out.println("Hello! What would you like to do "); intselection = displayMenu(); while (selection != 5) { // selection = displayMenu(); switch (selection) { case 1: System.out.print("Enter the client: "); String emailClient = keyboard.next(); Mailbox mailbox = findMailbox(emailClient, mailboxes); if (mailbox != null) { System.out.println(mailbox.toString()); } else { System.out.println("Mailbox not found. "); } break; case 2: System.out.print("For which client? "); String emailProvider = keyboard.next(); mailbox = findMailbox(emailProvider, mailboxes); if (mailbox == null) { System.out.println("Mailbox not found."); } else { System.out.println("Enter email information on one line as follows: "); System.out.println(", , , , "); booleanisUrgent = keyboard.nextBoolean(); intmonth = keyboard.nextInt(); intday = keyboard.nextInt(); intyear = keyboard.nextInt(); String messageReceived = keyboard.next(); Email email = new Email(new Date(year, month, day), isUrgent, messageReceived); mailbox.addEmail(email); System.out.println("Email added successfully."); } break; case 3: System.out.print("For which client?"); String emailService = keyboard.next(); mailbox = findMailbox(emailService, mailboxes); if (mailbox == null) { System.out.println("Mailbox not found."); } else { System.out.print("For which year?"); intyear = keyboard.nextInt(); Email[] emailsByYear = mailbox.findEmail(year); if (emailsByYear.length == 0) { System.out.println("No emails found for this year."); } else { System.out.println("Found " + emailsByYear.length + " email(s):"); for (inti = 0; i < emailsByYear.length; i++) { System.out.println("Email info: " + emailsByYear[i].toString()); } } } break; case 4: System.out.print("For which client?"); String emailInboxes = keyboard.next(); mailbox = findMailbox(emailInboxes, mailboxes); if (mailbox == null) { System.out.println("Mailbox not found."); } else { System.out.print("For which year?"); intyear = keyboard.nextInt(); Email[] emailsByYear = mailbox.findEmail(year); if (emailsByYear == null) { System.out.println("No emails found for this year."); } else { System.out.println("Found " + emailsByYear.length + " email(s):"); for (inti = 0; i < emailsByYear.length; i++) { System.out.println("Email info: " + emailsByYear[i].toString()); } } } break; case 5: System.out.println("Goodbye! "); break; default: System.out.println("Invalid input\n"); break; } selection = displayMenu(); } } privatestaticint displayMenu() { System.out.println("Hello! What would you like to do? "); System.out.println("1- List Emails in a mailbox"); System.out.println("2- Add an email to a mailbox"); System.out.println("3- Search for an email in a mailbox by year"); System.out.println("4- Sort emails in a mailbox by year"); System.out.println("5 - quit\n"); System.out.print("Enter your choice: "); intselection = keyboard.nextInt(); keyboard.nextLine(); returnselection; } privatestatic Mailbox findMailbox(String client, Mailbox[] mailboxes) { for (inti = 0; i < mailboxes.length; i++) { if (mailboxes[i].getClient().equals(client)) { returnmailboxes[i]; } } returnnull; } Why is program not printing goodbye message when I enter 5?
public class Driver
{
/**
* @param args
*/
static Scanner keyboard = new Scanner(System.in);
publicstaticvoid main(String[] args)
{
Mailbox[] mailboxes = new Mailbox[5];
Mailbox mailbox1 = new Mailbox("gmail");
Mailbox mailbox2 = new Mailbox("outlook");
Mailbox mailbox3 = new Mailbox("icloud");
mailboxes[0] = mailbox1;
mailboxes[1] = mailbox2;
mailboxes[2] = mailbox3;
Email email1 = new Email();
email1.setDate(new Date(2022, 11, 5));
email1.setSubject("Exam2 guidelines ");
Email email2 = new Email(new Date(2021, 3, 5), true, "Quiz3 in Ch7");
Email email3 = new Email();
email3.setDate(new Date(2020, 11, 3));
email3.setSubject("Late submission policy");
mailbox2.addEmail(email1);
mailbox2.addEmail(email2);
mailbox2.addEmail(email3);
// System.out.println("Hello! What would you like to do ");
intselection = displayMenu();
while (selection != 5)
{
// selection = displayMenu();
switch (selection)
{
case 1:
System.out.print("Enter the client: ");
String emailClient = keyboard.next();
Mailbox mailbox = findMailbox(emailClient, mailboxes);
if (mailbox != null)
{
System.out.println(mailbox.toString());
} else
{
System.out.println("Mailbox not found. ");
}
break;
case 2:
System.out.print("For which client? ");
String emailProvider = keyboard.next();
mailbox = findMailbox(emailProvider, mailboxes);
if (mailbox == null)
{
System.out.println("Mailbox not found.");
} else
{
System.out.println("Enter email information on one line as follows: ");
System.out.println("<urgent>, <m>, <d>, <y>, <Msg>");
booleanisUrgent = keyboard.nextBoolean();
intmonth = keyboard.nextInt();
intday = keyboard.nextInt();
intyear = keyboard.nextInt();
String messageReceived = keyboard.next();
Email email = new Email(new Date(year, month, day), isUrgent, messageReceived);
mailbox.addEmail(email);
System.out.println("Email added successfully.");
}
break;
case 3:
System.out.print("For which client?");
String emailService = keyboard.next();
mailbox = findMailbox(emailService, mailboxes);
if (mailbox == null)
{
System.out.println("Mailbox not found.");
} else
{
System.out.print("For which year?");
intyear = keyboard.nextInt();
Email[] emailsByYear = mailbox.findEmail(year);
if (emailsByYear.length == 0)
{
System.out.println("No emails found for this year.");
} else
{
System.out.println("Found " + emailsByYear.length + " email(s):");
for (inti = 0; i < emailsByYear.length; i++)
{
System.out.println("Email info: " + emailsByYear[i].toString());
}
}
}
break;
case 4:
System.out.print("For which client?");
String emailInboxes = keyboard.next();
mailbox = findMailbox(emailInboxes, mailboxes);
if (mailbox == null)
{
System.out.println("Mailbox not found.");
} else
{
System.out.print("For which year?");
intyear = keyboard.nextInt();
Email[] emailsByYear = mailbox.findEmail(year);
if (emailsByYear == null)
{
System.out.println("No emails found for this year.");
} else
{
System.out.println("Found " + emailsByYear.length + " email(s):");
for (inti = 0; i < emailsByYear.length; i++)
{
System.out.println("Email info: " + emailsByYear[i].toString());
}
}
}
break;
case 5:
System.out.println("Goodbye! ");
break;
default:
System.out.println("Invalid input\n");
break;
}
selection = displayMenu();
}
}
privatestaticint displayMenu()
{
System.out.println("Hello! What would you like to do? ");
System.out.println("1- List Emails in a mailbox");
System.out.println("2- Add an email to a mailbox");
System.out.println("3- Search for an email in a mailbox by year");
System.out.println("4- Sort emails in a mailbox by year");
System.out.println("5 - quit\n");
System.out.print("Enter your choice: ");
intselection = keyboard.nextInt();
keyboard.nextLine();
returnselection;
}
privatestatic Mailbox findMailbox(String client, Mailbox[] mailboxes)
{
for (inti = 0; i < mailboxes.length; i++)
{
if (mailboxes[i].getClient().equals(client))
{
returnmailboxes[i];
}
}
returnnull;
}
Why is
Step by step
Solved in 3 steps