21.4k views
4 votes
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.

User Hotpink
by
4.6k points

2 Answers

3 votes

Final answer:

To create an application that contains an enumeration representing the days of the week and display business hours for a chosen day, you can define an enum with the days of the week as its elements and additional properties like business hours. Prompt the user for a day and retrieve the business hours using the getBusinessHours() method.

Step-by-step explanation:

To create an application that contains an enumeration representing the days of the week, you can define an enum with the days of the week as its elements. Each element can also have additional properties, such as the business hours. Here's an example:

public enum DayOfWeek {
SUNDAY("11:00 AM to 5:00 PM"),
MONDAY("9:00 AM to 9:00 PM"),
TUESDAY("9:00 AM to 9:00 PM"),
WEDNESDAY("9:00 AM to 9:00 PM"),
THURSDAY("9:00 AM to 9:00 PM"),
FRIDAY("9:00 AM to 9:00 PM"),
SATURDAY("10:00 AM to 6:00 PM");

private String businessHours;

private DayOfWeek(String businessHours) {
this.businessHours = businessHours;
}

public String getBusinessHours() {
return businessHours;
}
}

After defining the enum, you can display a list of the days using a loop. Then, prompt the user for a day and retrieve the business hours using the getBusinessHours() method. Here's an example:

for (DayOfWeek day : DayOfWeek.values()) {
System.out.println(day);
}

Scanner scanner = new Scanner(System.in);
System.out.println("Enter a day: ");
String userInput = scanner.nextLine();

DayOfWeek chosenDay = DayOfWeek.valueOf(userInput.toUpperCase());
System.out.println("Business hours for " + chosenDay + ": " + chosenDay.getBusinessHours());

This application will display the list of days and prompt the user to enter a day. It will then display the business hours for the chosen day based on the enum values.

User Srinidhi
by
4.4k points
6 votes

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+"\\");//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.

Step-by-step 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.
Create an application that contains an enumeration that represents the days of the-example-1
User Boaz Stuller
by
5.2k points