Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project1;
- import java.io.*;
- import java.util.Scanner;
- /*********************************************************************
- * This class is used as a count down timer for dates that are very far in the future.
- *
- * @author Jake Kucinski and Nick Grim
- * @class CIS 163-03
- * @date 9/20/2019
- * @professor Dr. Roger Ferguson
- ********************************************************************/
- public class GeoCountDownTimer {
- /** This is the number of months for a given date */
- private int month;
- /** this is the number of years for a given date */
- private int year;
- /** this is the number of days for a given date */
- private int day;
- /** this is the minimum year this program will allow */
- private static final int MINYEAR = 2019;
- /** Days in each month (assuming months are numbered beginning with 1) */
- private static final int[] DAYS_IN_MONTH = {0, 31, 28, 31, 30, 31, 30, 31,
- 31, 30, 31, 30, 31};
- /** The months of the year in Strings */
- private static final String[] MONTHS = {"", "January", "February",
- "March", "April", "May", "June", "July", "August", "September",
- "October", "November", "December"};
- /****************************************************************
- * Default contructor that creates a GeoCountDownTimer with a
- * zero at every value.
- ****************************************************************/
- public GeoCountDownTimer() {
- this.month = 0;
- this.day = 0;
- this.year = 0;
- }
- /****************************************************************
- * Constructor that creates a GeoCountDownTimer
- * @param month - given months in a date
- * @param day - given days in a date
- * @param year - given year in a date
- ****************************************************************/
- public GeoCountDownTimer(int month, int day, int year) {
- if (isValidDate(month, day, year)) {
- this.month = month;
- this.day = day;
- this.year = year;
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- /****************************************************************
- * Constructor that creates a GeoCountDownTimer
- * @param other - another GeoCountDownTimer object that "this"
- * will be assigned to
- ****************************************************************/
- public void GeoCountDownTimer(GeoCountDownTimer other) {
- this.month = other.month;
- this.day = other.day;
- this.year = other.year;
- }
- /****************************************************************
- * Constructor that creates a GeoCountDownTimer
- * @param geoDate - accepts a String of a date
- ****************************************************************/
- public GeoCountDownTimer(String geoDate) {
- String s[] = geoDate.split("/");
- int m, d, y;
- if(s.length == 3) {
- m = Integer.parseInt(s[0]);
- d = Integer.parseInt(s[1]);
- y = Integer.parseInt(s[2]);
- }
- else{
- throw new IllegalArgumentException();
- }
- if (isValidDate(m, d, y)) {
- this.month = m;
- this.day = d;
- this.year = y;
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- /****************************************************************
- * Method to see if two dates are the same dates
- * @param other - date object passed to see if it equals "this"
- * @return true or false - depending on whether or not they are
- * the same dates
- ****************************************************************/
- public boolean equals(Object other) {
- if(!(other instanceof GeoCountDownTimer)){
- throw new IllegalArgumentException();
- }
- GeoCountDownTimer temp = (GeoCountDownTimer)other;
- return (this.month == temp.month) && (this.day == temp.day) && (this.year == temp.year);
- }
- /****************************************************************
- * Method that compares two given dates
- * @param other - GeoCountDownTimer passed to compare to "this"
- * @return 1 - other date came before "this" date,
- * -1 - "this" date came before other date,
- * 0 - "this" date and other date are the same
- ****************************************************************/
- public int compareTo(GeoCountDownTimer other) {
- int temp = 0;
- if(this.year > other.year) {
- temp = 1;
- }
- else if (this.year < other.year) {
- temp = -1;
- }
- else if (this.year == other.year) {
- if(this.month > other.month) {
- temp = 1;
- }
- else if (this.month < other.month) {
- temp = -1;
- }
- else if (this.month == other.month) {
- if (this.day > other.day) {
- temp = 1;
- }
- else if (this.day < other.day) {
- temp = -1;
- }
- else if (this.day == other.day) {
- temp = 0;
- }
- }
- }
- return temp;
- }
- /****************************************************************
- * Method used to decrement days from "this" date, calls dec()
- * to decrement
- * @param days - how many days we want to decrement from "this"
- ****************************************************************/
- public void dec(int days) {
- GeoCountDownTimer current = this;
- int i = days;
- while(i > 0) {
- this.dec();
- i--;
- }
- }
- /****************************************************************
- * Method used to decrement "this" date by a single day
- ****************************************************************/
- public void dec() {
- GeoCountDownTimer current = this;
- if(current.day == 1) {
- if(current.month == 1) {
- if(current.year == 2019){
- current.month = 1;
- current.day = 1;
- current.year = 2019;
- }
- current.year = current.year - 1;
- current.month = 12;
- current.day = DAYS_IN_MONTH[current.month];
- }
- else {
- current.month = current.month - 1;
- if(current.month == 2 && isLeapYear(year)) {
- current.day = DAYS_IN_MONTH[current.month] + 1;
- }
- else {
- current.day = DAYS_IN_MONTH[current.month];
- }
- }
- }
- else {
- current.day = current.day - 1;
- }
- }
- /****************************************************************
- * Method used to increment days from "this" date, calls inc()
- * to increment
- * @param days - how many days we want to increment from "this"
- ****************************************************************/
- public void inc(int days) {
- GeoCountDownTimer current = this;
- int i = days;
- while(i > 0) {
- this.inc();
- i--;
- }
- }
- /****************************************************************
- * Method used to increment "this" date by a single day
- ****************************************************************/
- public void inc() {
- GeoCountDownTimer current = this;
- if((current.month == 2) && (isLeapYear(current.year)) && (current.day == DAYS_IN_MONTH[current.month])) {
- current.day = current.day + 1;
- }
- else if((current.month == 2) && (isLeapYear(current.year)) && (current.day == DAYS_IN_MONTH[current.month] + 1)) {
- current.month = 3;
- current.day = 1;
- }
- else if(current.day == DAYS_IN_MONTH[current.month]) {
- if(current.month == 12) {
- current.month = 1;
- current.day = 1;
- current.year = current.year + 1;
- }
- else {
- current.month = current.month + 1;
- current.day = 1;
- }
- }
- else {
- current.day = current.day + 1;
- }
- }
- /****************************************************************
- * Method that converts "this" date into a string
- * @return s - string of "this" date
- ****************************************************************/
- public String toString() {
- String s = MONTHS[this.month] + " " + this.day + ", " + this.year;
- return s;
- }
- /****************************************************************
- * Method that converts "this" date into a string using numerals
- * @return s - string of "this" date
- ****************************************************************/
- public String toDateString() {
- String s = this.month + "/" + this.day + "/" + this.year;
- return s;
- }
- /****************************************************************
- * Method that checks to see if the input date is a valid date
- * @param month - months in a given date
- * @param day - days in a given date
- * @param year - year in a given date
- * @return true or false - depending on whether the date is valid
- ****************************************************************/
- private static boolean isValidDate(int month, int day, int year) {
- int t = 0;
- if(isLeapYear(year)) {
- t = 1;
- }
- if((t == 1) && (month == 2)) {
- if ((month > 0 && month <= 12) && (day <= DAYS_IN_MONTH[month] + 1 && day > 0) && (year >= MINYEAR)) {
- return true;
- }
- else {
- return false;
- }
- }
- if ((month > 0 && month <= 12) && (day <= DAYS_IN_MONTH[month] && day > 0) && (year >= MINYEAR)) {
- return true;
- }
- else {
- return false;
- }
- }
- /****************************************************************
- * Method to check is a given year is a leap year or not
- * @param year - the year being checked to see if it is a leap
- * year
- * @return true of false - whether the input year is a leap year
- ****************************************************************/
- public static boolean isLeapYear(int year) {
- return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
- }
- /****************************************************************
- * Method to save a given GeoCountDownTimer object to a .txt file
- * @param filename - the user input filename where the
- * GeoCountDownTimer object is stored
- ****************************************************************/
- public void save(String filename) {
- PrintWriter out = null;
- try {
- out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
- } catch (IOException e) {
- e.printStackTrace();
- }
- out.println (month);
- out.println (day);
- out.println (year);
- out.close();
- }
- /****************************************************************
- * Method to load a given GeoCountDownTimer object from a .txt
- * file
- * @param fileName - the user input filename where the
- * GeoCountDownTimer object is taken from
- ****************************************************************/
- public void load(String fileName) throws FileNotFoundException {
- try {
- Scanner fileReader = new Scanner(new File(fileName));
- String geoDate = fileReader.next();
- } catch(Exception error) {
- System.out.println("File not found");
- }
- }
- /****************************************************************
- * Method to see how many days are between "this" date and the
- * input fromDate
- * @param fromDate - the date to which we are seeing how many
- * days away it is
- * @return i - how many days are between "this" and fromDate
- ****************************************************************/
- public int daysToGo(String fromDate) {
- GeoCountDownTimer temp = new GeoCountDownTimer(fromDate);
- if(this.compareTo(temp) == -1) {
- throw new IllegalArgumentException();
- }
- int i = 0;
- while(this.compareTo(temp) != 0) {
- temp.inc();
- i++;
- }
- return i;
- }
- /****************************************************************
- * Method to return a GeoCountDownTimer object of a given date
- * so many days in the future, or past
- * @param n - the number of days we are looking for from now,
- * if this number is a negative then we are counting
- * so many days into the past
- * @return current - returns the GeoCountDownTimer object
- ****************************************************************/
- public GeoCountDownTimer daysInFuture(int n) {
- GeoCountDownTimer current = new GeoCountDownTimer(this.month, this.day, this.year);
- if(n > 0) {
- current.inc(n);
- return current;
- }
- else if(n < 0) {
- current.dec(n);
- return current;
- }
- return current;
- }
- /****************************************************************
- * Main method used for testing the various methods and functions
- * written throughout this class
- ****************************************************************/
- public static void main(String[] args) {
- GeoCountDownTimer s = new GeoCountDownTimer("2/10/2020");
- System.out.println("Date: " + s);
- GeoCountDownTimer s1 = new GeoCountDownTimer("2/10/2022");
- System.out.println("Date: " + s1.toDateString());
- s1.dec(365);
- System.out.println("Date: " + s1);
- s1.inc(365);
- System.out.println("Date: " + s1);
- GeoCountDownTimer s2 = new GeoCountDownTimer("2/10/2019");
- for (int i = 0; i < (365 + 366 ); i++)
- s2.inc(1);
- System.out.println("Date: " + s2);
- int d = 2;
- int m = 12;
- int y = 2020;
- GeoCountDownTimer s3 = new GeoCountDownTimer(d, m ,y);
- GeoCountDownTimer s4 = new GeoCountDownTimer("8/20/2028");
- s4.GeoCountDownTimer(s3);
- GeoCountDownTimer s5 = new GeoCountDownTimer("5/20/2028");
- System.out.println(s5);
- int output = s5.daysToGo("3/21/2028");
- System.out.println("Output: " + output);
- GeoCountDownTimer s6 = new GeoCountDownTimer("5/20/20282");
- try {
- GeoCountDownTimer s7 = new GeoCountDownTimer("5/201/2028");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- GeoCountDownTimer s8 = new GeoCountDownTimer("51/20/2022");
- } catch (Exception e) {
- e.printStackTrace();
- }
- m = 2;
- d = 122;
- y = 2020;
- try {
- GeoCountDownTimer s9 = new GeoCountDownTimer(m, d, y);
- } catch (Exception e) {
- e.printStackTrace();
- }
- m = 2;
- d = 12;
- y = 2010;
- try {
- GeoCountDownTimer s10 = new GeoCountDownTimer(m, d, y);
- } catch (Exception e) {
- e.printStackTrace();
- }
- m = 21;
- d = 12;
- y = 2020;
- try {
- GeoCountDownTimer s11 = new GeoCountDownTimer(m, d, y);
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- GeoCountDownTimer s12 = new GeoCountDownTimer("9/1/1/2022");
- } catch (Exception e) {
- e.printStackTrace();
- }
- GeoCountDownTimer s13 = new GeoCountDownTimer("9/1/2022");
- GeoCountDownTimer s14 = new GeoCountDownTimer("9/1/2022");
- s13.equals(s14);
- try {
- s13.equals(m);
- } catch (Exception e) {
- e.printStackTrace();
- }
- GeoCountDownTimer s15 = new GeoCountDownTimer("9/1/2021");
- s14.compareTo(s15);
- GeoCountDownTimer s16 = new GeoCountDownTimer("9/1/2023");
- s14.compareTo(s16);
- GeoCountDownTimer s17 = new GeoCountDownTimer("9/2/2022");
- s14.compareTo(s17);
- GeoCountDownTimer s18 = new GeoCountDownTimer("10/2/2022");
- s14.compareTo(s18);
- GeoCountDownTimer s19 = new GeoCountDownTimer("2/10/2019");
- for (int i = 0; i < (399); i++)
- s19.dec(1);
- GeoCountDownTimer s20 = new GeoCountDownTimer("2/10/2080");
- for (int i = 0; i < (10000); i++)
- s20.dec(1);
- GeoCountDownTimer s21 = new GeoCountDownTimer("2/10/2080");
- for (int i = 0; i < (10000); i++)
- s21.inc(1);
- GeoCountDownTimer s22 = new GeoCountDownTimer("2/12/2022");
- try {
- s22.daysToGo("2/10/2022");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- s22.daysToGo("2/14/2022");
- } catch (Exception e) {
- e.printStackTrace();
- }
- s22.daysInFuture(100);
- s22.daysInFuture(-100);
- s22.save("test.txt");
- try {
- s22.load("test.txt");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- s22.save("fake$$*&.txt");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- s22.load("fakeTest.txt");
- } catch (Exception e) {
- e.printStackTrace();
- }
- GeoCountDownTimer s23 = new GeoCountDownTimer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement