moreiramota

Untitled

Jan 9th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.model;
  7.  
  8. import java.sql.Timestamp;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.List;
  12. import java.util.Set;
  13. import lapr.project.data.JourneyDB;
  14. import lapr.project.utils.GraphHandler;
  15.  
  16. /**
  17. *
  18. * @author morei
  19. */
  20. public class Journey {
  21.  
  22. private static final double GRAVITATIONALCONSTANT = 9.8;
  23. private static final String NODBTEXT = "The dataBase connection was not yet set, either set or get a new one";
  24. private static final double AIRDENSITY = 1.2041; // at the temperature of 20ΒΊC
  25.  
  26. private static JourneyDB db;
  27.  
  28. private int idReservation;
  29. private User user;
  30. private Timestamp reservationDate;
  31. private Bicycle idBicycle;
  32. private int price;
  33. private Integer credits;
  34. private Date monthYear;
  35. private List<Route> routes;
  36.  
  37. /**
  38. *
  39. */
  40. public Journey() {
  41. //Mock Constructor
  42. }
  43.  
  44. /**
  45. *
  46. * @param idReservation
  47. * @param username
  48. * @param reservationDate
  49. * @param idBicycle
  50. * @param price
  51. * @param monthYear
  52. * @param routes
  53. */
  54. public Journey(int idReservation, User username, Timestamp reservationDate, Bicycle idBicycle, int price, Date monthYear, List<Route> routes) {
  55. this.idReservation = idReservation;
  56. this.user = username;
  57. this.reservationDate = reservationDate;
  58. this.idBicycle = idBicycle;
  59. this.price = price;
  60. this.monthYear = monthYear;
  61. this.routes = routes;
  62. this.credits = null;
  63. }
  64.  
  65. /**
  66. *
  67. * @param username
  68. * @param reservationDate
  69. * @param idBicycle
  70. * @param price
  71. * @param monthYear
  72. */
  73. public Journey(User username, Timestamp reservationDate, Bicycle idBicycle, int price, Date monthYear) {
  74. this.user = username;
  75. this.reservationDate = reservationDate;
  76. this.idBicycle = idBicycle;
  77. this.price = price;
  78. this.monthYear = monthYear;
  79. this.credits = null;
  80. routes = new ArrayList<>();
  81. }
  82.  
  83. /**
  84. *
  85. * @param username
  86. * @param reservationDate
  87. * @param idBicycle
  88. */
  89. public Journey(User username, Timestamp reservationDate, Bicycle idBicycle) {
  90. this.user = username;
  91. this.reservationDate = reservationDate;
  92. this.idBicycle = idBicycle;
  93. price = 0;
  94. this.credits = null;
  95. routes = new ArrayList<>();
  96. }
  97.  
  98. /**
  99. *
  100. * @param username
  101. * @param reservationDate
  102. * @param idBicycle
  103. * @param price
  104. */
  105. public Journey(User username, Timestamp reservationDate, Bicycle idBicycle, int price) {
  106. this.user = username;
  107. this.reservationDate = reservationDate;
  108. this.idBicycle = idBicycle;
  109. this.price = price;
  110. this.credits = null;
  111. routes = new ArrayList<>();
  112. }
  113.  
  114. /**
  115. * get the credits gained in the journey
  116. *
  117. * @return
  118. */
  119. public Integer getCredits() {
  120. return credits;
  121. }
  122.  
  123. public void setIdReservation(int idReservation) {
  124. this.idReservation = idReservation;
  125. }
  126.  
  127. /**
  128. *
  129. * @return
  130. */
  131. public int getIdReservation() {
  132. return idReservation;
  133. }
  134.  
  135. /**
  136. *
  137. * @return
  138. */
  139. public Timestamp getReservationDate() {
  140. return reservationDate;
  141. }
  142.  
  143. /**
  144. *
  145. * @return
  146. */
  147. public User getUser() {
  148. return user;
  149. }
  150.  
  151. /**
  152. *
  153. * @return
  154. */
  155. public Bicycle getBicycle() {
  156. return idBicycle;
  157. }
  158.  
  159. /**
  160. *
  161. * @return
  162. */
  163. public int getPrice() {
  164. return price;
  165. }
  166.  
  167. /**
  168. *
  169. * @return
  170. */
  171. public Date getMonthYear() {
  172. return monthYear;
  173. }
  174.  
  175. /**
  176. * get the routes in the journey
  177. *
  178. * @return
  179. */
  180. public List<Route> getRoutes() {
  181. return routes;
  182. }
  183.  
  184. /**
  185. * set the routes in the journey
  186. *
  187. * @param routes
  188. */
  189. public void setRoutes(List<Route> routes) {
  190. this.routes = routes;
  191. }
  192.  
  193. public void setCredits(Integer credits) {
  194. this.credits = credits;
  195. }
  196.  
  197. /**
  198. *
  199. * @param reservationDate
  200. */
  201. public void setReservationDate(Timestamp reservationDate) {
  202. this.reservationDate = reservationDate;
  203. }
  204.  
  205. /**
  206. *
  207. * @param user
  208. */
  209. public void setUserName(User user) {
  210. this.user = user;
  211. }
  212.  
  213. /**
  214. *
  215. * @param bicycle
  216. */
  217. public void setBicycle(Bicycle bicycle) {
  218. this.idBicycle = bicycle;
  219. }
  220.  
  221. /**
  222. *
  223. * @param price
  224. */
  225. public void setPrice(int price) {
  226. this.price = price;
  227. }
  228.  
  229. /**
  230. *
  231. * @param monthYear
  232. */
  233. public void setMonthYear(Date monthYear) {
  234. this.monthYear = monthYear;
  235. }
  236.  
  237. /**
  238. * set id bicycle for the instance
  239. *
  240. * @return
  241. */
  242. public Bicycle getIdBicycle() {
  243. return idBicycle;
  244. }
  245.  
  246. /**
  247. * set user for the instance
  248. *
  249. * @param user
  250. */
  251. public void setUser(User user) {
  252. this.user = user;
  253. }
  254.  
  255. /**
  256. * set id bicycle for the instance
  257. *
  258. * @param idBicycle
  259. */
  260. public void setIdBicycle(Bicycle idBicycle) {
  261. this.idBicycle = idBicycle;
  262. }
  263.  
  264. /**
  265. *
  266. * @return
  267. */
  268. @Override
  269. public String toString() {
  270. return "Reservation date " + reservationDate.toString() + " Username " + user.getUsername() + " Id bicycle " + idBicycle.getDescription() + " Price " + price + " Month Year " + monthYear.toString();
  271. }
  272.  
  273. /**
  274. *
  275. * @param obj
  276. * @return
  277. */
  278. @Override
  279. public boolean equals(Object obj) {
  280. if (obj == null) {
  281. return false;
  282. }
  283. if (this == obj) {
  284. return true;
  285. }
  286. if (!(obj instanceof Journey)) {
  287. return false;
  288. }
  289. final Journey other = (Journey) obj;
  290. return this.idReservation == other.idReservation;
  291. }
  292.  
  293. /**
  294. * hash code for the equals
  295. *
  296. * @return
  297. */
  298. @Override
  299. public int hashCode() {
  300. int hash = 5;
  301. hash = 37 * hash + this.idReservation;
  302. return hash;
  303. }
  304.  
  305. /**
  306. * add a route to a journey
  307. *
  308. * @param r
  309. */
  310. public void addRoute(Route r) {
  311. routes.add(r);
  312. }
  313.  
  314. /**
  315. * metod the gets all the energy and the calls the final method
  316. *
  317. * @param li
  318. * @param lf
  319. * @param user
  320. * @param velocity
  321. * @param b
  322. * @param graph
  323. * @return
  324. */
  325. public static double calculateEnergyBetweenTwoLocations(Location li, Location lf, User user, double velocity, Bicycle b, GraphHandler graph) {
  326.  
  327. double inicialHeight = (double) li.getAltitude();
  328. double finalHeight = (double) lf.getAltitude();
  329.  
  330. Point initialPoint = graph.getPoint(li);
  331. Point finalPoint = graph.getPoint(lf);
  332.  
  333. double windVelocity = graph.getWind(initialPoint, finalPoint);
  334.  
  335. double bodyArea = getBodyArea(user.getWeight(), b.getWeigth(), user.getHeight());
  336.  
  337. double distance = graph.getDistance(initialPoint, finalPoint);
  338. double heightDifference = finalHeight - inicialHeight;
  339.  
  340. // bearing angle, angle of the movement and the result angle of the difference between both
  341. double bearingAngle = Wind.calculatePathDirection(li, lf);
  342. double angle = getAngle(heightDifference, distance);
  343. double angleBetweenWindAndMovement = bearingAngle - angle;
  344.  
  345. double bearingForce = calculateBearingsForce(user.getWeight(), b.getWeigth(), graph.getCoefficient(initialPoint, finalPoint));
  346. double dragForce = calculateDragForce(windVelocity, bodyArea, velocity, angleBetweenWindAndMovement, b.getAerodynamic());
  347. double gravitationalForce = calculateGravitationForce(user.getWeight(), b.getWeigth(), heightDifference, angle);
  348. double airResistanceForce = calculateAirResistanceForce(windVelocity, bodyArea, velocity, angleBetweenWindAndMovement, b.getAerodynamic());
  349.  
  350. double totalEnergy = calculateTotalEnergy(bearingForce, dragForce, gravitationalForce, airResistanceForce, distance);
  351. return convertToKWH(totalEnergy);
  352. }
  353.  
  354. /**
  355. * calculate the air resistance for a user and determinate speed and wind
  356. * speed
  357. *
  358. * @param windVelocity
  359. * @param bodyArea
  360. * @param velocity
  361. * @param angleBetweenWindAndMovement
  362. * @param aeroDinamicCoefficient
  363. * @return
  364. */
  365. private static double calculateAirResistanceForce(double windVelocity, double bodyArea, double velocity, double angleBetweenWindAndMovement, double aeroDinamicCoefficient) {
  366. //Fra= K v^2-------- k= 1 / 2 * P * Cd * A
  367. return 0.5 * AIRDENSITY * bodyArea * aeroDinamicCoefficient * (Math.pow((velocity - windVelocity * Math.cos(angleBetweenWindAndMovement)), 2));
  368.  
  369. }
  370.  
  371. /**
  372. * get the body area for a user in a bike
  373. *
  374. * @param weightH
  375. * @param weightB
  376. * @param height
  377. * @return
  378. */
  379. private static double getBodyArea(double weightH, double weightB, double height) {
  380. return Math.sqrt(((weightB + weightH) * height / 3600));
  381. }
  382.  
  383. /**
  384. * with the height and the distance make get angle
  385. *
  386. * @param heightDifference
  387. * @param distance
  388. * @return
  389. */
  390. private static double getAngle(double heightDifference, double distance) {
  391. return (Math.asin(heightDifference / distance));
  392. }
  393.  
  394. /**
  395. * calculate the bearings force
  396. *
  397. * @param weightH
  398. * @param weightB
  399. * @param bearingCoeffiecient
  400. * @return
  401. */
  402. private static double calculateBearingsForce(double weightH, double weightB, double bearingCoeffiecient) {
  403. //Fr=mg*ft ----ft= coeficiente mecanico
  404. return ((weightB + weightH) * GRAVITATIONALCONSTANT * bearingCoeffiecient);
  405.  
  406. }
  407.  
  408. /**
  409. * calculate the drag force for a user in a path
  410. *
  411. * @param windVelocity
  412. * @param bodyArea
  413. * @param velocity
  414. * @param angleBetweenWindAndMovement
  415. * @param aeroDinamicCoefficient
  416. * @return
  417. */
  418. private static double calculateDragForce(double windVelocity, double bodyArea, double velocity, double angleBetweenWindAndMovement, double aeroDinamicCoefficient) {
  419. //Fdrag = 1 / 2 * P * v ^ 2 * Cd * A
  420. return (0.5 * AIRDENSITY * (Math.pow(velocity - windVelocity * Math.cos(angleBetweenWindAndMovement), 2)) * aeroDinamicCoefficient * bodyArea);
  421.  
  422. }
  423.  
  424. /**
  425. * metoth that calculate gravitation force for a user and a path
  426. *
  427. * @param weightH
  428. * @param weightB
  429. * @param heightDifference
  430. * @param angle
  431. * @return
  432. */
  433. private static double calculateGravitationForce(double weightH, double weightB, double heightDifference, double angle) {
  434. // FG = mg * deltaH * cos angle
  435. return (weightH + weightB) * GRAVITATIONALCONSTANT * heightDifference * Math.sin(angle);
  436. }
  437.  
  438. /**
  439. * the sum and multiplicatiion for all forces
  440. *
  441. * @param bearingForce
  442. * @param dragForce
  443. * @param gravitationalForce
  444. * @param airResistanceForce
  445. * @param distance
  446. * @return
  447. */
  448. private static double calculateTotalEnergy(double bearingForce, double dragForce, double gravitationalForce, double airResistanceForce, double distance) {
  449. //w = F * distacne------------------------------ - Wtotal = Wfa + Wfg + WFdrag
  450. return ((bearingForce * distance) + (dragForce * distance) + (gravitationalForce * distance) + (airResistanceForce * distance));
  451. }
  452.  
  453. /**
  454. * calculateEnergyDuringJourney
  455. *
  456. * @param journey
  457. * @param graph
  458. * @return
  459. */
  460. public static double calculateEnergyDuringJourney(Journey journey, GraphHandler graph) {
  461. double totalEnergy = 0.0;
  462.  
  463. List<Route> routes = journey.getRoutes();
  464.  
  465. for (int i = 0; i < routes.size() - 1; i++) {
  466. Route firstRoute = routes.get(i);
  467. Route secondRoute = routes.get(i + 1);
  468.  
  469. Point initialPoint = graph.getPoint(firstRoute.getLocation());
  470. Point finalPoint = graph.getPoint(secondRoute.getLocation());
  471.  
  472. secondRoute.setEnergy(calculateEnergyBetweenTwoLocations(firstRoute.getLocation(), secondRoute.getLocation(), journey.getUser(), journey.getUser().getAverageSpeed(), journey.getBicycle(), graph));
  473. totalEnergy += secondRoute.getEnergy();
  474.  
  475. graph.setEnergy(initialPoint, finalPoint, secondRoute.getEnergy());
  476. }
  477.  
  478. return convertToKWH(totalEnergy);
  479. }
  480.  
  481. /**
  482. * convert the energy to KWH
  483. *
  484. * @param totalEnergy
  485. * @return
  486. */
  487. private static double convertToKWH(double totalEnergy) {
  488. return totalEnergy * 2.77777778E-7;
  489.  
  490. }
  491.  
  492. /**
  493. * get a journey
  494. *
  495. * @param idReservation
  496. * @return
  497. */
  498. public static Journey getJourney(int idReservation) {
  499. if (db == null) {
  500. throw new NullPointerException(NODBTEXT);
  501. }
  502. return db.getJourney(idReservation);
  503. }
  504.  
  505. /**
  506. * get all journey for a user in a 30 days period
  507. *
  508. * @param username
  509. * @param reservationDate
  510. * @return
  511. */
  512. public static Set<Journey> getJourneysMonth(String username, Timestamp reservationDate) {
  513. if (db == null) {
  514. throw new NullPointerException(NODBTEXT);
  515. }
  516. return db.getJourneysMonth(username, reservationDate);
  517. }
  518.  
  519. /**
  520. * Tries to end the jouney by parking the bicycle in a slot.
  521. *
  522. * @return
  523. */
  524. public boolean endJourney() {
  525. try {
  526. Location local = routes.get(routes.size() - 1).getLocation();
  527. Park destination = Park.getPark(local.getLatitude(), local.getLongitude());
  528. if (destination.addBicycle(idBicycle) == null) {
  529. return false;
  530. }
  531. destination.save();
  532.  
  533. calculateMonthYear();
  534. calculatePrice();
  535. return true;
  536. } catch (Exception ex) {
  537. return false;
  538. }
  539. }
  540.  
  541. /**
  542. * calculate credits gained by a user in a journey
  543. */
  544. public void calculateCredits() {
  545. if (credits == null && db != null) {
  546. this.credits = db.getJourneyCredits(routes.get(routes.size() - 1).getLocation().getAltitude(), routes.get(0).getLocation());
  547. user.addCreditPoints(this.credits);
  548. }
  549. }
  550.  
  551. /**
  552. * calculate when was the journey
  553. */
  554. public void calculateMonthYear() {
  555. this.monthYear = routes.get(routes.size() - 1).getVisitDate();
  556. }
  557.  
  558. /**
  559. * calculate the price of the journey
  560. */
  561. public void calculatePrice() {
  562. int difference = getTimeDifference(routes.get(0).getVisitDate(), routes.get(routes.size() - 1).getVisitDate());
  563. this.price = difference * 3;
  564. }
  565.  
  566. /**
  567. * get the time direnfce for two timestamps
  568. *
  569. * @param visitDateI
  570. * @param visitDateF
  571. * @return
  572. */
  573. public static int getTimeDifference(Timestamp visitDateI, Timestamp visitDateF) {
  574. long difference = visitDateF.getTime() - visitDateI.getTime();
  575. long difHours = difference / (60 * 60 * 1000);
  576. return (int) difHours;
  577. }
  578.  
  579. /**
  580. * calculate the velocity from one point to another
  581. *
  582. * @param graph
  583. * @param initialPoint
  584. * @param finalPoint
  585. * @param initialTimestamp
  586. * @param finalTimestamp
  587. * @return
  588. */
  589. private static double calculateVelocity(GraphHandler graph, Point initialPoint, Point finalPoint, Timestamp initialTimestamp, Timestamp finalTimestamp) {
  590. double distance = graph.getDistance(initialPoint, finalPoint);
  591. int time = Journey.getTimeDifference(initialTimestamp, finalTimestamp) * 3600;
  592. return (distance / time);
  593. }
  594.  
  595. /**
  596. * calculate bunerd caluries for a journey
  597. *
  598. * @param journey
  599. * @param graph
  600. * @return
  601. */
  602. public static double calculateBurnedCalories(Journey journey, GraphHandler graph) {
  603. double kwh = 0;
  604.  
  605. List<Route> routes = journey.getRoutes();
  606. for (int i = 0; i < routes.size() - 1; i++) {
  607. Route firstRoute = routes.get(i);
  608. Route secondRoute = routes.get(i + 1);
  609.  
  610. Point initialPoint = graph.getPoint(firstRoute.getLocation());
  611. Point finalPoint = graph.getPoint(secondRoute.getLocation());
  612.  
  613. double velocity = calculateVelocity(graph, initialPoint, finalPoint, firstRoute.getVisitDate(), secondRoute.getVisitDate());
  614.  
  615. kwh = kwh + calculateEnergyBetweenTwoLocations(firstRoute.getLocation(), secondRoute.getLocation(), journey.getUser(), velocity, journey.getBicycle(), graph);
  616. }
  617.  
  618. if (journey.getBicycle().getType() == 1) {
  619. kwh = kwh * 0.4;
  620. }
  621. //calories
  622. return kwh * 860420.65;
  623. }
  624.  
  625. /**
  626. * make sure that the data base is not null
  627. */
  628. public static void getDB() {
  629. db = new JourneyDB();
  630. }
  631.  
  632. /**
  633. * get the current data base
  634. *
  635. * @return
  636. */
  637. public static JourneyDB getCurrentDB() {
  638. return db;
  639. }
  640.  
  641. /**
  642. * set data base
  643. *
  644. * @param jdb
  645. */
  646. public static void setDB(JourneyDB jdb) {
  647. db = jdb;
  648. }
  649.  
  650. /**
  651. * remove journey by the idReservation
  652. *
  653. * @param idReservation
  654. */
  655. public static void removeJourney(int idReservation) {
  656. if (db == null) {
  657. throw new NullPointerException(NODBTEXT);
  658. }
  659. db.removeJourney(idReservation);
  660.  
  661. }
  662.  
  663. /**
  664. * save instance in the data base
  665. */
  666. public void save() {
  667. if (db == null) {
  668. throw new NullPointerException(NODBTEXT);
  669. }
  670. try {
  671. db.updateJourney(this);
  672. } catch (IllegalArgumentException ex) {
  673. db.addJourney(this);
  674. }
  675. for (Route r : routes) {
  676. try {
  677. r.updateRoute();
  678. } catch (IllegalArgumentException ex) {
  679. r.save();
  680. }
  681. }
  682. }
  683.  
  684. /**
  685. * sum all values of the user invoices
  686. *
  687. * @param journeys
  688. * @return
  689. */
  690. public static double getTotalMonth(Set<Journey> journeys) {
  691. double totalMonth = 0;
  692. for (Journey j : journeys) {
  693. totalMonth += j.getPrice();
  694. }
  695. return totalMonth;
  696. }
  697. }
Add Comment
Please, Sign In to add comment