Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.Scanner;
- class Event {
- String name;
- LocalDateTime dateTime;
- public Event(String name, LocalDateTime dateTime) {
- this.name = name;
- this.dateTime = dateTime;
- }
- public String toString() {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy 'at' h:mm a");
- return name + " on " + dateTime.format(formatter);
- }
- }
- class Main {
- static ArrayList<Event> events = new ArrayList<>();
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.println("What is your name?");
- String name = kb.nextLine();
- System.out.println();
- System.out.println("Hello " + name + ".");
- System.out.println();
- while (true) { // loop back to the start
- if (events.size() == 0) {
- System.out.println("You have no events yet.");
- System.out.println("Please add an event.");
- System.out.println();
- addEvent(kb);
- } else {
- System.out.println();
- System.out.println("What do you wish to do?");
- System.out.println();
- System.out.println("add");
- System.out.println("remove");
- System.out.println("view all");
- System.out.println("exit");
- System.out.println();
- String choice = kb.nextLine();
- switch (choice) {
- case "add":
- addEvent(kb);
- break;
- case "remove":
- removeEvent(kb);
- break;
- case "view all":
- viewAllEvents();
- break;
- case "exit":
- System.out.println();
- System.out.println("Goodbye!");
- return; // exit the program
- default:
- System.out.println("Invalid choice. Please try again.");
- System.out.println();
- break;
- }
- }
- }
- }
- public static void addEvent(Scanner kb) {
- System.out.println();
- System.out.println("Enter the name of the event:");
- String name = kb.nextLine();
- LocalDateTime dateTime = null;
- while (true) {
- try {
- System.out.println();
- 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):");
- String input = kb.nextLine().trim();
- if (!input.isEmpty()) {
- String pattern = "M/d/yyyy h:mm a";
- if (input.matches("\\d{1}/\\d{1}/\\d{4}\\s\\d{1}:\\d{2}\\s\\w{2}")) {
- input = "0" + input;
- }
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
- dateTime = LocalDateTime.parse(input, formatter);
- }
- if (dateTime == null) {
- System.out.println();
- System.out.println("Event not added.");
- System.out.println();
- break;
- } else {
- Event event = new Event(name, dateTime);
- events.add(event);
- System.out.println();
- System.out.println("Event added: " + event.toString());
- break;
- }
- } catch (DateTimeParseException e) {
- System.out.println();
- 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'.");
- }
- }
- }
- public static void removeEvent(Scanner kb) {
- System.out.println();
- System.out.println("Enter the name of the event you want to remove:");
- String name = kb.nextLine();
- boolean found = false;
- Iterator<Event> iterator = events.iterator();
- while (iterator.hasNext()) {
- Event event = iterator.next();
- if (event.name.equalsIgnoreCase(name)) {
- found = true;
- iterator.remove();
- System.out.println();
- System.out.println("Event removed: " + event.toString());
- System.out.println();
- break;
- }
- }
- if (!found) {
- System.out.println();
- System.out.println("No event found with the name '" + name + "'.");
- }
- }
- public static void viewAllEvents() {
- Scanner kb = new Scanner(System.in);
- if (events.size() == 0) {
- System.out.println("You have no events.");
- } else {
- System.out.println();
- System.out.println("How would you like the events to be sorted?");
- System.out.println();
- System.out.println("1. Alphabetically");
- System.out.println("2. Time");
- System.out.println("3. Cancel");
- System.out.println();
- String choice = kb.nextLine();
- switch (choice) {
- case "1":
- Collections.sort(events, new Comparator<Event>() {
- @Override
- public int compare(Event e1, Event e2) {
- return e1.name.compareToIgnoreCase(e2.name);
- }
- });
- break;
- case "2":
- Collections.sort(events, new Comparator<Event>() {
- @Override
- public int compare(Event e1, Event e2) {
- return e1.dateTime.compareTo(e2.dateTime);
- }
- });
- break;
- case "3":
- return;
- default:
- System.out.println("Invalid choice. Please try again.");
- System.out.println();
- break;
- }
- System.out.println("Your events:");
- System.out.println();
- for (Event event : events) {
- System.out.println(event.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement