Advertisement
Guest User

Geo

a guest
Sep 20th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.49 KB | None | 0 0
  1. package project1;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. /*********************************************************************
  7. * This class is used as a count down timer for dates that are very far in the future.
  8. *
  9. * @author Jake Kucinski and Nick Grim
  10. * @class CIS 163-03
  11. * @date 9/20/2019
  12. * @professor Dr. Roger Ferguson
  13. ********************************************************************/
  14.  
  15. public class GeoCountDownTimer {
  16.  
  17. /** This is the number of months for a given date */
  18. private int month;
  19.  
  20. /** this is the number of years for a given date */
  21. private int year;
  22.  
  23. /** this is the number of days for a given date */
  24. private int day;
  25.  
  26. /** this is the minimum year this program will allow */
  27. private static final int MINYEAR = 2019;
  28.  
  29. /** Days in each month (assuming months are numbered beginning with 1) */
  30. private static final int[] DAYS_IN_MONTH = {0, 31, 28, 31, 30, 31, 30, 31,
  31. 31, 30, 31, 30, 31};
  32.  
  33. /** The months of the year in Strings */
  34. private static final String[] MONTHS = {"", "January", "February",
  35. "March", "April", "May", "June", "July", "August", "September",
  36. "October", "November", "December"};
  37.  
  38. /****************************************************************
  39. * Default contructor that creates a GeoCountDownTimer with a
  40. * zero at every value.
  41. ****************************************************************/
  42. public GeoCountDownTimer() {
  43.  
  44. this.month = 0;
  45. this.day = 0;
  46. this.year = 0;
  47.  
  48. }
  49.  
  50. /****************************************************************
  51. * Constructor that creates a GeoCountDownTimer
  52. * @param month - given months in a date
  53. * @param day - given days in a date
  54. * @param year - given year in a date
  55. ****************************************************************/
  56. public GeoCountDownTimer(int month, int day, int year) {
  57.  
  58. if (isValidDate(month, day, year)) {
  59.  
  60. this.month = month;
  61. this.day = day;
  62. this.year = year;
  63.  
  64. }
  65.  
  66. else {
  67. throw new IllegalArgumentException();
  68. }
  69. }
  70.  
  71. /****************************************************************
  72. * Constructor that creates a GeoCountDownTimer
  73. * @param other - another GeoCountDownTimer object that "this"
  74. * will be assigned to
  75. ****************************************************************/
  76. public void GeoCountDownTimer(GeoCountDownTimer other) {
  77.  
  78. this.month = other.month;
  79. this.day = other.day;
  80. this.year = other.year;
  81.  
  82. }
  83.  
  84. /****************************************************************
  85. * Constructor that creates a GeoCountDownTimer
  86. * @param geoDate - accepts a String of a date
  87. ****************************************************************/
  88. public GeoCountDownTimer(String geoDate) {
  89.  
  90. String s[] = geoDate.split("/");
  91.  
  92. int m, d, y;
  93.  
  94. if(s.length == 3) {
  95.  
  96. m = Integer.parseInt(s[0]);
  97. d = Integer.parseInt(s[1]);
  98. y = Integer.parseInt(s[2]);
  99.  
  100. }
  101.  
  102. else{
  103. throw new IllegalArgumentException();
  104. }
  105.  
  106. if (isValidDate(m, d, y)) {
  107.  
  108. this.month = m;
  109. this.day = d;
  110. this.year = y;
  111.  
  112. }
  113.  
  114. else {
  115. throw new IllegalArgumentException();
  116. }
  117.  
  118. }
  119.  
  120. /****************************************************************
  121. * Method to see if two dates are the same dates
  122. * @param other - date object passed to see if it equals "this"
  123. * @return true or false - depending on whether or not they are
  124. * the same dates
  125. ****************************************************************/
  126. public boolean equals(Object other) {
  127.  
  128. if(!(other instanceof GeoCountDownTimer)){
  129.  
  130. throw new IllegalArgumentException();
  131.  
  132. }
  133. GeoCountDownTimer temp = (GeoCountDownTimer)other;
  134.  
  135. return (this.month == temp.month) && (this.day == temp.day) && (this.year == temp.year);
  136.  
  137. }
  138.  
  139. /****************************************************************
  140. * Method that compares two given dates
  141. * @param other - GeoCountDownTimer passed to compare to "this"
  142. * @return 1 - other date came before "this" date,
  143. * -1 - "this" date came before other date,
  144. * 0 - "this" date and other date are the same
  145. ****************************************************************/
  146. public int compareTo(GeoCountDownTimer other) {
  147.  
  148. int temp = 0;
  149.  
  150. if(this.year > other.year) {
  151.  
  152. temp = 1;
  153.  
  154. }
  155. else if (this.year < other.year) {
  156.  
  157. temp = -1;
  158.  
  159. }
  160. else if (this.year == other.year) {
  161.  
  162. if(this.month > other.month) {
  163.  
  164. temp = 1;
  165.  
  166. }
  167. else if (this.month < other.month) {
  168.  
  169. temp = -1;
  170.  
  171. }
  172. else if (this.month == other.month) {
  173.  
  174. if (this.day > other.day) {
  175.  
  176. temp = 1;
  177.  
  178. }
  179. else if (this.day < other.day) {
  180.  
  181. temp = -1;
  182.  
  183. }
  184. else if (this.day == other.day) {
  185.  
  186. temp = 0;
  187.  
  188. }
  189. }
  190. }
  191.  
  192. return temp;
  193.  
  194. }
  195.  
  196. /****************************************************************
  197. * Method used to decrement days from "this" date, calls dec()
  198. * to decrement
  199. * @param days - how many days we want to decrement from "this"
  200. ****************************************************************/
  201. public void dec(int days) {
  202.  
  203. GeoCountDownTimer current = this;
  204.  
  205. int i = days;
  206.  
  207. while(i > 0) {
  208.  
  209. this.dec();
  210. i--;
  211.  
  212. }
  213. }
  214.  
  215. /****************************************************************
  216. * Method used to decrement "this" date by a single day
  217. ****************************************************************/
  218. public void dec() {
  219.  
  220. GeoCountDownTimer current = this;
  221.  
  222. if(current.day == 1) {
  223.  
  224. if(current.month == 1) {
  225.  
  226. if(current.year == 2019){
  227.  
  228. current.month = 1;
  229. current.day = 1;
  230. current.year = 2019;
  231.  
  232. }
  233.  
  234. current.year = current.year - 1;
  235. current.month = 12;
  236. current.day = DAYS_IN_MONTH[current.month];
  237.  
  238. }
  239.  
  240. else {
  241.  
  242. current.month = current.month - 1;
  243.  
  244. if(current.month == 2 && isLeapYear(year)) {
  245.  
  246. current.day = DAYS_IN_MONTH[current.month] + 1;
  247.  
  248. }
  249.  
  250. else {
  251.  
  252. current.day = DAYS_IN_MONTH[current.month];
  253.  
  254. }
  255. }
  256. }
  257.  
  258. else {
  259.  
  260. current.day = current.day - 1;
  261.  
  262. }
  263. }
  264.  
  265. /****************************************************************
  266. * Method used to increment days from "this" date, calls inc()
  267. * to increment
  268. * @param days - how many days we want to increment from "this"
  269. ****************************************************************/
  270. public void inc(int days) {
  271.  
  272. GeoCountDownTimer current = this;
  273.  
  274. int i = days;
  275.  
  276. while(i > 0) {
  277.  
  278. this.inc();
  279. i--;
  280.  
  281. }
  282. }
  283.  
  284. /****************************************************************
  285. * Method used to increment "this" date by a single day
  286. ****************************************************************/
  287. public void inc() {
  288.  
  289. GeoCountDownTimer current = this;
  290.  
  291. if((current.month == 2) && (isLeapYear(current.year)) && (current.day == DAYS_IN_MONTH[current.month])) {
  292.  
  293. current.day = current.day + 1;
  294.  
  295. }
  296.  
  297. else if((current.month == 2) && (isLeapYear(current.year)) && (current.day == DAYS_IN_MONTH[current.month] + 1)) {
  298.  
  299. current.month = 3;
  300. current.day = 1;
  301.  
  302. }
  303.  
  304. else if(current.day == DAYS_IN_MONTH[current.month]) {
  305.  
  306. if(current.month == 12) {
  307.  
  308. current.month = 1;
  309. current.day = 1;
  310. current.year = current.year + 1;
  311.  
  312. }
  313.  
  314. else {
  315.  
  316. current.month = current.month + 1;
  317. current.day = 1;
  318.  
  319. }
  320. }
  321.  
  322. else {
  323.  
  324. current.day = current.day + 1;
  325.  
  326. }
  327. }
  328.  
  329. /****************************************************************
  330. * Method that converts "this" date into a string
  331. * @return s - string of "this" date
  332. ****************************************************************/
  333. public String toString() {
  334.  
  335. String s = MONTHS[this.month] + " " + this.day + ", " + this.year;
  336.  
  337. return s;
  338.  
  339. }
  340.  
  341. /****************************************************************
  342. * Method that converts "this" date into a string using numerals
  343. * @return s - string of "this" date
  344. ****************************************************************/
  345. public String toDateString() {
  346.  
  347. String s = this.month + "/" + this.day + "/" + this.year;
  348.  
  349. return s;
  350.  
  351. }
  352.  
  353. /****************************************************************
  354. * Method that checks to see if the input date is a valid date
  355. * @param month - months in a given date
  356. * @param day - days in a given date
  357. * @param year - year in a given date
  358. * @return true or false - depending on whether the date is valid
  359. ****************************************************************/
  360. private static boolean isValidDate(int month, int day, int year) {
  361.  
  362. int t = 0;
  363.  
  364. if(isLeapYear(year)) {
  365.  
  366. t = 1;
  367.  
  368. }
  369.  
  370. if((t == 1) && (month == 2)) {
  371.  
  372. if ((month > 0 && month <= 12) && (day <= DAYS_IN_MONTH[month] + 1 && day > 0) && (year >= MINYEAR)) {
  373.  
  374. return true;
  375.  
  376. }
  377.  
  378. else {
  379.  
  380. return false;
  381.  
  382. }
  383. }
  384.  
  385. if ((month > 0 && month <= 12) && (day <= DAYS_IN_MONTH[month] && day > 0) && (year >= MINYEAR)) {
  386.  
  387. return true;
  388.  
  389. }
  390.  
  391. else {
  392.  
  393. return false;
  394.  
  395. }
  396. }
  397.  
  398. /****************************************************************
  399. * Method to check is a given year is a leap year or not
  400. * @param year - the year being checked to see if it is a leap
  401. * year
  402. * @return true of false - whether the input year is a leap year
  403. ****************************************************************/
  404. public static boolean isLeapYear(int year) {
  405.  
  406. return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  407.  
  408. }
  409.  
  410. /****************************************************************
  411. * Method to save a given GeoCountDownTimer object to a .txt file
  412. * @param filename - the user input filename where the
  413. * GeoCountDownTimer object is stored
  414. ****************************************************************/
  415. public void save(String filename) {
  416.  
  417. PrintWriter out = null;
  418.  
  419. try {
  420.  
  421. out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
  422.  
  423. } catch (IOException e) {
  424.  
  425. e.printStackTrace();
  426.  
  427. }
  428.  
  429. out.println (month);
  430. out.println (day);
  431. out.println (year);
  432.  
  433. out.close();
  434.  
  435. }
  436.  
  437. /****************************************************************
  438. * Method to load a given GeoCountDownTimer object from a .txt
  439. * file
  440. * @param fileName - the user input filename where the
  441. * GeoCountDownTimer object is taken from
  442. ****************************************************************/
  443. public void load(String fileName) throws FileNotFoundException {
  444.  
  445. try {
  446.  
  447. Scanner fileReader = new Scanner(new File(fileName));
  448. String geoDate = fileReader.next();
  449.  
  450. } catch(Exception error) {
  451.  
  452. System.out.println("File not found");
  453.  
  454. }
  455. }
  456.  
  457. /****************************************************************
  458. * Method to see how many days are between "this" date and the
  459. * input fromDate
  460. * @param fromDate - the date to which we are seeing how many
  461. * days away it is
  462. * @return i - how many days are between "this" and fromDate
  463. ****************************************************************/
  464. public int daysToGo(String fromDate) {
  465.  
  466. GeoCountDownTimer temp = new GeoCountDownTimer(fromDate);
  467.  
  468. if(this.compareTo(temp) == -1) {
  469.  
  470. throw new IllegalArgumentException();
  471.  
  472. }
  473.  
  474. int i = 0;
  475.  
  476. while(this.compareTo(temp) != 0) {
  477.  
  478. temp.inc();
  479. i++;
  480.  
  481. }
  482.  
  483. return i;
  484.  
  485. }
  486.  
  487. /****************************************************************
  488. * Method to return a GeoCountDownTimer object of a given date
  489. * so many days in the future, or past
  490. * @param n - the number of days we are looking for from now,
  491. * if this number is a negative then we are counting
  492. * so many days into the past
  493. * @return current - returns the GeoCountDownTimer object
  494. ****************************************************************/
  495. public GeoCountDownTimer daysInFuture(int n) {
  496.  
  497. GeoCountDownTimer current = new GeoCountDownTimer(this.month, this.day, this.year);
  498.  
  499. if(n > 0) {
  500.  
  501. current.inc(n);
  502. return current;
  503.  
  504. }
  505.  
  506. else if(n < 0) {
  507.  
  508. current.dec(n);
  509. return current;
  510.  
  511. }
  512.  
  513. return current;
  514.  
  515. }
  516.  
  517. /****************************************************************
  518. * Main method used for testing the various methods and functions
  519. * written throughout this class
  520. ****************************************************************/
  521. public static void main(String[] args) {
  522.  
  523. GeoCountDownTimer s = new GeoCountDownTimer("2/10/2020");
  524. System.out.println("Date: " + s);
  525.  
  526. GeoCountDownTimer s1 = new GeoCountDownTimer("2/10/2022");
  527. System.out.println("Date: " + s1.toDateString());
  528.  
  529. s1.dec(365);
  530. System.out.println("Date: " + s1);
  531.  
  532. s1.inc(365);
  533. System.out.println("Date: " + s1);
  534.  
  535. GeoCountDownTimer s2 = new GeoCountDownTimer("2/10/2019");
  536. for (int i = 0; i < (365 + 366 ); i++)
  537. s2.inc(1);
  538. System.out.println("Date: " + s2);
  539.  
  540. int d = 2;
  541. int m = 12;
  542. int y = 2020;
  543. GeoCountDownTimer s3 = new GeoCountDownTimer(d, m ,y);
  544.  
  545. GeoCountDownTimer s4 = new GeoCountDownTimer("8/20/2028");
  546. s4.GeoCountDownTimer(s3);
  547.  
  548. GeoCountDownTimer s5 = new GeoCountDownTimer("5/20/2028");
  549. System.out.println(s5);
  550.  
  551. int output = s5.daysToGo("3/21/2028");
  552. System.out.println("Output: " + output);
  553.  
  554. GeoCountDownTimer s6 = new GeoCountDownTimer("5/20/20282");
  555. try {
  556.  
  557. GeoCountDownTimer s7 = new GeoCountDownTimer("5/201/2028");
  558.  
  559. } catch (Exception e) {
  560.  
  561. e.printStackTrace();
  562.  
  563. }
  564. try {
  565.  
  566. GeoCountDownTimer s8 = new GeoCountDownTimer("51/20/2022");
  567.  
  568. } catch (Exception e) {
  569.  
  570. e.printStackTrace();
  571.  
  572. }
  573.  
  574. m = 2;
  575. d = 122;
  576. y = 2020;
  577. try {
  578.  
  579. GeoCountDownTimer s9 = new GeoCountDownTimer(m, d, y);
  580.  
  581. } catch (Exception e) {
  582.  
  583. e.printStackTrace();
  584.  
  585. }
  586.  
  587. m = 2;
  588. d = 12;
  589. y = 2010;
  590. try {
  591.  
  592. GeoCountDownTimer s10 = new GeoCountDownTimer(m, d, y);
  593.  
  594. } catch (Exception e) {
  595.  
  596. e.printStackTrace();
  597.  
  598. }
  599.  
  600. m = 21;
  601. d = 12;
  602. y = 2020;
  603. try {
  604.  
  605. GeoCountDownTimer s11 = new GeoCountDownTimer(m, d, y);
  606.  
  607. } catch (Exception e) {
  608.  
  609. e.printStackTrace();
  610.  
  611. }
  612.  
  613. try {
  614.  
  615. GeoCountDownTimer s12 = new GeoCountDownTimer("9/1/1/2022");
  616.  
  617. } catch (Exception e) {
  618.  
  619. e.printStackTrace();
  620.  
  621. }
  622.  
  623. GeoCountDownTimer s13 = new GeoCountDownTimer("9/1/2022");
  624. GeoCountDownTimer s14 = new GeoCountDownTimer("9/1/2022");
  625. s13.equals(s14);
  626.  
  627. try {
  628.  
  629. s13.equals(m);
  630.  
  631. } catch (Exception e) {
  632.  
  633. e.printStackTrace();
  634.  
  635. }
  636.  
  637. GeoCountDownTimer s15 = new GeoCountDownTimer("9/1/2021");
  638. s14.compareTo(s15);
  639.  
  640. GeoCountDownTimer s16 = new GeoCountDownTimer("9/1/2023");
  641. s14.compareTo(s16);
  642.  
  643. GeoCountDownTimer s17 = new GeoCountDownTimer("9/2/2022");
  644. s14.compareTo(s17);
  645.  
  646. GeoCountDownTimer s18 = new GeoCountDownTimer("10/2/2022");
  647. s14.compareTo(s18);
  648.  
  649. GeoCountDownTimer s19 = new GeoCountDownTimer("2/10/2019");
  650. for (int i = 0; i < (399); i++)
  651. s19.dec(1);
  652.  
  653. GeoCountDownTimer s20 = new GeoCountDownTimer("2/10/2080");
  654. for (int i = 0; i < (10000); i++)
  655. s20.dec(1);
  656.  
  657. GeoCountDownTimer s21 = new GeoCountDownTimer("2/10/2080");
  658. for (int i = 0; i < (10000); i++)
  659. s21.inc(1);
  660.  
  661. GeoCountDownTimer s22 = new GeoCountDownTimer("2/12/2022");
  662. try {
  663.  
  664. s22.daysToGo("2/10/2022");
  665.  
  666. } catch (Exception e) {
  667.  
  668. e.printStackTrace();
  669.  
  670. }
  671.  
  672. try {
  673.  
  674. s22.daysToGo("2/14/2022");
  675.  
  676. } catch (Exception e) {
  677.  
  678. e.printStackTrace();
  679.  
  680. }
  681.  
  682. s22.daysInFuture(100);
  683. s22.daysInFuture(-100);
  684.  
  685. s22.save("test.txt");
  686. try {
  687.  
  688. s22.load("test.txt");
  689.  
  690. } catch (Exception e) {
  691.  
  692. e.printStackTrace();
  693.  
  694. }
  695.  
  696. try {
  697.  
  698. s22.save("fake$$*&.txt");
  699.  
  700. } catch (Exception e) {
  701.  
  702. e.printStackTrace();
  703.  
  704. }
  705. try {
  706.  
  707. s22.load("fakeTest.txt");
  708.  
  709. } catch (Exception e) {
  710.  
  711. e.printStackTrace();
  712.  
  713. }
  714.  
  715. GeoCountDownTimer s23 = new GeoCountDownTimer();
  716. }
  717. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement