Reputation: 77
It is an OOD question. Design a calendar. The requirement are:
Justify the data structure and design test cases
I know that I need to provide the APIs like addEvent(), deleteEvent(), getEventByDay()
I stuck at the first API, add an event. I have proposed two solutions, I just do not know which pattern is better and why.
version1:
public class Calender {
// helper class
public static class Event {
String name;
Event(String s) {
name = s;
}
}
Calender() {
}
// API
public void addEvent(Event e) {
// todo
}
// driver function
public static void main(String[] args) {
Calender calender = new Calender();
Event event = new Event("event");
calender.addEvent(event);
}
}
version2:
public class Calender {
int systemId;
// helper class
public static class Event {
String name;
Event(String s) {
name = s;
}
}
Calender() {
}
// API
public int addEvent(String s) {
Event event = new Event(s);
systemId++;
return systemId - 1; // after add an event, return the unique id for it
}
// driver function
public static void main(String[] args) {
Calender calender = new Calender();
int id = calender.addEvent("a");
}
}
The above class is not completed. The major difference is that do we need to let the user create an Event then add this instance or just using existing API to create a event and get a unique id for this event?
Upvotes: 1
Views: 1746
Reputation: 506
Calendar
and Event
, as I identify, are two different, self contained entities.
Although an Event may be dependent on the Calendar object to store its date/time specific. They should be independent, and may not be wrapped within a Calendar API.
That being said, the version2 looks more promising, and scalable. Version2 gives you flexibility to modify the Event class to extend the capabilities, modify constructors, or add methods easily, without affecting Calendar .
For example, if you need to add the Event's venue along with its Name, you can simply modify Event's constructor. Or add another method inside the Event class. Leaving Calendar unaffected.
On a different design point, the Event class can also extend Calendar, if the Calendar use case is only specific to adding date/time capabilities to an Event.
Upvotes: 1
Reputation: 4694
I would keep both methods. Allow the user to either insert a new Event
object or just pass the string and add it.
public class Calendar {
public static class Event {
String name;
Event(String s) {
name = s;
}
}
Calendar() {
}
public void addEvent(Event e) {
// todo
}
public int addEvent(String s) {
Event event = new Event(s);
systemId++;
return systemId - 1; // after add an event, return the unique id for it
}
}
That said, I would do the following:
main
from the Calendar
class, it should be in a separate Application
class for example, that would instantiate your calendar.id
to your Event
class, fill it with the new ID, and return the whole Event
object.Event
class in a separate file instead of having it in Calendar
. It's arguable if it makes sense or not so that's left to your appreciation.Beside that, you should store the list of events in your calendar, in order to be able to return the whole list of events or to delete one, as stated by the subject of your exercise.
Upvotes: 2