Question

I have to am having trouble writing MyCalendar class... (see: What I have written so far)...

I have to am having trouble writing MyCalendar class... (see: What I have written so far)

The methods that must be used are:

boolean add​(Event evt)

Add an event to the calendar

Event get​(int i)

Fetch the ith Event added to the calendar

Event get​(java.lang.String name)

Fetch the first Event in the calendar whose eventName is equal to the given name

java.util.ArrayList<Event> list()

The list of all Events in the order that they were inserted into the calendar

int size()

The number of events in the calendar

**********What I have written so far***********************

import java.util.ArrayList;

public class MyCalendar {

private ArrayList<Event> calendar;

//no-arg constructor

public MyCalendar() {

}

//add event to the calendar. evt -- the event to be added to the calendar. Returns true is the event is successfully added, false otherwise

public boolean add​(Event evt) {

int x = 0;

for(int i = 0; i < calendar.size(); i++) {

x = calendar.size() + i;

}

if( x > calendar.size()) {

return true;

} else {

return false;

}

}

// i -- the index within the calendar ArrayList of the Event to be returned

//Returns the i th Event in the calendar, or null if no such Event exist

public Event get​(int i) {

return calendar.get(i);

}

//name -- the Event name of the Event within the ArrayList to be returned

//Returns: the Event in the calendar whose name is equal to the given name, or null if no such Event exists.

public Event get​(java.lang.String name) {

String y = name;

int index = -1;

for(int i = 0; i <calendar.size(); i++) {

if(calendar.get(i).equals(name)) {

index = i;

return calendar.get(index);

} else {

return null;

}

}

}

//Returns : the number of events in the calendar

public int size() {

return calendar.size();

}

//Returns: list of events

public java.util.ArrayList<Event> list() {

return calendar;

}

public java.lang.String toString() {

return "There are a total of " + calendar.size() + " Events on the Calendar and their names are " + calendar;

}

}

I have to create a separate MyCalendarTester class that tests that the calendar’s get, size, list, etc. methods work correctly. I can do this, I just need help with the class above.

MyCalendar works off this even class below (this works fine and I don't need help with this... I only need help with the MyCalendar class above)

public class Event {

String name;

String venue;

String eventDate;

int ticketsSold;

int ticketPrice;

int overhead;

int numberOfTickets = 0;

public Event() {

}

public Event(String eventName, String eventVenue) {

this.name = eventName;

this.venue = eventVenue;

}

public Event(String eventName,

String eventVenue,

String date,

int ticketsSold,

int ticketPrice,

int overhead) {

this.name = eventName;

this.venue = eventVenue;

this.ticketsSold = ticketsSold;

this.ticketPrice = ticketPrice;

this.overhead = overhead;

this.eventDate = date;

}

public int compareDate(Event other) {

String x = other.getDate();

String[] arrX = x.split("-");

String y = this.getDate();

String[] arrY = y.split("-");

int a = Integer.parseInt(arrX[0]);

int a1 = Integer.parseInt(arrX[1]);

int a2 = Integer.parseInt(arrX[2]);

int b = Integer.parseInt(arrY[0]);

int b1 = Integer.parseInt(arrY[1]);

int b2 = Integer.parseInt(arrX[2]);

if(x == y) {

return 0;

}

else if(b > a){

return 1;

}

else if(b == a && b1 > a1 ) {

return 1;

}

else if(b == a && b1 == a1 && b2 > a2 ) {

return 1;

} else {

return -1;

}

}

public int compareName(Event other) {

String tN = this.getEventName();

String oN = other.getEventName();

int x = tN.compareToIgnoreCase(oN);

if(x == 0) {

return 0;

}else if (x > 0) {

return 1;

}else {

return -1;

}

}

public int compareProfit(Event other) {

int tP = this.getProfit();

int oP = other.getProfit();

if(tP == oP) {

return 0;

} else if(tP > oP) {

return 1;

} else {

return -1;

}

}

public int getBreakEvenPoint() {

return overhead/ticketPrice;

}

public String getDate() {

return eventDate;

}

public String getEventName() {

return name;

}

public String getEventVenue() {

return venue;

}

public int getOverhead() {

return overhead;

}

public int getProfit() {

return (ticketPrice * ticketsSold) - overhead;

}

public int getTicketPrice() {

return ticketPrice;

}

public int getTicketsSold() {

return ticketsSold;

}

public boolean sellTickets​(int numberOfTickets) {

this.ticketsSold = ticketsSold + this.numberOfTickets;

if(this.ticketsSold > ticketsSold) {

return true;

} else {

return false;

}

}

public void setDate​(String date) {

this.eventDate = date;

}

public void setEventName​(String eventName) {

this.name = eventName;

}

public void setEventVenue​(String eventVenue) {

this.venue = eventVenue;

}

public void setOverhead​(int overhead) {

this.overhead = overhead;

}

public void setTicketPrice​(int ticketPrice) {

this.ticketPrice = ticketPrice;

}

public void setTicketsSold​(int ticketsSold) {

if(this.ticketsSold < 0) {

ticketsSold = 0;

} else {

this.ticketsSold = ticketsSold;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the fixed code for this problem. Since the exact guidelines are not mentioned, I have written this according to my understandings. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MyCalendar.java

import java.util.ArrayList;

public class MyCalendar {

                private ArrayList<Event> calendar;

                // no-arg constructor

                public MyCalendar() {

                                // initializing calendar

                                calendar = new ArrayList<Event>();

                }

                // add event to the calendar. evt -- the event to be added to the calendar.

                // Returns true is the event is successfully added, false otherwise

                public boolean add(Event evt) {

                                // simply calling add method of array list to add the event at the end,

                                // it returns true if addition is successful

                                return calendar.add(evt);

                }

                // Fetch the ith Event added to the calendar

                public Event get(int i) {

                                if (i < 0 || i > calendar.size()) {

                                               // invalid index. you may throw an exception here if you need

                                               return null;

                                }

                                // returning ith element

                                return calendar.get(i);

                }

                // name -- the Event name of the Event within the ArrayList to be returned

                // Returns: the Event in the calendar whose name is equal to the given name,

                // or null if no such Event exists.

                public Event get(java.lang.String name) {

                                // looping through calendar

                                for (int i = 0; i < calendar.size(); i++) {

                                               // checking if current event's name is target name

                                               if (calendar.get(i).getEventName().equals(name)) {

                                                               // returning current element

                                                               return calendar.get(i);

                                               }

                                }

                                // not found

                                return null;

                }

                // Returns : the number of events in the calendar

                public int size() {

                                return calendar.size();

                }

                // Returns: list of events

                public java.util.ArrayList<Event> list() {

                                // if you wish to return a copy of the list instead of original, you may

                                // use the below statement.

                                // return new ArrayList<Event>(calendar);

                                return calendar;

                }

                public java.lang.String toString() {

                                // since you did not provide any information about the format of this

                                // method, I'm just letting it return the size of the list and names of

                                // all events

                                String data = "There are a total of " + calendar.size()

                                                               + " Events on the Calendar";

                                // appending ' and their names are ' only if the list is not empty

                                if (calendar.size() > 0) {

                                               data += " and their names are ";

                                }

                                // appending names of all events to the data string before returning it

                                for (int i = 0; i < calendar.size(); i++) {

                                               data += "\n" + calendar.get(i).getEventName();

                                }

                                return data;

                }

}

Add a comment
Know the answer?
Add Answer to:
I have to am having trouble writing MyCalendar class... (see: What I have written so far)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT