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!