3

I am fairly new to object oriented programming and I am curious to know at a high level how to solve this problem.

If I have a list of different calendars i.e. work calendar, personal calendar, sports calendars, etc and each of them have there own respective events. I assume I would have a calendar class and then the nested class would be the event class. Since I want to display all of the events from all the calendars on one list they would all go into on sorted array. But lets say I wanted to delete one of my calendars like my work calendar how would I ensure that all the work events were deleted from this list/array. I am curious to how this class structure would be set up at a high level. Any help would be greatly appreciated.

4

4 回答 4

1

这是我将如何去做的,突出显示的部分是我要学习的:

如果我有一个不同的列表,calendarswork calendar, personal calendar,sports calendars等,并且他们每个人都有自己的events. 因为我想在一个上显示所有日历中的所有事件,所以list它们都将进入排序数组。但是假设我想delete one of my calendars喜欢我的工作日历,我将如何确保所有work events were deleted来自这个列表/数组。

这给了我:

emum CalendarType
{
    WORK,
    PERSONAL,
    SPORT,
}

class Event
{
    // event data, like time, place, people, etc...
    private Calendar owner;
}

class Calendar
{
    private CalendarType type; // or make this a String and get rid of the enum 
                               // if people can make their own types
    private List<Event> event;s
}

关于希望将所有事件放在一个列表中并在日历有点困难时删除它们的部分。

我想我会在CalendarUtils类中添加这样的方法:

public static void deleteEvents(Calendar c, List<Event> events)
{
    // loop through all the events, for each one from the calendar remove it
}
于 2013-07-24T07:39:50.820 回答
0

如果我要满足您的要求,我将有以下阵容:

interface Calendar 
class DefaultCalendar implements Calendar //<- default implementation for calendar. will hold the implementation for the common behavior for all the calendars
class PersonalCalendar implements Calendar //<- implementation for calendar that holds personal events, decorator for DefaultCalendar
class SportsCalendar implements Calendar //<- implementation for calendar that holds sports events, decorator for DefaultCalendar
class WorkCalendar implements Calendar //<- implementation for calendar that holds work events, decorator for DefaultCalendar
class Event //<- the event object, setters & getters
class CalendarManager //<- object for adding, removing calendars

我将使用TreeMapwhich 是SortedMap利用要放置的事件的自然顺序的实现。

Map<String, Calendar> calendars = new TreeMap<String, Calendar>();

我还将在事件名称前加上特定于日历的单词,以便在显示时按日历类型(因此,事件类型)对事件进行分组

@Override public void addEvent(Event event){
    //event.setName("Work Event : " + event.getEventName()); for work event
    //event.setName("Sports Event : " + event.getEventName()); for sports event
    //event.setName("Personal Event : " + event.getEventName()); for personal event
    calendar.addEvent(event);
}

因此,当要求经理显示所有事件时,将导致

Personal Event : blah blah blah abc 123
Personal Event : blah blah blah abc 123
Sports Event : blah blah blah abc 123
Work Event : blah blah blah abc 123

现在,为了证明我在说什么,这是我的可运行代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

/* the demo class */

public class CalendarDemo {
    public static void main(String[] args) throws ParseException{
        CalendarManager manager = new CalendarManager();

        Calendar mySportsCalendar = new SportsCalendar(new DefaultCalendar());
        Calendar myWorkCalendar = new WorkCalendar(new DefaultCalendar());
        Calendar myPersonalCalendar = new PersonalCalendar(new DefaultCalendar());

        Event myBirthday = new Event("My 23rd Birthday Celebration", "September 17, 2013", "This marks my 23rd birthday celebration!");
        Event myMothersBirthday = new Event("My Mother's 50th Birthday Celebration", "December 19, 2013", "This marks my mothers 50th birthday celebration!");

        Event tennisGame = new Event("Tennis Game With Denver", "August 11, 2013", "3rd tennis game with my nephew.");

        Event newProjectMeeting = new Event("New Project Meeting", "August 11, 2013", "Meeting with our new project.");

        myPersonalCalendar.addEvent(myBirthday);
        myPersonalCalendar.addEvent(myMothersBirthday);
        myWorkCalendar.addEvent(newProjectMeeting);
        mySportsCalendar.addEvent(tennisGame);

        manager.addCalendar("sportsCalendar", mySportsCalendar);
        manager.addCalendar("workCalendar", myWorkCalendar);
        manager.addCalendar("personalCalendar", myPersonalCalendar);

        System.out.println("Before the removal of personal calendar : ");
        System.out.println("--------------------------------------------------------------------------------");
        System.out.println(manager.displayAllCalendarEvents());

        manager.removeCalendar("personalCalendar"); //<- will delete all the events in personalCalendar;

        System.out.println("After the removal of personal calendar : ");
        System.out.println("--------------------------------------------------------------------------------");
        System.out.println(manager.displayAllCalendarEvents());
    }
}

/* the calendar interface */

interface Calendar{
    Event getEvent(String eventName);
    List<Event> getEvents();
    void addEvent(Event event);
    String getDescription();
    void setDescription(String desc);
}

/* the default implementation of calendar */

class DefaultCalendar implements Calendar{

    private List<Event> events;
    private String description;

    public DefaultCalendar(){
        events = new ArrayList<Event>();
    }

    @Override public Event getEvent(String eventName) {

        for(Event event : events){
            if(event.getEventName().equals(eventName))
                return event;
        }

        throw new IllegalArgumentException("Event name does not exist in the event list.");
    }

    @Override public void addEvent(Event event) {
        events.add(event);
    }

    @Override public String getDescription(){
        return description;
    }

    @Override public void setDescription(String desc){
        description = desc;
    }

    @Override public List<Event> getEvents() {
        return events;
    }
}

/* decorators for defaultcalendar. If you want
 * specific behavior for a specific calendar, you can
 * specify it in these classes.
 * ---------------------------------------------- */

class SportsCalendar implements Calendar{

    private Calendar calendar;

    public SportsCalendar(Calendar baseCalendar){
        calendar = baseCalendar;
    }

    @Override public String getDescription(){
        return calendar.getDescription();
    }

    @Override public void addEvent(Event event){
        event.setEventName("Sports Event : " + event.getEventName());
        calendar.addEvent(event);
    }

    @Override public Event getEvent(String eventName) {
        return calendar.getEvent("Sports Event : " + eventName);
    }

    @Override public void setDescription(String desc) {

        //do something to the passed desc
        //...
        //then pass it to the wrapped calendar;

        calendar.setDescription(desc);
    }

    @Override public List<Event> getEvents() {
        return calendar.getEvents();
    }
}

/* another decorator */

class WorkCalendar implements Calendar{

    private Calendar calendar;

    public WorkCalendar(Calendar baseCalendar){
        calendar = baseCalendar;
    }

    @Override public String getDescription(){
        return calendar.getDescription();
    }

    @Override public void addEvent(Event event){
        event.setEventName("Work Event : " + event.getEventName());
        calendar.addEvent(event);
    }

    @Override public Event getEvent(String eventName) {
        return calendar.getEvent("Work Event : " + eventName);
    }

    @Override public void setDescription(String desc){

        //do something to the passed desc
        //...
        //then pass it to the wrapped calendar;

        calendar.setDescription(desc);
    }

    @Override public List<Event> getEvents() {
        return calendar.getEvents();
    }
}

/* another decorator */

class PersonalCalendar implements Calendar{

    private Calendar calendar;

    public PersonalCalendar(Calendar baseCalendar){
        calendar = baseCalendar;
    }

    @Override public String getDescription(){
        return calendar.getDescription();
    }

    @Override public void addEvent(Event event){
        event.setEventName("Personal Event : " + event.getEventName());
        calendar.addEvent(event);
    }

    @Override public Event getEvent(String eventName) {
        return calendar.getEvent("Personal Event : " + eventName);
    }

    @Override public void setDescription(String desc) {

        //do something to the passed desc
        //...
        //then pass it to the wrapped calendar;

        calendar.setDescription(desc);
    }

    @Override public List<Event> getEvents() {
        return calendar.getEvents();
    }
}

/* the calendar manager, for adding & removing calendars */

class CalendarManager{

    private Map<String, Calendar> calendars;

    public CalendarManager(){
        calendars = new TreeMap<String, Calendar>();
    }

    public void addCalendar(String key, Calendar calendar){
        calendars.put(key, calendar);
    }

    public void removeCalendar(String key){
        calendars.remove(key);
    }

    public String displayAllCalendarEvents(){
        StringBuilder bdr = new StringBuilder();

        for(String key : calendars.keySet()){
            for(Event event : calendars.get(key).getEvents()){
                bdr.append(event.getEventName())
                .append("\t")
                .append(event.getEventDate())
                .append("\t")
                .append(event.getEventDescription())
                .append("\n");
            }
        }

        return bdr.toString();
    }
}

/* the event object */

class Event{

    private String name;
    private Date date;
    private String description;

    public Event(String name, String date, String description){
        this.name = name;
        try {
            this.date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("December 19, 2013");
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format!");
        }
        this.description = description;
    }

    public String getEventName() {
        return name;
    }

    public Date getEventDate() {
        return date;
    }

    public String getEventDescription() {
        return description;
    }

    public void setEventName(String name) {
        this.name = name;
    }

    public void setEventDate(String date) {
        try {
            this.date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("December 19, 2013");
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format!");
        }
    }

    public void setEventDescription(String description) {
        this.description = description;
    }
}
于 2013-07-24T04:12:10.410 回答
0
public abstract class BaseCalendar{//do some common functionality here or define the abstract methods so child class will provide the implementation }
public class WorkCalendar extends BaseCalendar{//do WorkCalendar specific functionality here}
public class PersonalCalendar extends BaseCalendar{//do Personalcalendar specific functionality here}
public class SportsCalendar extends BaseCalendar{//do Sportscalendar specific functionality here}
create another class for Events and create list of events for each calendar(use aggrgation here, once your calendar is destroyed your events will also destroyed)

如果您的每个日历都有不同的行为,这将是我将采取的方法。

于 2013-07-24T03:03:06.970 回答
0

你不一定需要嵌套你的类。正如 MadProgrammer 建议的那样,只需创建一个类,即一个事件,将其实现到您的 Calendar 类并根据需要使用它。如果你想删除一个日历,你可能会包含一些方法来确保从该日历中删除所有事件。但只需删除日历对象就可以了。

于 2013-07-24T02:34:01.727 回答