Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package lapr.project.model;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Set;
- import lapr.project.data.JourneyDB;
- import lapr.project.utils.GraphHandler;
- /**
- *
- * @author morei
- */
- public class Journey {
- private static final double GRAVITATIONALCONSTANT = 9.8;
- private static final String NODBTEXT = "The dataBase connection was not yet set, either set or get a new one";
- private static final double AIRDENSITY = 1.2041; // at the temperature of 20ΒΊC
- private static JourneyDB db;
- private int idReservation;
- private User user;
- private Timestamp reservationDate;
- private Bicycle idBicycle;
- private int price;
- private Integer credits;
- private Date monthYear;
- private List<Route> routes;
- /**
- *
- */
- public Journey() {
- //Mock Constructor
- }
- /**
- *
- * @param idReservation
- * @param username
- * @param reservationDate
- * @param idBicycle
- * @param price
- * @param monthYear
- * @param routes
- */
- public Journey(int idReservation, User username, Timestamp reservationDate, Bicycle idBicycle, int price, Date monthYear, List<Route> routes) {
- this.idReservation = idReservation;
- this.user = username;
- this.reservationDate = reservationDate;
- this.idBicycle = idBicycle;
- this.price = price;
- this.monthYear = monthYear;
- this.routes = routes;
- this.credits = null;
- }
- /**
- *
- * @param username
- * @param reservationDate
- * @param idBicycle
- * @param price
- * @param monthYear
- */
- public Journey(User username, Timestamp reservationDate, Bicycle idBicycle, int price, Date monthYear) {
- this.user = username;
- this.reservationDate = reservationDate;
- this.idBicycle = idBicycle;
- this.price = price;
- this.monthYear = monthYear;
- this.credits = null;
- routes = new ArrayList<>();
- }
- /**
- *
- * @param username
- * @param reservationDate
- * @param idBicycle
- */
- public Journey(User username, Timestamp reservationDate, Bicycle idBicycle) {
- this.user = username;
- this.reservationDate = reservationDate;
- this.idBicycle = idBicycle;
- price = 0;
- this.credits = null;
- routes = new ArrayList<>();
- }
- /**
- *
- * @param username
- * @param reservationDate
- * @param idBicycle
- * @param price
- */
- public Journey(User username, Timestamp reservationDate, Bicycle idBicycle, int price) {
- this.user = username;
- this.reservationDate = reservationDate;
- this.idBicycle = idBicycle;
- this.price = price;
- this.credits = null;
- routes = new ArrayList<>();
- }
- /**
- * get the credits gained in the journey
- *
- * @return
- */
- public Integer getCredits() {
- return credits;
- }
- public void setIdReservation(int idReservation) {
- this.idReservation = idReservation;
- }
- /**
- *
- * @return
- */
- public int getIdReservation() {
- return idReservation;
- }
- /**
- *
- * @return
- */
- public Timestamp getReservationDate() {
- return reservationDate;
- }
- /**
- *
- * @return
- */
- public User getUser() {
- return user;
- }
- /**
- *
- * @return
- */
- public Bicycle getBicycle() {
- return idBicycle;
- }
- /**
- *
- * @return
- */
- public int getPrice() {
- return price;
- }
- /**
- *
- * @return
- */
- public Date getMonthYear() {
- return monthYear;
- }
- /**
- * get the routes in the journey
- *
- * @return
- */
- public List<Route> getRoutes() {
- return routes;
- }
- /**
- * set the routes in the journey
- *
- * @param routes
- */
- public void setRoutes(List<Route> routes) {
- this.routes = routes;
- }
- public void setCredits(Integer credits) {
- this.credits = credits;
- }
- /**
- *
- * @param reservationDate
- */
- public void setReservationDate(Timestamp reservationDate) {
- this.reservationDate = reservationDate;
- }
- /**
- *
- * @param user
- */
- public void setUserName(User user) {
- this.user = user;
- }
- /**
- *
- * @param bicycle
- */
- public void setBicycle(Bicycle bicycle) {
- this.idBicycle = bicycle;
- }
- /**
- *
- * @param price
- */
- public void setPrice(int price) {
- this.price = price;
- }
- /**
- *
- * @param monthYear
- */
- public void setMonthYear(Date monthYear) {
- this.monthYear = monthYear;
- }
- /**
- * set id bicycle for the instance
- *
- * @return
- */
- public Bicycle getIdBicycle() {
- return idBicycle;
- }
- /**
- * set user for the instance
- *
- * @param user
- */
- public void setUser(User user) {
- this.user = user;
- }
- /**
- * set id bicycle for the instance
- *
- * @param idBicycle
- */
- public void setIdBicycle(Bicycle idBicycle) {
- this.idBicycle = idBicycle;
- }
- /**
- *
- * @return
- */
- @Override
- public String toString() {
- return "Reservation date " + reservationDate.toString() + " Username " + user.getUsername() + " Id bicycle " + idBicycle.getDescription() + " Price " + price + " Month Year " + monthYear.toString();
- }
- /**
- *
- * @param obj
- * @return
- */
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof Journey)) {
- return false;
- }
- final Journey other = (Journey) obj;
- return this.idReservation == other.idReservation;
- }
- /**
- * hash code for the equals
- *
- * @return
- */
- @Override
- public int hashCode() {
- int hash = 5;
- hash = 37 * hash + this.idReservation;
- return hash;
- }
- /**
- * add a route to a journey
- *
- * @param r
- */
- public void addRoute(Route r) {
- routes.add(r);
- }
- /**
- * metod the gets all the energy and the calls the final method
- *
- * @param li
- * @param lf
- * @param user
- * @param velocity
- * @param b
- * @param graph
- * @return
- */
- public static double calculateEnergyBetweenTwoLocations(Location li, Location lf, User user, double velocity, Bicycle b, GraphHandler graph) {
- double inicialHeight = (double) li.getAltitude();
- double finalHeight = (double) lf.getAltitude();
- Point initialPoint = graph.getPoint(li);
- Point finalPoint = graph.getPoint(lf);
- double windVelocity = graph.getWind(initialPoint, finalPoint);
- double bodyArea = getBodyArea(user.getWeight(), b.getWeigth(), user.getHeight());
- double distance = graph.getDistance(initialPoint, finalPoint);
- double heightDifference = finalHeight - inicialHeight;
- // bearing angle, angle of the movement and the result angle of the difference between both
- double bearingAngle = Wind.calculatePathDirection(li, lf);
- double angle = getAngle(heightDifference, distance);
- double angleBetweenWindAndMovement = bearingAngle - angle;
- double bearingForce = calculateBearingsForce(user.getWeight(), b.getWeigth(), graph.getCoefficient(initialPoint, finalPoint));
- double dragForce = calculateDragForce(windVelocity, bodyArea, velocity, angleBetweenWindAndMovement, b.getAerodynamic());
- double gravitationalForce = calculateGravitationForce(user.getWeight(), b.getWeigth(), heightDifference, angle);
- double airResistanceForce = calculateAirResistanceForce(windVelocity, bodyArea, velocity, angleBetweenWindAndMovement, b.getAerodynamic());
- double totalEnergy = calculateTotalEnergy(bearingForce, dragForce, gravitationalForce, airResistanceForce, distance);
- return convertToKWH(totalEnergy);
- }
- /**
- * calculate the air resistance for a user and determinate speed and wind
- * speed
- *
- * @param windVelocity
- * @param bodyArea
- * @param velocity
- * @param angleBetweenWindAndMovement
- * @param aeroDinamicCoefficient
- * @return
- */
- private static double calculateAirResistanceForce(double windVelocity, double bodyArea, double velocity, double angleBetweenWindAndMovement, double aeroDinamicCoefficient) {
- //Fra= K v^2-------- k= 1 / 2 * P * Cd * A
- return 0.5 * AIRDENSITY * bodyArea * aeroDinamicCoefficient * (Math.pow((velocity - windVelocity * Math.cos(angleBetweenWindAndMovement)), 2));
- }
- /**
- * get the body area for a user in a bike
- *
- * @param weightH
- * @param weightB
- * @param height
- * @return
- */
- private static double getBodyArea(double weightH, double weightB, double height) {
- return Math.sqrt(((weightB + weightH) * height / 3600));
- }
- /**
- * with the height and the distance make get angle
- *
- * @param heightDifference
- * @param distance
- * @return
- */
- private static double getAngle(double heightDifference, double distance) {
- return (Math.asin(heightDifference / distance));
- }
- /**
- * calculate the bearings force
- *
- * @param weightH
- * @param weightB
- * @param bearingCoeffiecient
- * @return
- */
- private static double calculateBearingsForce(double weightH, double weightB, double bearingCoeffiecient) {
- //Fr=mg*ft ----ft= coeficiente mecanico
- return ((weightB + weightH) * GRAVITATIONALCONSTANT * bearingCoeffiecient);
- }
- /**
- * calculate the drag force for a user in a path
- *
- * @param windVelocity
- * @param bodyArea
- * @param velocity
- * @param angleBetweenWindAndMovement
- * @param aeroDinamicCoefficient
- * @return
- */
- private static double calculateDragForce(double windVelocity, double bodyArea, double velocity, double angleBetweenWindAndMovement, double aeroDinamicCoefficient) {
- //Fdrag = 1 / 2 * P * v ^ 2 * Cd * A
- return (0.5 * AIRDENSITY * (Math.pow(velocity - windVelocity * Math.cos(angleBetweenWindAndMovement), 2)) * aeroDinamicCoefficient * bodyArea);
- }
- /**
- * metoth that calculate gravitation force for a user and a path
- *
- * @param weightH
- * @param weightB
- * @param heightDifference
- * @param angle
- * @return
- */
- private static double calculateGravitationForce(double weightH, double weightB, double heightDifference, double angle) {
- // FG = mg * deltaH * cos angle
- return (weightH + weightB) * GRAVITATIONALCONSTANT * heightDifference * Math.sin(angle);
- }
- /**
- * the sum and multiplicatiion for all forces
- *
- * @param bearingForce
- * @param dragForce
- * @param gravitationalForce
- * @param airResistanceForce
- * @param distance
- * @return
- */
- private static double calculateTotalEnergy(double bearingForce, double dragForce, double gravitationalForce, double airResistanceForce, double distance) {
- //w = F * distacne------------------------------ - Wtotal = Wfa + Wfg + WFdrag
- return ((bearingForce * distance) + (dragForce * distance) + (gravitationalForce * distance) + (airResistanceForce * distance));
- }
- /**
- * calculateEnergyDuringJourney
- *
- * @param journey
- * @param graph
- * @return
- */
- public static double calculateEnergyDuringJourney(Journey journey, GraphHandler graph) {
- double totalEnergy = 0.0;
- List<Route> routes = journey.getRoutes();
- for (int i = 0; i < routes.size() - 1; i++) {
- Route firstRoute = routes.get(i);
- Route secondRoute = routes.get(i + 1);
- Point initialPoint = graph.getPoint(firstRoute.getLocation());
- Point finalPoint = graph.getPoint(secondRoute.getLocation());
- secondRoute.setEnergy(calculateEnergyBetweenTwoLocations(firstRoute.getLocation(), secondRoute.getLocation(), journey.getUser(), journey.getUser().getAverageSpeed(), journey.getBicycle(), graph));
- totalEnergy += secondRoute.getEnergy();
- graph.setEnergy(initialPoint, finalPoint, secondRoute.getEnergy());
- }
- return convertToKWH(totalEnergy);
- }
- /**
- * convert the energy to KWH
- *
- * @param totalEnergy
- * @return
- */
- private static double convertToKWH(double totalEnergy) {
- return totalEnergy * 2.77777778E-7;
- }
- /**
- * get a journey
- *
- * @param idReservation
- * @return
- */
- public static Journey getJourney(int idReservation) {
- if (db == null) {
- throw new NullPointerException(NODBTEXT);
- }
- return db.getJourney(idReservation);
- }
- /**
- * get all journey for a user in a 30 days period
- *
- * @param username
- * @param reservationDate
- * @return
- */
- public static Set<Journey> getJourneysMonth(String username, Timestamp reservationDate) {
- if (db == null) {
- throw new NullPointerException(NODBTEXT);
- }
- return db.getJourneysMonth(username, reservationDate);
- }
- /**
- * Tries to end the jouney by parking the bicycle in a slot.
- *
- * @return
- */
- public boolean endJourney() {
- try {
- Location local = routes.get(routes.size() - 1).getLocation();
- Park destination = Park.getPark(local.getLatitude(), local.getLongitude());
- if (destination.addBicycle(idBicycle) == null) {
- return false;
- }
- destination.save();
- calculateMonthYear();
- calculatePrice();
- return true;
- } catch (Exception ex) {
- return false;
- }
- }
- /**
- * calculate credits gained by a user in a journey
- */
- public void calculateCredits() {
- if (credits == null && db != null) {
- this.credits = db.getJourneyCredits(routes.get(routes.size() - 1).getLocation().getAltitude(), routes.get(0).getLocation());
- user.addCreditPoints(this.credits);
- }
- }
- /**
- * calculate when was the journey
- */
- public void calculateMonthYear() {
- this.monthYear = routes.get(routes.size() - 1).getVisitDate();
- }
- /**
- * calculate the price of the journey
- */
- public void calculatePrice() {
- int difference = getTimeDifference(routes.get(0).getVisitDate(), routes.get(routes.size() - 1).getVisitDate());
- this.price = difference * 3;
- }
- /**
- * get the time direnfce for two timestamps
- *
- * @param visitDateI
- * @param visitDateF
- * @return
- */
- public static int getTimeDifference(Timestamp visitDateI, Timestamp visitDateF) {
- long difference = visitDateF.getTime() - visitDateI.getTime();
- long difHours = difference / (60 * 60 * 1000);
- return (int) difHours;
- }
- /**
- * calculate the velocity from one point to another
- *
- * @param graph
- * @param initialPoint
- * @param finalPoint
- * @param initialTimestamp
- * @param finalTimestamp
- * @return
- */
- private static double calculateVelocity(GraphHandler graph, Point initialPoint, Point finalPoint, Timestamp initialTimestamp, Timestamp finalTimestamp) {
- double distance = graph.getDistance(initialPoint, finalPoint);
- int time = Journey.getTimeDifference(initialTimestamp, finalTimestamp) * 3600;
- return (distance / time);
- }
- /**
- * calculate bunerd caluries for a journey
- *
- * @param journey
- * @param graph
- * @return
- */
- public static double calculateBurnedCalories(Journey journey, GraphHandler graph) {
- double kwh = 0;
- List<Route> routes = journey.getRoutes();
- for (int i = 0; i < routes.size() - 1; i++) {
- Route firstRoute = routes.get(i);
- Route secondRoute = routes.get(i + 1);
- Point initialPoint = graph.getPoint(firstRoute.getLocation());
- Point finalPoint = graph.getPoint(secondRoute.getLocation());
- double velocity = calculateVelocity(graph, initialPoint, finalPoint, firstRoute.getVisitDate(), secondRoute.getVisitDate());
- kwh = kwh + calculateEnergyBetweenTwoLocations(firstRoute.getLocation(), secondRoute.getLocation(), journey.getUser(), velocity, journey.getBicycle(), graph);
- }
- if (journey.getBicycle().getType() == 1) {
- kwh = kwh * 0.4;
- }
- //calories
- return kwh * 860420.65;
- }
- /**
- * make sure that the data base is not null
- */
- public static void getDB() {
- db = new JourneyDB();
- }
- /**
- * get the current data base
- *
- * @return
- */
- public static JourneyDB getCurrentDB() {
- return db;
- }
- /**
- * set data base
- *
- * @param jdb
- */
- public static void setDB(JourneyDB jdb) {
- db = jdb;
- }
- /**
- * remove journey by the idReservation
- *
- * @param idReservation
- */
- public static void removeJourney(int idReservation) {
- if (db == null) {
- throw new NullPointerException(NODBTEXT);
- }
- db.removeJourney(idReservation);
- }
- /**
- * save instance in the data base
- */
- public void save() {
- if (db == null) {
- throw new NullPointerException(NODBTEXT);
- }
- try {
- db.updateJourney(this);
- } catch (IllegalArgumentException ex) {
- db.addJourney(this);
- }
- for (Route r : routes) {
- try {
- r.updateRoute();
- } catch (IllegalArgumentException ex) {
- r.save();
- }
- }
- }
- /**
- * sum all values of the user invoices
- *
- * @param journeys
- * @return
- */
- public static double getTotalMonth(Set<Journey> journeys) {
- double totalMonth = 0;
- for (Journey j : journeys) {
- totalMonth += j.getPrice();
- }
- return totalMonth;
- }
- }
Add Comment
Please, Sign In to add comment