Event Scheduling: Our fair is stretching for months and the users need to know the count of events for each month so that they can manage their time and monetary balances accordingly. So let’s create a part of our application which counts the number of events and displays them for each month. Write a threading program which counts the number of events as the user enters the events and display them at the last.
Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names, and method names should be the same as specified in the problem statement.
Create a class Event with the following private attributes
Attributes | Datatype |
name | String |
eventDate | Date |
organizer | String |
Include appropriate getters and setters.
Create default constructor and a parameterized constructor with arguments in order Event(String name, Date eventDate, String organizer).
Create a class EventThread with the following public method
Method | Description |
Map<String, Integer> eventMonths(final List<Event> eventList) | This method takes the event list as a parameter and computes the number of events conducted every month. It returns a map of the month with corresponding event count. |
Use ExecutorService to execute a single thread executor which provides events scheduled.
Hint: [use executorService.awaitTermination(200, TimeUnit.MILLISECONDS) method before returning the map]
Create a driver class Main to test the above classes.
Input Format:
The first line of input corresponds to the number of events ‘n’.
The next ‘n’ line of input corresponds to the event details in CSV format of (Event Name,Event Date(dd/MM/yyyy),Organizer Name).
Refer to sample input for formatting specifications.
Output Format:
The output consists of a month name and corresponding event count.
Refer to sample output for formatting specifications.
Problem constraints:
n>0 else display as “Invalid Input“.
[All text in bold corresponds to input and rest corresponds to output]
Sample Input/Output-1:
Enter the number of events
5
Enter event details in CSV(Event Name,Event Date,Organizer Name)
Wedding,12/12/2010,Martin
Public Party,01/04/2010,Jarvis
Info Mata,25/08/2010,Siri
Automobile,13/01/2010,OkGoogle
Circuits academy,31/12/2010,Purseup
December = 2 events
January = 1 events
April = 1 events
August = 1 events
Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
int jan=0,feb=0,mar=0,apr=0,may=0,jun=0,jul=0,aug=0,sep=0,oct=0,nov=0,dec=0;
Scanner sc=new Scanner(System.in);
ArrayList<Integer> al=new ArrayList<>();
System.out.println("Enter the number of events");
String num=sc.nextLine();
Integer n=Integer.parseInt(num);
if(n>0){
System.out.println("Enter event details in CSV(Event Name,Event Date,Organizer Name)");
for(int i=0;i<n;i++){
String temp=sc.nextLine();
String[] arr=temp.split(",");
String t=arr[1];
String[] arr1=t.split("/");
String t1=arr1[1];
al.add(Integer.parseInt(t1));
}
for(int j=0;j<al.size();j++){
if(al.get(j)==1){jan+=1;}
if(al.get(j)==2){feb+=1;}
if(al.get(j)==3){mar+=1;}
if(al.get(j)==4){apr+=1;}
if(al.get(j)==5){may+=1;}
if(al.get(j)==6){jun+=1;}
if(al.get(j)==7){jul+=1;}
if(al.get(j)==8){aug+=1;}
if(al.get(j)==9){sep+=1;}
if(al.get(j)==10){oct+=1;}
if(al.get(j)==11){nov+=1;}
if(al.get(j)==12){dec+=1;}
}
}
else{
System.out.println("Invalid Input");
}
if(n>0){
HashMap<String,Integer> map = new HashMap<>();
if(jan>0){map.put("January",jan);}
if(feb>0){map.put("February",feb);}
if(mar>0){map.put("March",mar);}
if(apr>0){map.put("April",apr);}
if(may>0){map.put("May",may);}
if(jun>0){map.put("June",jun);}
if(jul>0){map.put("July",jul);}
if(aug>0){map.put("August",aug);}
if(sep>0){map.put("September",sep);}
if(oct>0){map.put("October",oct);}
if(nov>0){map.put("November",nov);}
if(dec>0){map.put("December",dec);}
//System.out.println(map);
for (Map.Entry< String,Integer> entry : map.entrySet()){
System.out.println(entry.getKey()+" = "+entry.getValue()+" events");
}
/*System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());*/
}
}
}
Happy Learning – If you require any further information, feel free to contact me.