Skip to content

Instantly share code, notes, and snippets.

@NielsPilgaard
Created November 17, 2016 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NielsPilgaard/b5e8c0584b17efdf4441219ea156c5b7 to your computer and use it in GitHub Desktop.
Save NielsPilgaard/b5e8c0584b17efdf4441219ea156c5b7 to your computer and use it in GitHub Desktop.
package application.service;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import application.model.Conference;
import application.model.Excursion;
import application.model.Hotel;
import application.model.HotelReservation;
import application.model.HotelService;
import application.model.Participant;
import application.model.Person;
import application.model.Registration;
import application.model.Role;
import application.model.Speaker;
import storage.Storage;
public class Service {
private static int calcTotalDaysSpentAtConference(LocalDate arrivalDate, LocalDate departureDate) {
int daysSpentAtConference = 1;
daysSpentAtConference += (int) ((ChronoUnit.DAYS.between(arrivalDate, departureDate)));
// System.out.println("Participation days is " + daysSpentAtConference);
return daysSpentAtConference;
}
private static double calcConferenceParticipationCost(Registration aRegistration, int daysSpentAtConference,
Role conferenceRole) {
double participationCost = 0;
if (conferenceRole instanceof Participant) {
participationCost += aRegistration.getConference().getParticipationPrice() * daysSpentAtConference;
}
// System.out.println("Participation cost is " + participationCost);
return participationCost;
}
private static double calcCompanionParticipationCost(ArrayList<Excursion> excursionsChoosen) {
double excursionTotalCost = 0;
for (Excursion e : excursionsChoosen) {
excursionTotalCost += e.getExcursionPrice();
}
// System.out.println("Companion cost is " + excursionTotalCost);
return excursionTotalCost;
}
private static double calcTotalHotelCost(HotelReservation aHotelReservation, int daysSpentAtConference) {
double totalHotelCost = 0;
if (aHotelReservation.wantsSingleRoom()) {
totalHotelCost += aHotelReservation.getHotel().getSingleRoomPrice() * (daysSpentAtConference - 1);
} else {
totalHotelCost += aHotelReservation.getHotel().getDoubleRoomPrice() * (daysSpentAtConference - 1);
}
for (HotelService s : aHotelReservation.getServicesChosen()) {
totalHotelCost += s.getServicePrice() * (daysSpentAtConference - 1);
}
// System.out.println("Hotel cost is " + totalHotelCost);
return totalHotelCost;
}
public static double calculateTotalConferencePrice(Registration aRegistration) {
int daysSpentAtConference = calcTotalDaysSpentAtConference(aRegistration.getArrivalDate(),
aRegistration.getDepartureDate());
double totalPrice = 0;
totalPrice += calcConferenceParticipationCost(aRegistration, daysSpentAtConference,
aRegistration.getParticipantRole());
if (aRegistration.getCompanion() != null) {
totalPrice += calcCompanionParticipationCost(aRegistration.getChosenExcursions());
}
if (aRegistration.getHotelReservation() != null) {
totalPrice += calcTotalHotelCost(aRegistration.getHotelReservation(), daysSpentAtConference);
}
return totalPrice;
}
/*
* TO-DO ret i metodekald
*/
public static void initStorage() {
ArrayList<Excursion> havOgHimmelExcusrions = new ArrayList<>();
havOgHimmelExcusrions.add(new Excursion("Byrundtur, Odense, incl. frokost", 125));
havOgHimmelExcusrions.add(new Excursion("Egeskov", 75));
havOgHimmelExcusrions.add(new Excursion("Trapholt Museum, Kolding, incl. frokost", 200));
Conference havOgHimmel = new Conference("Hav og Himmel", 1500, LocalDate.of(2017, 4, 18),
LocalDate.of(2017, 4, 20), havOgHimmelExcusrions, "Hav og himmel");
Hotel hvideSvane = new Hotel("Den Hvide Svane", "incl. bath", "5", "", "", 1050, 1250);
Hotel hotelPhoenix = new Hotel("Hotel Phønix", "Hotel Phønix", "3", "", "", 700, 800);
Hotel pensionTusindfryd = new Hotel("Pension Tusindfryd", "Hotel Tusindfryd", "2", "", "", 500, 600);
hvideSvane.createService("WiFi", 50);
hotelPhoenix.createService("Bath", 200);
hotelPhoenix.createService("WiFi", 75);
pensionTusindfryd.createService("Breakfast", 100);
Person madsen = new Person("Finn Madsen", "", "", "", "");
Person petersen = new Person("Niels Petersen", "", "", "", "");
Person sommer = new Person("Peter Sommer", "", "", "", "");
Person jensen = new Person("Lone Jesnen", "", "", "", "");
Registration r1 = new Registration(madsen, havOgHimmel, LocalDate.of(2017, 4, 18), LocalDate.of(2017, 4, 20),
new Participant());
Registration r2 = new Registration(petersen, havOgHimmel, LocalDate.of(2017, 4, 18), LocalDate.of(2017, 4, 20),
new Participant());
Registration r3 = new Registration(sommer, havOgHimmel, LocalDate.of(2017, 4, 18), LocalDate.of(2017, 4, 20),
new Participant());
Registration r4 = new Registration(jensen, havOgHimmel, LocalDate.of(2017, 4, 18), LocalDate.of(2017, 4, 20),
new Speaker());
r2.setHotelReservation(new HotelReservation(hvideSvane, new ArrayList<HotelService>(), r2, true));
ArrayList<HotelService> sommerChoosenHotelService = new ArrayList<>();
sommerChoosenHotelService.add(hvideSvane.getServices().get(0));
r3.setHotelReservation(new HotelReservation(hvideSvane, sommerChoosenHotelService, r3, false));
r3.createCompanion("Mie Sommer");
r3.addExcursion(havOgHimmelExcusrions.get(1));
r3.addExcursion(havOgHimmelExcusrions.get(2));
ArrayList<HotelService> jensenChoosenHotelService = new ArrayList<>();
jensenChoosenHotelService.add(hvideSvane.getServices().get(0));
r4.setHotelReservation(new HotelReservation(hvideSvane, jensenChoosenHotelService, r4, false));
r4.createCompanion("Jan Madsenr");
r4.addExcursion(havOgHimmelExcusrions.get(0));
r4.addExcursion(havOgHimmelExcusrions.get(1));
Storage.addHotel(hvideSvane);
Storage.addHotel(pensionTusindfryd);
Storage.addHotel(hotelPhoenix);
Storage.addConference(havOgHimmel);
Storage.addRegistration(r1);
Storage.addRegistration(r2);
Storage.addRegistration(r3);
Storage.addRegistration(r4);
for (Excursion e : havOgHimmelExcusrions) {
Storage.addExcursion(e);
}
Storage.addPerson(jensen);
Storage.addPerson(sommer);
Storage.addPerson(madsen);
Storage.addPerson(petersen);
}
// ======================================================================================================
// Methods relating Persons
/**
* Create a person and add it to storage
*
* @param aName
* @param anAddress
* @param aCity
* @param aCountry
* @param aPhoneNumber
* @return reference of person created
*/
public static Person createPerson(String aName, String anAddress, String aCity, String aCountry,
String aPhoneNumber) {
Person p = new Person(aName, anAddress, aCity, aCountry, aPhoneNumber);
// System.out.println(Storage.getPersons().size());
Storage.addPerson(p);
// System.out.println(Storage.getPersons().size());
return p;
}
/**
* Updates person with an index
*
* @param personIndex
* @param name
* @param address
* @param city
* @param country
* @param phoneNr
*/
public static void updatePerson(int personIndex, String name, String address, String city, String country,
String phoneNr) {
Person p = Storage.getPersons().get(personIndex);
if (p != null) {
p.setName(name);
p.setAddress(address);
p.setCity(city);
p.setCountry(country);
p.setPhoneNumber(phoneNr);
}
}
/**
* Updates person with a reference
*
* @param personIndex
* @param name
* @param address
* @param city
* @param country
* @param phoneNr
*/
public static void updatePerson(Person p, String name, String address, String city, String country,
String phoneNr) {
if (p != null) {
p.setName(name);
p.setAddress(address);
p.setCity(city);
p.setCountry(country);
p.setPhoneNumber(phoneNr);
}
}
/**
* Removes a person from storage
*
* @param person
*/
public static void deletePerson(Person person) {
for (Registration r : person.getAllRegistrations()) {
deleteRegistration(r);
}
Storage.removePerson(person);
}
// ======================================================================================================
// Methods relating Registration
/**
*
* @param foredragsholder
* If true then the role is a 'speaker' else it is a
* 'participant'
* @return
*/
public static Role createRole(boolean foredragsholder) {
Role role;
if (foredragsholder) {
role = new Speaker();
} else {
role = new Participant();
}
return role;
}
public static Registration createRegistration(Person person, Conference conference, LocalDate arrivalDate,
LocalDate departureDate, Role role) {
// TODO When creating a new registration the participant role is set to
// null. fix it
Registration registration = new Registration(person, conference, arrivalDate, departureDate, role);
Storage.addRegistration(registration);
return registration;
}
public static void updateRegistration(Registration registration, LocalDate arrivalDate, LocalDate departureDate) {
// TODO When creating a new registration the participant role is set to
// null. fix it
registration.setArrivalDate(arrivalDate);
registration.setDepartureDate(departureDate);
}
public static void deleteRegistration() {
for (Registration r : Storage.getRegistration()) {
if (r.getPerson() == null) {
r.removeConference();
Storage.removeRegistration(r);
}
}
}
public static void deleteRegistration(Registration registration) {
// registration needs to be removed in person, conference, hotel
Hotel hotel = null;
HotelReservation hotelReservation = null;
Person person = null;
Conference conference = null;
if (registration != null) {
person = registration.getPerson();
person.removeRegistrations(registration);
if (person != null) {
hotel = registration.getHotelReservation().getHotel();
hotelReservation = registration.getHotelReservation();
conference = registration.getConference();
}
}
hotel.removeHotelReservation(hotelReservation);
conference.removeRegistration(registration);
storage.Storage.removeRegistration(registration);
}
public static void RegistrationAddExcursion(Registration registration, Excursion excursion) {
registration.addExcursion(excursion);
}
public static void RegistrationRemoveExcursion(Registration registration, Excursion excursion) {
registration.removeExcursion(excursion);
}
// ======================================================================================================
// Methods relating Hotels
public static Hotel createHotel(String name, String description, String stars, String telephoneNumber,
String address, Double singleRoomPrice, Double doubleRoomPrice) {
Hotel hotel = new Hotel(name, description, stars, telephoneNumber, address, singleRoomPrice, doubleRoomPrice);
Storage.addHotel(hotel);
return hotel;
}
public static void updateHotel(Hotel hotel, String description, String stars, String telephoneNumber,
String address, Double singleRoomPrice, Double doubleRoomPrice) {
hotel.setDescription(description);
hotel.setStars(stars);
hotel.setPhoneNumber(telephoneNumber);
hotel.setAddress(address);
hotel.setSingleRoomPrice(singleRoomPrice);
hotel.setDoubleRoomPrice(doubleRoomPrice);
}
public static boolean hotelIsRemovable(Hotel hotel) {
if (hotel.getReservations().size() == 0) {
return true;
}
return false;
}
public static void deleteHotel(Hotel hotel) {
if (hotelIsRemovable(hotel)) {
Storage.removeHotel(hotel);
}
}
// ======================================================================================================
// Methods relating conference
public static void createConference(String conferenceName, Double participationPrice, LocalDate startDate,
LocalDate endDate, ArrayList<Excursion> excursions, String description) {
Conference conference = new Conference(conferenceName, participationPrice, startDate, endDate, excursions,
description);
storage.Storage.addConference(conference);
}
public static void UpdateConference(Conference conference, String conferenceName, Double participationPrice,
LocalDate startDate, LocalDate endDate, ArrayList<Excursion> excursions, String description) {
// TODO
conference.setName(conferenceName);
conference.setParticipationPrice(participationPrice);
conference.setStartDate(startDate);
conference.setEndDate(endDate);
// conference.set
}
// ======================================================================================================
// Methods relating excursions
public static Excursion createExcursion(String description, double excursionPrice) {
Excursion excursion = new Excursion(description, excursionPrice);
Storage.addExcursion(excursion);
return excursion;
}
// TODO public static boolean excursion(Hotel hotel) {
//
// if (hotel.getReservations().size() == 0) {
// return true;
// }
// return false;
// }
public static void deleteExcursion(Excursion excursion) {
Storage.removeExcursion(excursion);
}
public static void updateExcursion(Excursion excursion, String description, double excursionPrice) {
excursion.setDescription(description);
excursion.setExcursionPrice(excursionPrice);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment