Create an application that contains an enumeration that represents the days of the week. Display a list of the days, and then prompt the user for a day. Display business hours for the chosen day. Assume that the business is open from 11 to 5 on Sunday, 9 to 9 on weekdays, and 10 to 6 on Saturday. Save the file as DayOfWeek.java.

Respuesta :

Answer:

The program to this question can be defined as below:

Program:

import java.util.*; //import package for user input

enum Days{Sunday, Monday, Tuesday, Wednesday, Thusday, Friday, Saturday} //defining enum

public class DayOfWeek //defining class DayOfWeek

{

  public static void main(String[] args) //defining main method

  {

  System.out.println("Days of the Week: "); //print message  

  for (Days d : Days.values()) //defining for loop to print enums values

  {

  System.out.print(d+"\n");//print values

  }

  System.out.println("Enter your day: "); //print message

  Scanner obx= new Scanner(System.in); //create Scanner class object for user input

  Object d= obx.next(); //create object to hold user-input value

  System.out.print("Business Hours "); //print message

   //check conditions  

  if(Days.Sunday.toString().equals(d)) //defining if block to check if day is Sunday

  {

  System.out.println("from 11 to 5"); //print message

  }

  else if(Days.Saturday.toString().equals(d))// check if the day isn't Sunday

  {

  System.out.println("from 10 to 6");  //print message

  }

  else //else block

  {

      System.out.println("from 9 to 9");//print message

  }

  }

}

Output:

please see the attachment.

Explanation:

In the given code first package is import for user input then the enumeration "Days" is used, in which weekday values are defined. In the next line, the class "DayOfWeek" is defined, in this main method is declared, that first uses a loop to print enum value than it will create object "d" for user input and define conditional statement to check values, that can be described as follows:

  • In the if block it will check if the input value is "Sunday", it will print 11 to 5 business hours, otherwise, it will go to the else if block.
  • In this block it will check, that input value is "Saturday", if it is true it will print 10 to 6 business hours.
  • If both above condition is false so, it will print 9 to 9 business hours.
Ver imagen codiepienagoya