How to design a Calendar
It is an OOD question. Design a calendar.
The requirement are:
- add event
- delete an event
- get all events for a particular day
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?
java algorithm oop system
add a comment |
It is an OOD question. Design a calendar.
The requirement are:
- add event
- delete an event
- get all events for a particular day
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?
java algorithm oop system
3
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44
add a comment |
It is an OOD question. Design a calendar.
The requirement are:
- add event
- delete an event
- get all events for a particular day
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?
java algorithm oop system
It is an OOD question. Design a calendar.
The requirement are:
- add event
- delete an event
- get all events for a particular day
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?
java algorithm oop system
java algorithm oop system
edited Nov 20 '18 at 11:23
Joris Schellekens
6,16011242
6,16011242
asked Nov 20 '18 at 9:30
TianboTianbo
276
276
3
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44
add a comment |
3
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44
3
3
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44
add a comment |
2 Answers
2
active
oldest
votes
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:
- Remove the
main
from theCalendar
class, it should be in a separateApplication
class for example, that would instantiate your calendar. - Instead of returning the ID of the event when you add it, add a field
id
to yourEvent
class, fill it with the new ID, and return the wholeEvent
object. - Maybe put the
Event
class in a separate file instead of having it inCalendar
. 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.
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389946%2fhow-to-design-a-calendar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
- Remove the
main
from theCalendar
class, it should be in a separateApplication
class for example, that would instantiate your calendar. - Instead of returning the ID of the event when you add it, add a field
id
to yourEvent
class, fill it with the new ID, and return the wholeEvent
object. - Maybe put the
Event
class in a separate file instead of having it inCalendar
. 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.
add a comment |
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:
- Remove the
main
from theCalendar
class, it should be in a separateApplication
class for example, that would instantiate your calendar. - Instead of returning the ID of the event when you add it, add a field
id
to yourEvent
class, fill it with the new ID, and return the wholeEvent
object. - Maybe put the
Event
class in a separate file instead of having it inCalendar
. 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.
add a comment |
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:
- Remove the
main
from theCalendar
class, it should be in a separateApplication
class for example, that would instantiate your calendar. - Instead of returning the ID of the event when you add it, add a field
id
to yourEvent
class, fill it with the new ID, and return the wholeEvent
object. - Maybe put the
Event
class in a separate file instead of having it inCalendar
. 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.
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:
- Remove the
main
from theCalendar
class, it should be in a separateApplication
class for example, that would instantiate your calendar. - Instead of returning the ID of the event when you add it, add a field
id
to yourEvent
class, fill it with the new ID, and return the wholeEvent
object. - Maybe put the
Event
class in a separate file instead of having it inCalendar
. 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.
answered Nov 20 '18 at 9:36
AntoineBAntoineB
2,30811742
2,30811742
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 '18 at 9:46
ShariqShariq
374112
374112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389946%2fhow-to-design-a-calendar%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
I'm voting to close this question as off-topic because it seems like it would be a better fit for codereview.stackexchange.com
– DaveyDaveDave
Nov 20 '18 at 9:44