Java枚举

时间:2011-11-12 20:39:30

标签: java enums

这就是我要做的事情:

创建一个包含表示星期几的枚举的应用程序。显示日期列表,然后提示用户一天。显示所选日期的营业时间。假设营业时间为周日11至5,工作日为9至9,周六为9至6。

这就是我所拥有的:

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);
    }
}
}

和枚举类:

public enum Day {

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

}

请帮助我如何进行比较和打印输出。谢谢。

7 个答案:

答案 0 :(得分:6)

如果您创建这样的枚举:

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;
    }
}

您可以获得请求的日期:

Day day = null;

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

然后只是

if (day != null)
    System.out.println(day.getOpeningHours());

答案 1 :(得分:1)

您可以保存这样的数据:

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 
  ...

}

并使用:

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());

答案 2 :(得分:0)

您可以使用

获取String值的enum表示形式
for (Day d : Day.values())
{
   System.out.println(d);
}

这应该可以帮助您打印日期列表。

从用户那里获得(字符串)输入后,您可以使用以下内容将其映射回枚举:

Day userInputDay = Day.valueOf(userInputString);

现在您拥有正确的枚举值,并且您的switch语句将发挥作用。

答案 3 :(得分:0)

根据谢尔盖的回答: 您可以向枚举添加字段,因为枚举被编译为内置java类的子类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;
       }
}

当然,如果您需要在工作时间内操作,请使用日历。 这样你就可以:

 WorkingDay day = WorkingDays.forString(dayInput);
 System.out.println("Open from: " + day.start + " to:" + day.end);

答案 4 :(得分:0)

看看这里,它几乎就是你正在做的......

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

您已经在与switch语句进行比较,并且它也应该执行输出。 if else语句是不必要的,因为您正在使用switch语句进行检查。认真查看链接中的代码....

答案 5 :(得分:0)

你有一个错误。而不是

dayInput == "Sunday"

您应该使用equals方法

"Sunday".equals(dayInput)

因为它实际上比较了字符串相等性,而不是引用相等性。 有关详细信息,请参阅Java String.equals versus ==

或者,您可以使用以下内容:

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

我会将其作为练习留给读者,以确定"Friday".toUpperCase()何时与"Friday".toUpperCase(Locale.ENGLISH)不同。

答案 6 :(得分:0)

我宁愿以这种方式完成你的任务:

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();
    }
}

和主要方法:

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);
}

时间类可以用toString()方法实现,但你可以简单地添加分钟,比较等等(或者你可以使用普通的整数而不是创建Time类,但它是非常不可扩展的。)