Java expert please help me the program below. Here is requirement from my profes
ID: 3799837 • Letter: J
Question
Java expert please help me the program below. Here is requirement from my professor:
You are to write the following PersonalCalendar, Event and Appointment classes and other interfaces or classes as necessary. Note that there is no main method required in this assignment; a JUnit test will be used instead.
Some details of the implementation are left to you. Other details are implied, but not stated directly, in the description. (That said, don't over think this; do the minimum required to perform the specified function.)
You are to write a class PersonalCalendar. Entries in a PersonalCalendar can be either an Event or anAppointment. Entries can be added to, returned from and removed from the PersonalCalendar.
An Event has a date consisting of ints for year, month, dayOfMonth, hour and minute. It also has a Stringdescription, a String location and an int duration in minutes. Events do not have attendees. Two Eventobjects are equal if their dates and descriptions are equal.
An Appointment has a date consisting of ints for year, month, dayOfMonth, hour and minute. It has an int duration in minutes and a String description. An Appointment also has an ArrayList of Strings for attendees. Attendees can be added to an Appointment. An Appointment does not have a location--that's assumed to be my office. Appointment has an equals method that compares dates and descriptions to determine equality.
A PersonalCalendar maintains its entries sorted by date, not the order the entries were inserted. Thus the first element of a PersonalCalendar is always the fist element chronologically, regardless of the order the elements were added. If two elements have exactly the same date and time, description is used to break the tie. (Hint: GregorianCalendar and Collections.sort. Also let your IDE help you write commonly used code.)
You are to write a JUnit test to verify that a PersonalCalendar object maintains its entries in the correct order.
Explanation / Answer
public class ScheduleRemainder implements Comparable<ScheduleRemainder> {
public int year;
public int month;
public int dayofMonth;
public int hour;
public int minute;
public int durationinMinutes;
public ScheduleRemainder(int year, int month, int dayofMonth, int hour,
int minute, int durationinMinutes) {
this.year = year;
this.month = month;
this.dayofMonth = dayofMonth;
this.hour = hour;
this.minute = minute;
this.durationinMinutes = durationinMinutes;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + dayofMonth;
result = prime * result + durationinMinutes;
result = prime * result + hour;
result = prime * result + minute;
result = prime * result + month;
result = prime * result + year;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ScheduleRemainder other = (ScheduleRemainder) obj;
if (dayofMonth != other.dayofMonth)
return false;
if (durationinMinutes != other.durationinMinutes)
return false;
if (hour != other.hour)
return false;
if (minute != other.minute)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
public int compareTo(ScheduleRemainder args) {
if (this.year != args.year) {
return this.year - args.year;
} else {
if (this.month != args.month)
return this.month - args.year;
else {
if (this.dayofMonth != args.dayofMonth) {
return this.dayofMonth - args.dayofMonth;
} else {
if (this.hour != args.hour) {
return this.hour - args.hour;
} else {
if (this.minute != args.minute) {
return this.minute - args.minute;
}
}
}
}
}
return 0;
}
}
******************************************************************************************************
public class Event extends ScheduleRemainder{
public String description;
public String location;
public Event(int year, int month, int dayofMonth, int hour, int minute,
int durationinMinutes, String description, String location) {
super(year, month, dayofMonth, hour, minute, durationinMinutes);
this.description = description;
this.location = location;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result
+ ((location == null) ? 0 : location.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Event other = (Event) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (location == null) {
if (other.location != null)
return false;
} else if (!location.equals(other.location))
return false;
return true;
}
@Override
public String toString() {
return "Event [description=" + description + ", location=" + location
+ ", year=" + year + ", month=" + month + ", dayofMonth="
+ dayofMonth + ", hour=" + hour + ", minute=" + minute
+ ", durationinMinutes=" + durationinMinutes + "]";
}
}
************************************************************************************************************************
import java.util.ArrayList;
import java.util.List;
public class Appointment extends ScheduleRemainder {
public String description;
List<Attendee> attendees;
public Appointment(int year, int month, int dayofMonth, int hour,
int minute, int durationinMinutes, String description,
List<Attendee> attendes) {
super(year, month, dayofMonth, hour, minute, durationinMinutes);
this.description = description;
this.attendees = new ArrayList<Attendee>();
}
public void addAttendees(Attendee attendee)
{
int count=0;
for(Attendee a:attendees)
{
if(a.equals(attendee))
{
++count;
}
}
if(count==0)
{
attendees.add(attendee);
}else
System.out.println("already added");
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((attendees == null) ? 0 : attendees.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Appointment other = (Appointment) obj;
if (attendees == null) {
if (other.attendees != null)
return false;
} else if (!attendees.equals(other.attendees))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
return true;
}
@Override
public String toString() {
return "Appointment [description=" + description + ", attendees="
+ attendees + ", year=" + year + ", month=" + month
+ ", dayofMonth=" + dayofMonth + ", hour=" + hour + ", minute="
+ minute + ", durationinMinutes=" + durationinMinutes + "]";
}
}
*************************************************************************************************************
public class Attendee {
public String attendeeName;
public Attendee(String attendeeName) {
this.attendeeName = attendeeName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((attendeeName == null) ? 0 : attendeeName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Attendee other = (Attendee) obj;
if (attendeeName == null) {
if (other.attendeeName != null)
return false;
} else if (!attendeeName.equals(other.attendeeName))
return false;
return true;
}
@Override
public String toString() {
return "Attendee [attendeeName=" + attendeeName + "]";
}
}
*******************************************************************************************************************
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PersonCalendar {
List<ScheduleRemainder> schedules;
public PersonCalendar() {
this.schedules = new ArrayList<ScheduleRemainder>();
}
public void add(ScheduleRemainder r) {
int count = 0;
for (ScheduleRemainder remainder : schedules) {
if (remainder.equals(r)) {
++count;
}
}
if (count == 0) {
schedules.add(r);
} else
System.out.println("already added the event");
}
public void showList(List<ScheduleRemainder> schedules) {
for (ScheduleRemainder remainder : schedules) {
System.out.println(remainder);
}
}
public void sortedByDate(List<ScheduleRemainder> schedules) {
Collections.sort(schedules);
for (ScheduleRemainder remainder : schedules) {
System.out.println(remainder);
}
}
public static void main(String[] args) {
PersonCalendar calendar = new PersonCalendar();
ScheduleRemainder r = new Event(2017, 04, 25, 10, 36, 60,
"success meet", "new york");
calendar.add(r);
List<Attendee> attendees = new ArrayList<Attendee>();
Attendee a = new Attendee("david");
Attendee a1 = new Attendee("mark");
Attendee a2 = new Attendee("smith");
Attendee a3 = new Attendee("alice");
attendees.add(a);
attendees.add(a1);
attendees.add(a2);
attendees.add(a3);
ScheduleRemainder r1 = new Appointment(2015, 11, 02, 05, 30, 30,
"private appointment", attendees);
calendar.add(r1);
System.out.println("before sorting by date");
calendar.showList(calendar.schedules);
System.out
.println("***************************************************************");
System.out.println("after sorting by date");
calendar.sortedByDate(calendar.schedules);
}
}
output
before sorting by date
Event [description=success meet, location=new york, year=2017, month=4, dayofMonth=25, hour=10, minute=36, durationinMinutes=60]
Appointment [description=private appointment, attendees=[], year=2015, month=11, dayofMonth=2, hour=5, minute=30, durationinMinutes=30]
***************************************************************
after sorting by date
Appointment [description=private appointment, attendees=[], year=2015, month=11, dayofMonth=2, hour=5, minute=30, durationinMinutes=30]
Event [description=success meet, location=new york, year=2017, month=4, dayofMonth=25, hour=10, minute=36, durationinMinutes=60]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.