Advertisement
iccaka

Java Enum example

Oct 22nd, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5.  
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         WeeklyCalendar weeklyCalendar = new WeeklyCalendar();
  12.         weeklyCalendar.addEntry(new WeeklyEntry("friday", "Sport"));
  13.         weeklyCalendar.addEntry(new WeeklyEntry("monday", "Chill"));
  14.         weeklyCalendar.addEntry(new WeeklyEntry("sunday", "PC"));
  15.         weeklyCalendar.addEntry(new WeeklyEntry("tuesday", "Work"));
  16.  
  17.         weeklyCalendar.getWeeklySchedule().forEach(x -> System.out.println(x.toString()));
  18.  
  19.     /*
  20.     Monday - Chill
  21.     Tuesday - Work
  22.     Friday - Sport
  23.     Sunday - PC
  24.     */
  25.     }
  26. }
  27.  
  28. enum Weekday{  // Main enum, containing the days of the week
  29.     MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
  30.  
  31.     @Override
  32.     public String toString() {  // Basic toString() method
  33.         return this.name().substring(0, 1) + this.name().substring(1, this.name().length()).toLowerCase();
  34.     }
  35.  
  36. }
  37.  
  38. class WeeklyEntry{
  39.     public static Comparator<WeeklyEntry> BY_WEEKDAY = getComparatorByWeekday();  // The Collections.sort() method requires a comparator, so it can sort the days
  40.     private Weekday weekday;
  41.     private String notes;
  42.  
  43.     public WeeklyEntry(String weekday, String notes) {  // Basic constructor
  44.         this.weekday = Weekday.valueOf(Weekday.class, weekday.toUpperCase());
  45.         this.notes = notes;
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {  // Normal toString() method
  50.         return String.format("%s - %s", this.weekday, this.notes);
  51.     }
  52.  
  53.     private static Comparator<WeeklyEntry> getComparatorByWeekday() {  // Here we return the comparator (Collections.sort() requires it)
  54.         return (e1, e2) -> Integer.compare(e1.weekday.ordinal(), e2.weekday.ordinal());
  55.     }
  56. }
  57.  
  58. class WeeklyCalendar{
  59.     private List<WeeklyEntry> weekdayList;
  60.  
  61.     public WeeklyCalendar() {
  62.         this.weekdayList = new ArrayList<>();
  63.     }
  64.  
  65.     public void addEntry(WeeklyEntry weeklyEntry){
  66.         this.weekdayList.add(weeklyEntry);
  67.     }
  68.  
  69.     public Iterable<WeeklyEntry> getWeeklySchedule(){
  70.         Collections.sort(this.weekdayList, WeeklyEntry.BY_WEEKDAY);  // We sort it using the custom comparator...
  71.         return Collections.unmodifiableList(this.weekdayList);  // and then we return it, so the user can iterat trought it
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement