Advertisement
Guest User

Java Calander

a guest
Mar 9th, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | Source Code | 0 0
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeParseException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.Iterator;
  8. import java.util.Scanner;
  9.  
  10. class Event {
  11.     String name;
  12.     LocalDateTime dateTime;
  13.  
  14.     public Event(String name, LocalDateTime dateTime) {
  15.         this.name = name;
  16.         this.dateTime = dateTime;
  17.     }
  18.  
  19.     public String toString() {
  20.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy 'at' h:mm a");
  21.         return name + " on " + dateTime.format(formatter);
  22.     }
  23. }
  24.  
  25. class Main {
  26.     static ArrayList<Event> events = new ArrayList<>();
  27.  
  28.     public static void main(String[] args) {
  29.         Scanner kb = new Scanner(System.in);
  30.  
  31.         System.out.println("What is your name?");
  32.         String name = kb.nextLine();
  33.         System.out.println();
  34.         System.out.println("Hello " + name + ".");
  35.         System.out.println();
  36.  
  37.         while (true) { // loop back to the start
  38.             if (events.size() == 0) {
  39.                 System.out.println("You have no events yet.");
  40.                 System.out.println("Please add an event.");
  41.                 System.out.println();
  42.                 addEvent(kb);
  43.             } else {
  44.                 System.out.println();
  45.                 System.out.println("What do you wish to do?");
  46.                 System.out.println();
  47.                 System.out.println("add");
  48.                 System.out.println("remove");
  49.                 System.out.println("view all");
  50.                 System.out.println("exit");
  51.                 System.out.println();
  52.  
  53.                 String choice = kb.nextLine();
  54.                 switch (choice) {
  55.                     case "add":
  56.                         addEvent(kb);
  57.                         break;
  58.                     case "remove":
  59.                         removeEvent(kb);
  60.                         break;
  61.                     case "view all":
  62.                         viewAllEvents();
  63.                         break;
  64.                     case "exit":
  65.                         System.out.println();
  66.                         System.out.println("Goodbye!");
  67.                         return; // exit the program
  68.                     default:
  69.                         System.out.println("Invalid choice. Please try again.");
  70.                         System.out.println();
  71.                         break;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     public static void addEvent(Scanner kb) {
  78.     System.out.println();
  79.     System.out.println("Enter the name of the event:");
  80.     String name = kb.nextLine();
  81.  
  82.     LocalDateTime dateTime = null;
  83.     while (true) {
  84.         try {
  85.             System.out.println();
  86.             System.out.println("Enter the date and time of the event in the format 'M/d/yyyy h:mm AM/PM' (press enter to skip):");
  87.             String input = kb.nextLine().trim();
  88.             if (!input.isEmpty()) {
  89.                 String pattern = "M/d/yyyy h:mm a";
  90.                 if (input.matches("\\d{1}/\\d{1}/\\d{4}\\s\\d{1}:\\d{2}\\s\\w{2}")) {
  91.                     input = "0" + input;
  92.                 }
  93.                 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
  94.                 dateTime = LocalDateTime.parse(input, formatter);
  95.             }
  96.             if (dateTime == null) {
  97.                 System.out.println();
  98.                 System.out.println("Event not added.");
  99.                 System.out.println();
  100.                 break;
  101.             } else {
  102.                 Event event = new Event(name, dateTime);
  103.                 events.add(event);
  104.                 System.out.println();
  105.                 System.out.println("Event added: " + event.toString());
  106.                 break;
  107.             }
  108.         } catch (DateTimeParseException e) {
  109.             System.out.println();
  110.             System.out.println("Invalid date/time format. Please enter a date/time in the format 'M/d/yyyy h:mm AM/PM', for example '1/1/2022 01:30 PM'.");
  111.         }
  112.     }
  113. }
  114.  
  115.     public static void removeEvent(Scanner kb) {
  116.     System.out.println();
  117.     System.out.println("Enter the name of the event you want to remove:");
  118.     String name = kb.nextLine();
  119.  
  120.     boolean found = false;
  121.     Iterator<Event> iterator = events.iterator();
  122.     while (iterator.hasNext()) {
  123.         Event event = iterator.next();
  124.         if (event.name.equalsIgnoreCase(name)) {
  125.             found = true;
  126.             iterator.remove();
  127.             System.out.println();
  128.             System.out.println("Event removed: " + event.toString());
  129.           System.out.println();
  130.             break;
  131.         }
  132.     }
  133.  
  134.     if (!found) {
  135.         System.out.println();
  136.         System.out.println("No event found with the name '" + name + "'.");
  137.     }
  138.   }
  139.  
  140.     public static void viewAllEvents() {
  141.     Scanner kb = new Scanner(System.in);
  142.  
  143.     if (events.size() == 0) {
  144.         System.out.println("You have no events.");
  145.     } else {
  146.         System.out.println();
  147.         System.out.println("How would you like the events to be sorted?");
  148.         System.out.println();
  149.         System.out.println("1. Alphabetically");
  150.         System.out.println("2. Time");
  151.         System.out.println("3. Cancel");
  152.         System.out.println();
  153.  
  154.         String choice = kb.nextLine();
  155.         switch (choice) {
  156.             case "1":
  157.                 Collections.sort(events, new Comparator<Event>() {
  158.                     @Override
  159.                     public int compare(Event e1, Event e2) {
  160.                         return e1.name.compareToIgnoreCase(e2.name);
  161.                     }
  162.                 });
  163.                 break;
  164.             case "2":
  165.                 Collections.sort(events, new Comparator<Event>() {
  166.                     @Override
  167.                     public int compare(Event e1, Event e2) {
  168.                         return e1.dateTime.compareTo(e2.dateTime);
  169.                     }
  170.                 });
  171.                 break;
  172.             case "3":
  173.                 return;
  174.             default:
  175.                 System.out.println("Invalid choice. Please try again.");
  176.                 System.out.println();
  177.                 break;
  178.         }
  179.  
  180.         System.out.println("Your events:");
  181.         System.out.println();
  182.      
  183.         for (Event event : events) {
  184.             System.out.println(event.toString());          
  185.         }      
  186.     }  
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement