Skip to content

Feedback #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/main/java/com/redi/j2/Appointment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.redi.j2;

import java.time.LocalDateTime;

public class Appointment implements Comparable{

private String title;
private LocalDateTime date;
private int duration;
private String location;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public LocalDateTime getDate() {
return date;
}

public void setDate(LocalDateTime date) {
this.date = date;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public Appointment(String title, LocalDateTime date, int duration, String location) {
this.title = title;
this.date = date;
this.duration = duration;
this.location = location;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Appointment{");
sb.append("title='").append(title).append('\'');
sb.append(", date=").append(date);
sb.append(", duration=").append(duration);
sb.append(", location='").append(location).append('\'');
sb.append('}');
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Appointment that = (Appointment) o;

if (getDuration() != that.getDuration()) return false;
if (getTitle() != null ? !getTitle().equals(that.getTitle()) : that.getTitle() != null) return false;
if (getDate() != null ? !getDate().equals(that.getDate()) : that.getDate() != null) return false;
return getLocation() != null ? getLocation().equals(that.getLocation()) : that.getLocation() == null;
}

@Override
public int hashCode() {
int result = getTitle() != null ? getTitle().hashCode() : 0;
result = 31 * result + (getDate() != null ? getDate().hashCode() : 0);
result = 31 * result + getDuration();
result = 31 * result + (getLocation() != null ? getLocation().hashCode() : 0);
return result;
}

@Override
public int compareTo(Object o) {
if (! (o instanceof Appointment)) return -1;
return date.compareTo(((Appointment)o).getDate());
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/redi/j2/Calendar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.redi.j2;

import java.util.*;

public class Calendar {

private String owner;
private List<Appointment> appointments;

public String getOwner() {
return owner;
}

public void setOwner(String owner) {
this.owner = owner;
}

public Calendar(String owner) {
this.owner = owner;
this.appointments = new ArrayList<>();
}

public void addAppointment(Appointment a) {
if (appointments.contains(a) == false) {
appointments.add(a);
}
}

public List<Appointment> getAllAppointments() {
Collections.sort(appointments);
return appointments;
}

public List<Appointment> getAppointmentsOrderByTitle() {
appointments.sort(Comparator.comparing(Appointment::getTitle));
return appointments;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Calendar calendar = (Calendar) o;

if (getOwner() != null ? !getOwner().equals(calendar.getOwner()) : calendar.getOwner() != null) return false;
return Objects.equals(appointments, calendar.appointments);
}

@Override
public int hashCode() {
int result = getOwner() != null ? getOwner().hashCode() : 0;
result = 31 * result + (appointments != null ? appointments.hashCode() : 0);
return result;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Calendar{");
sb.append("owner='").append(owner).append('\'');
sb.append(", appointments=").append(appointments);
sb.append('}');
return sb.toString();
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/redi/j2/CalendarService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.redi.j2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalendarService {

private List<Calendar> calendars;

public CalendarService() {
calendars = new ArrayList<>();
}

public void addCalendar(Calendar c) {
if (calendars.contains(c)) return;
calendars.add(c);
}

public List<Appointment> getAppointmentsOrderByDateAndOwner() {

Map<Appointment, String> ownerMapping = new HashMap<>();
List<Appointment> allAppointments = new ArrayList<>();

for (Calendar c : calendars) {
for (Appointment a: c.getAllAppointments()) {
ownerMapping.put(a, c.getOwner());
allAppointments.add(a);
}
}

allAppointments.sort((o1, o2) -> {
return o1.getDate().compareTo(o2.getDate()) * 10 +
ownerMapping.get(o1).compareTo(ownerMapping.get(o2));
});
Comment on lines +23 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution would be much better using a Map, which will be shown in future classes


return allAppointments;
}
}