1

I'm making a event scheduler in Java, and so far I can only add one event. However, I'd like to be able to add more than one event and be able to display them, but the only other option I can think of is using arrays.

I also have a counter called numOfCreatedEvents to keep track of the events and increments when an event is created.

Example of user input

Enter the event ID: A12

Enter the event title: Lorem Ipsum

Enter the fee: $ 10.0

Enter the maximum attendee limit: 15

Enter the start time: 14

Enter the duration time in minutes: 120

Enter requirements (optional): Wear shoes.

Below is my program attempting to use arrays, but when I call the setter methods, they have an error (marked in the code below).

Event.java

    Scanner sc = new Scanner(System.in);

    // Private instance variables.
    private String[] ID;
    private String[] title;
    private double[] baseFee;
    private int[] maxAttendeeLimit;
    private int[] startTime;
    private int[] durationTime;
    private String[] requirements;

    private int numOfCreatedEvents;

    // Getters.
    public String[] getID() {
        return this.ID;
    }

    public String[] getTitle() {
        return this.title;
    }

    public double[] getBaseFee() {
        return this.baseFee;
    }

    public int[] getMaxAttendeeLimit() {
        return this.maxAttendeeLimit;
    }

    public int[] getStartTime() {
        return this.startTime;
    }

    public int[] getDurationTime() {
        return this.durationTime;
    }

    public String[] getRequirements() {
        return this.requirements;
    }

    // Setters.
    public void setID(String[] ID) {
        this.ID = ID;
    }

    public void setTitle(String[] title) {
        this.title = title;
    }

    public void setBaseFee(double[] baseFee) {
        this.baseFee = baseFee;
    }

    public void setMaxAttendeeLimit(int[] maxAttendeeLimit) {
        this.maxAttendeeLimit = maxAttendeeLimit;
    }

    public void setStartTime(int[] startTime) {
        this.startTime = startTime;
    }

    public void setDurationTime(int[] durationTime) {
        this.durationTime = durationTime;
    }

    public void setRequirements(String[] requirements) {
        this.requirements = requirements;
    }

    // Schedule a event.
    public void scheduleAEvent() {
        System.out.println("\n~ SCHEDULE A EVENT ~");
        System.out.println("---------------------");

        System.out.print("Enter the event ID: ");
        String eventID = sc.nextLine();
        setID(eventID); // Error here.

        System.out.print("Enter the event title: ");
        String eventTitle = sc.nextLine();
        setTitle(eventTitle); // Error here.

        System.out.print("Enter the fee: $");
        String baseFee = sc.nextLine();
        double eventBaseFee = Double.parseDouble(baseFee);
        setBaseFee(eventBaseFee); // Error here.

        System.out.print("Enter the maximum attendee limit: ");
        String maxAttendeeLimit = sc.nextLine();
        int eventMaxAttendeeLimit = Integer.parseInt(maxAttendeeLimit);
        setMaxAttendeeLimit(eventMaxAttendeeLimit); // Error here.

        System.out.print("Enter the start time: ");
        String startTime = sc.nextLine();
        int eventStartTime = Integer.parseInt(startTime);
        setStartTime(eventStartTime); // Error here.

        System.out.print("Enter the duration time in minutes: ");
        String durationTime = sc.nextLine();
        int eventDurationTime = Integer.parseInt(durationTime);
        setDurationTime(eventDurationTime); // Error here.

        System.out.print("Enter requirements (optional): ");
        String requirements = sc.nextLine();
        setRequirements(requirements); // Error here.

        // Increase the created event count.
        numOfCreatedEvents++;
    }

    // Print event details.
    public void printDetails() {
        System.out.println("\n~ EVENTS ~");
        System.out.println("-----------");

        String pattern = "%-25s %-50s %-25s %-43s %-34s %-34s %-1s\n";

        System.out.printf(pattern, "ID", "Title", "Fee", "Maximum Attendee Limit", "Start Time", "Duration Time", "Requirements");
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        // Display the records of events scheduled.
        for(int i = 0; i < numOfCreatedEvents; i++) {
            System.out.format(pattern, getID(), getTitle(), "$" + getBaseFee(), getMaxAttendeeLimit(), getStartTime(), getDurationTime(), getRequirements());
        }
    }

Main.java

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input;

        Event event = new Event();

        // Main menu.
        do {
            System.out.println("\n~ EVENT BOOKING SYSTEM ~");
            System.out.println("------------------------");
            System.out.println("A. Schedule an Event");
            System.out.println("B. View Event Details");
            System.out.println("X. Exit\n");

            System.out.print("Select an option: ");
            input = sc.nextLine();

            input = input.toUpperCase();

            switch(input) {
                case "A":
                    event.scheduleAnEvent();
                    break;

                case "B":
                    event.printDetails();
                    break;

                case "X":
                    System.out.println("INFO: You have exited the booking system.");
                    break;

                default:
                    System.out.println("ERROR: Invalid input!");
            }
        } while (input.equals("X") == false);

        sc.close();
    }

Problem: How do I add multiple events and keep a record of them when I call printDetails() to list all of them?

Thank you for your help!

5
  • Can you share your main method as well. Commented Apr 17, 2020 at 3:45
  • Can you put a sample of input? Commented Apr 17, 2020 at 3:48
  • Hi @Klaus, I have edited my question to include the main method. Commented Apr 17, 2020 at 4:14
  • Hi @SeyyedMortezaSeyyedAghaei, I have edited the question to include an example of the program and the user input. Commented Apr 17, 2020 at 4:20
  • The Event object you are attempting to use in main is not what you want. See the Java docs for more info: Event Object Commented Apr 17, 2020 at 21:11

3 Answers 3

1

The error is because your setter methods want to take in an array (denoted by the "[]" after the type in the method header), but the places you've marked as an error are attempting to send just a single piece of data of the given type. I think it would be best if you created an Object to represent an event, then had an array that stored these objects. Here's a quick mock-up:

In a file called CalendarEvent.java:

public class CalendarEvent {

    private String ID;
    private String title;
    private double baseFee;
    private int maxAttendeeLimit;
    private int startTime;
    private int durationTime;
    private String requirements;

    // Getters
    public String getID() { return this.ID; }
    public String getTitle() { return this.title; }
    public double getBaseFee() { return this.baseFee; }
    public int getMaxAttendeeLimit() { return this.maxAttendeeLimit; }
    public int getStartTime() { return this.startTime; }
    public int getDurationTime() { return this.durationTime; }
    public String getRequirements() { return this.requirements; }

    // Setters
    public void setID(String ID) { this.ID = ID; }
    public void setTitle(String title) { this.title = title; }
    public void setBaseFee(double baseFee) { this.baseFee = baseFee; }
    public void setMaxAttendeeLimit(int maxAttendeeLimit) { this.maxAttendeeLimit = maxAttendeeLimit; }
    public void setStartTime(int startTime) { this.startTime = startTime; }
    public void setDurationTime(int durationTime) { this.durationTime = durationTime; }
    public void setRequirements(String requirements) { this.requirements = requirements; }

    // this should return a String, built similarly to how you previously did it in your printDetails method
    public String toString() {
        return ID + " - " + title;
    }

    // other methods related to modifying a single event go here
    // ...

}

In another class called EventHandler.java:

import java.util.Scanner;

public class EventHandler {

    CalendarEvent[] myEvents;

    public void scheduleAEvent() {

        Scanner sc = new Scanner(System.in);

        System.out.println("\n~ SCHEDULE A EVENT ~");
        System.out.println("---------------------");

        CalendarEvent toAdd = new CalendarEvent();

        System.out.print("Enter the event ID: ");
        toAdd.setID(sc.nextLine());

        System.out.print("Enter the event title: ");
        toAdd.setTitle(sc.nextLine());

        System.out.print("Enter the fee: $");
        toAdd.setBaseFee(sc.nextDouble());

        System.out.print("Enter the maximum attendee limit: ");
        toAdd.setMaxAttendeeLimit(sc.nextInt());

        System.out.print("Enter the start time: ");
        toAdd.setStartTime(sc.nextInt());

        System.out.print("Enter the duration time in minutes: ");
        toAdd.setDurationTime(sc.nextInt());

        System.out.print("Enter requirements (optional): ");
        toAdd.setRequirements(sc.nextLine());
    }

    // Print event details
    public void printDetails() {
        System.out.println("\n~ EVENTS ~");
        System.out.println("-----------");

        String pattern = "%-25s %-50s %-25s %-43s %-34s %-34s %-1s\n";

        System.out.printf(pattern, "ID", "Title", "Fee", "Maximum Attendee Limit", "Start Time", "Duration Time", "Requirements");
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        // Display the records of events scheduled.
        for(int i = 0; i < myEvents.length; i++) {
            System.out.println(myEvents[i]);
        }
    }

}

I would suggest doing some reading on object oriented design, this is a much more organized way to structure your data. Best of luck, hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @NickChris, I've updated my question to include the main method; you can see my implementation there
@Wolfizzy Your main looks great, and should work fine with the code I created above. I would really recommend going the route prescribed here, your project here is a really good example of when to use an object. Currently, your Event.java class represents an array of events. It makes more sense to have a class representing a single event, then have an array of those objects. I hope that makes sense, let me know if I can clarify anything.
I made a new Java project in Eclipse to try your code and added the same main method I used in my question, but I got a NullPointerException error. How do I fix this?
The null pointer exception is because you haven't initialized the myEvents array. You can do that somewhere in your main.
Thanks @NickChris! It fixed my problem.
1

I am 100% clear what your expectation is. But the looks from your code, you are trying to set a String value to a method that is expecting an array of Strings. i.e. String[]

I suggest to remove the array implementation and replace with List<String>. For example:

private List<String> ID;

public void setID( String i )
{
    if( ID == null )
    {
        ID= new ArrayList<>();
    }
    ID.add( i );
}

public List<String> getID() 
{
   return ID;
}

Do this for all the variables. That is ID, Title, baseFee, maxAttendeeLimit, startTime, durationTime, requirements. Because arrays are fixed types and you cannot increment the size of an existing array once created. Access the elements like ID.get(i) within the loop

2 Comments

Hi @Klaus, I'm a bit new to Java and haven't learnt about ArrayLists yet (and I'll look into it), but thank you for your answer!
Arrays and other in-memory collections (such as ArrayLists are all well and good, but don't forget that they are transient: they exist only as long as your program is running. So at some point, you'll probably want to start looking into more permanent storage solutions: simple files to begin with, and maybe even databases further down the road.
1

Here's another example solution.

In the file below, I've removed all of the setters and replaced it with a constructor instead.

Reservation.java

public class Reservation {

    private String ID;
    private String title;
    private double baseFee;
    private int maxAttendeeLimit;
    private int startTime;
    private int durationTime;
    private String requirements;

    public Reservation(String ID, String title, double baseFee, int maxAttendeeLimit, int startTime, int durationTime, String requirements) {
        this.ID = ID;
        this.title = title;
        this.baseFee = baseFee;
        this.maxAttendeeLimit = maxAttendeeLimit;
        this.startTime = startTime;
        this.durationTime = durationTime;
        this.requirements = requirements;
    }

    public String getID() {
        return this.ID;
    }

    public String getTitle() {
        return this.title;
    }

    public double getBaseFee() {
        return this.baseFee;
    }

    public int getMaxAttendeeLimit() {
        return this.maxAttendeeLimit;
    }

    public int getStartTime() {
        return this.startTime;
    }

    public int getDurationTime() {
        return this.durationTime;
    }

    public String getRequirements() {
        return this.requirements;
    }

}

Below I've also used an array to store the reservation information.

Main.java

private Reservation[] reservation = new Reservation[5];
private int reservationCounter;

public void printDetails() {
        System.out.println("\n~ RESERVATIONS ~");
        System.out.println("----------------");

        String pattern = "%-25s %-50s %-25s %-43s %-34s %-34s %-1s\n";

        System.out.printf(pattern, "ID", "Title", "Fee", "Maximum Attendee Limit", "Start Time", "Duration Time", "Requirements");
        System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

        for(int i = 0; i < reservationCounter; i++)
            System.out.format(pattern, reservation[i].getID(), reservation[i].getTitle(), "$" + reservation[i].getBaseFee(), reservation[i].getMaxAttendeeLimit(), reservation[i].getStartTime(), reservation[i].getDurationTime(), reservation[i].getRequirements());
    }

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.