这是我必须要合作的,我有 一个多班次的餐厅:
周一至周六
周一至周五
星期五
星期六
我需要将这些班次转换为营业时间,因此输出应如下所示:
周一至周四
上午9点到下午2点
下午4点至晚上9点
星期五
星期六
星期日
我有2个.java文件:ShiftHour.Java
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
//do not modify
public class ShiftHour {
private Set<Integer> daysInWeek = new HashSet<Integer>(); //CALENDAR'S DAYINWEEK
private int startHour; //IN 24HR FORMAT (1 to 24)
private int endHour;
//helper methods
public Set<Integer> getDaysInWeek() {
return daysInWeek;
}
public void setDaysInWeek(Set<Integer> daysInWeek) {
this.daysInWeek = daysInWeek;
}
public int getStartHour() {
return startHour;
}
public void setStartHour(int startHour) {
this.startHour = startHour;
}
public int getEndHour() {
return endHour;
}
public void setEndHour(int endHour) {
this.endHour = endHour;
}
//methods that generate assignment
public static List<ShiftHour> initializeShiftHoursSetOne() {
List<ShiftHour> shiftHours = new ArrayList<ShiftHour>(0);
ShiftHour sh1 = new ShiftHour();
sh1.getDaysInWeek().add(Calendar.MONDAY);
sh1.getDaysInWeek().add(Calendar.TUESDAY);
sh1.getDaysInWeek().add(Calendar.WEDNESDAY);
sh1.getDaysInWeek().add(Calendar.THURSDAY);
sh1.getDaysInWeek().add(Calendar.FRIDAY);
sh1.getDaysInWeek().add(Calendar.SATURDAY);
sh1.setStartHour(9);
sh1.setEndHour(14);
ShiftHour sh2 = new ShiftHour();
sh2.getDaysInWeek().add(Calendar.MONDAY);
sh2.getDaysInWeek().add(Calendar.TUESDAY);
sh2.getDaysInWeek().add(Calendar.WEDNESDAY);
sh2.getDaysInWeek().add(Calendar.THURSDAY);
sh2.getDaysInWeek().add(Calendar.FRIDAY);
sh2.setStartHour(16);
sh2.setEndHour(21);
ShiftHour sh3 = new ShiftHour();
sh3.getDaysInWeek().add(Calendar.FRIDAY);
sh3.setStartHour(12);
sh3.setEndHour(17);
ShiftHour sh4 = new ShiftHour();
sh4.getDaysInWeek().add(Calendar.SATURDAY);
sh4.setStartHour(14);
sh4.setEndHour(19);
shiftHours.add(sh1);
shiftHours.add(sh2);
shiftHours.add(sh3);
shiftHours.add(sh4);
return shiftHours;
}
public static List<ShiftHour> initializeShiftHoursSetTwo() {
List<ShiftHour> shiftHours = new ArrayList<ShiftHour>(0);
ShiftHour sh1 = new ShiftHour();
sh1.getDaysInWeek().add(Calendar.MONDAY);
sh1.getDaysInWeek().add(Calendar.TUESDAY);
sh1.getDaysInWeek().add(Calendar.THURSDAY);
sh1.getDaysInWeek().add(Calendar.FRIDAY);
sh1.getDaysInWeek().add(Calendar.SATURDAY);
sh1.setStartHour(9);
sh1.setEndHour(14);
ShiftHour sh2 = new ShiftHour();
sh2.getDaysInWeek().add(Calendar.MONDAY);
sh2.getDaysInWeek().add(Calendar.TUESDAY);
sh2.getDaysInWeek().add(Calendar.WEDNESDAY);
sh2.getDaysInWeek().add(Calendar.THURSDAY);
sh2.getDaysInWeek().add(Calendar.FRIDAY);
sh2.setStartHour(16);
sh2.setEndHour(21);
ShiftHour sh3 = new ShiftHour();
sh3.getDaysInWeek().add(Calendar.WEDNESDAY);
sh3.setStartHour(12);
sh3.setEndHour(17);
ShiftHour sh4 = new ShiftHour();
sh4.getDaysInWeek().add(Calendar.SATURDAY);
sh4.setStartHour(15);
sh4.setEndHour(19);
ShiftHour sh5 = new ShiftHour();
sh5.getDaysInWeek().add(Calendar.MONDAY);
sh5.setStartHour(8);
sh5.setEndHour(19);
shiftHours.add(sh1);
shiftHours.add(sh2);
shiftHours.add(sh3);
shiftHours.add(sh4);
shiftHours.add(sh5);
return shiftHours;
}
}
和test.java
public class Test {
public static void main(String[] args) {
//Initialize first set of data
List<ShiftHour> shiftHours = ShiftHour.initializeShiftHoursSetOne();
//WHERE THE SOLUTION SHOULD GO.
//Additional set of data for verification
shiftHours = ShiftHour.initializeShiftHoursSetTwo();
}
}
答案 0 :(得分:1)
一看起来像家庭作业,我就不想给出简单的复制粘贴解决方案
这里是纯java实现,您可以将其作为基础来实现您的数据类型:
public class ShiftHour {
public static final int MONDAY = 0, TUESDAY = 1, WEDNESDAY = 2, THURSDAY = 3, FRIDAY = 4, SATURDAY = 5, SUNDAY = 6;
private static final String[] names = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
public static class OperatingHours {
int[] hours = new int[7];
public void addShift(final int start, final int end, final int... days) {
for (int i = start; i <= end; i++) {
for (final int w : days)
hours[w] |= (1 << i);
}
public void printHours() {
final StringBuilder sb = new StringBuilder();
String prevDay = getDay(hours[0]);
String startDay = names[0];
String endDay = names[0];
for (int i = 1; i < 7; i++) {
final String day = getDay(hours[i]);
if (day.equals(prevDay)) endDay = names[i];
else {
appendDay(sb, startDay, endDay, prevDay);
prevDay = day;
startDay = endDay = names[i];
}
}
appendDay(sb, startDay, endDay, prevDay);
System.out.println(sb.toString());
}
String getDay(final int m) {
final StringBuilder sb = new StringBuilder();
int startHour = -1;
int endHour = -1;
for (int i = 0; i <= 24; i++) {
if (((1 << i) & m) > 0) {
if (startHour == -1) startHour = i;
endHour = i;
} else if (startHour >= 0) {
sb.append("\t" + formatHour(startHour) + " to " + formatHour(endHour) + "\n");
startHour = endHour = -1;
}
}
if (startHour >= 0) sb.append("\t" + formatHour(startHour) + " to " + formatHour(endHour) + "\n");
return sb.toString();
}
void appendDay(final StringBuilder sb, final String startDay, final String endDay, final String prevDay) {
sb.append(startDay + (startDay.equals(endDay) ? "" : ("-" + endDay)) + "\n" + (prevDay.equals("") ? "\tClosed\n" : prevDay));
}
String formatHour(final int h) {
if (h == 0) return "12AM";
if (h < 12) return h + "AM";
if (h == 12) return "12PM";
if (h < 24) return (h - 12) + "PM";
return "12AM"; // 24 hours is midnight again
}
};
public static void main(final String[] args) {
final OperatingHours hours = new OperatingHours();
hours.addShift(9, 14, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
hours.addShift(16, 21, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY);
hours.addShift(12, 17, FRIDAY);
hours.addShift(14, 19, SATURDAY);
hours.printHours();
}
}
输出:
Monday-Thursday
9AM to 2PM
4PM to 9PM
Friday
9AM to 9PM
Saturday
9AM to 7PM
Sunday
Closed