-2

This is what I'm trying to do:

Create an application that contains an enumeration that represents the days of the week. Display a list of the days, 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 9 to 6 on Saturday.

This is what I have:

import javax.swing.JOptionPane;


public class DayOfWeek {
Day day;

public void Day(Day day) {
    this.day = day;
}

public void businessHours() {
    switch (day) {
        case SATURDAY: System.out.println("Open from 9 to 6.");
                     break;

        case SUNDAY: System.out.println("Open from 11 to 5.");
                     break;

        default:     System.out.println("Open from 9 to 9.");
                     break;
    }
}

public static void main(String[] args) {

    String dayInput = JOptionPane.showInputDialog("Please input a day: ");

    EnumDay sixthDay = new EnumDay(Day.SATURDAY);
    sixthDay.businessHours();
    EnumDay seventhDay = new EnumDay(Day.SUNDAY);
    seventhDay.businessHours();

    if (dayInput == "Saturday")
    {
    JOptionPane.showMessageDialog(null, sixthDay.businessHours());
    System.exit(0);
    }

    else if (dayInput == "Sunday")
    {
        JOptionPane.showMessageDialog(null, seventhDay.businessHours());
        System.exit(0);
    }
    else
    {
        JOptionPane.showMessageDialog(null, default.businessHours());
        System.exit(0);
    }
}
}

and the enum class:

public enum Day {

SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
THURSDAY, FRIDAY, SATURDAY 

}

Please help how I can do the comparison and the print out. Thank you.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
user1043361
  • 9
  • 1
  • 3

7 Answers7

6

If you create your enum like this:

public enum Day {
    SUNDAY("Sunday", "Open from 11 to 5."),
    MONDAY("Monday", "Open from 9 to 9."),
    TUESDAY("Tuesday", "Open from 9 to 9."),
    WEDNESDAY("Wednesday", "Open from 9 to 9."),
    THURSDAY("Thursday", "Open from 9 to 9."),
    FRIDAY("Monday", "Open from 9 to 9."),
    SATURDAY("Saturday", "Open from 9 to 6.");

    private final String day;
    private final String openingHours;

    Day(String day, String openingHours) {
        this.day = day;
        this.openingHours = openingHours;
    }

    public String getDay() {
        return day;
    }

    public String getOpeningHours() {
        return openingHours;
    }
}

You can get the requested day like:

Day day = null;

for (Day d : Day.values()) {
    if (d.getDay().equals(dayInput)) {
        day = d;
        break;
    }
}

and then just

if (day != null)
    System.out.println(day.getOpeningHours());
flesk
  • 7,439
  • 4
  • 24
  • 33
1

You can keep your data like this:

Class WorkingDay{
  Day day;
  Time startTime;
  Time endTime;

  public WorkingDay(Day day, Time startTime, Time endTime){
    this.day = day;
    this.startTime = startTime;
    this.endTime = endTime;
  }   

 //getters/setters 
 ...

}

class Time{
   int hours;
   int minutes;
   public Time(int hours, int minutes){
     ...
   }

   public String toString(){
       return hours + " : " + minutes; 
   }

  //getters/setters 
  ...

}

and use :

WorkingDay monday =  new WorkingDay (Day.MONDAY, new Time(9, 0), new Time(21,0));
...
WorkingDay saturday=  new WorkingDay (Day.SATURDAY, new Time(9, 0), new Time(18,0));
...

WorkingDay workingDay = ...;
System.out.println("Open from " + workingDay.getStartTime() +  " to " + workingDay.getEndTime());

)

Sergey Gazaryan
  • 1,013
  • 1
  • 9
  • 25
  • 1
    If you wanted to go this route, there's no need for a `WorkingDay` class. An `enum` in Java is a "special" class which you can extend to include this functionality ("extend" for lack of a better term - you can add methods and private fields to your `enum`). Edit: flesk's answer illustrates this beautifully. – Brian Roach Nov 12 '11 at 20:59
  • 2
    @BrianRoach, when we add our fields in enum class , we have predefined instances (Monday (Time1, Time2) ... ) . Our code is more flexible when Time arguments we sets from the outside. – Sergey Gazaryan Nov 12 '11 at 21:04
  • That's great for a DTO, but why would you do that unless you use a database for persistence, which is not the case here? – flesk Nov 13 '11 at 20:18
0

You can get the String representations of your enum values with:

for (Day d : Day.values())
{
   System.out.println(d);
}

That should help you print the list of days.

Once you get (String) input from the user, you can map that back to the enum with:

Day userInputDay = Day.valueOf(userInputString);

Now you have the correct enum value, and your switch statement would come into play.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

Based on Sergey's answer: You can add fields to enums, since an enum is compiled to be a child of the built-in java class Enum

enum WorkingDay{ MON("Monday", 9, 9), TUE... SUN("Sunday", 11,5);
       String name;
       int start;
       int end;
       //add private ctor

       public static WorkingDay forString(String dayName) {
             for(WorkingDay d : values())
                 if(d.name.equals(dayName))
                      return d;
       }
}

Use Calendars of course, if you need to operate with the working hours. This way you can do:

 WorkingDay day = WorkingDays.forString(dayInput);
 System.out.println("Open from: " + day.start + " to:" + day.end);
zeller
  • 4,904
  • 2
  • 22
  • 40
0

look here, it is almost exactly what you are working on...

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

You are already doing a comparison with the switch statement, and a it should do the output as well. The if else statements are unnecessary because you are checking with the switch statement. seriously look over the code in the link ....

0

You have a bug. Instead of

dayInput == "Sunday"

you should use the equals method

"Sunday".equals(dayInput)

since that actually compares for string equality, not reference equality. See Java String.equals versus == for more details.

Alternatively you can play around with the following:

Day day = Day.valueOf(dayInput.toUpperCase(Locale.ENGLISH))

I'll leave it as an exercise to the reader to figure out when "Friday".toUpperCase() is not the same as "Friday".toUpperCase(Locale.ENGLISH).

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

I'd rather do your task in this way:

public enum Day {
    MONDAY(new Time(9, Time.AM), new Time(6, Time.PM)),
    // ... rest of weekdays here
    SATURDAY(new Time(9, Time.AM), new Time(6, Time.PM)),
    SUNDAY(new Time(11, Time.AM), new Time(5, Time.PM));

    private final Time open;
    private final Time close;

    private Day(Time open, Time close) {
        this.open = open;
        this.close = close;
    }

    public String businessHours() {
        return "Open from " + open.toString() + " to " + close.toString() + ".";
    }

    @Override public String toString() {
        return name().substring(0, 1) + name().substring(1).toLowerCase();
    }
}

and main method:

public static void main(String[] args) {
    final String dayInput = JOptionPane.showInputDialog("Please input a day: ");
    try {
        final Day actualDay = Day.valueOf(dayInput.toUpperCase()); // I don't know swing at all, it could be null pointer access here
    } catch(IllegalArgumentException e) {
        // for example print dialog with error about wrong day name
    }
    System.exit(0);
}

Time class could be implemented just with toString() method but you can simply add minutes, compare etc. (or alternatively you can use plain Integers instead of creating Time class, but it's quite non-extensible).

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112