Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //File: Birthdays.java
- //Author: Ian Bartlow
- //Date: 2/20/20
- //Purpose: To compare 2 peoples birthdays and see which one is closest,
- //then display the amoutn of days until their birthday
- import java.util.*;
- import java.text.SimpleDateFormat;//got this to format the calendar api date,
- //from http://tutorials.jenkov.com/java-internationalization/simpledateformat.html
- public class Birthdays
- {
- public static void main(String args[])
- {
- Scanner sc = new Scanner(System.in);
- System.out.println(); //top blank line
- intro();
- System.out.println();
- int yourBirthMonth = getYourBirthMonth(sc);
- int yourBirthday = getYourBirthday(sc);
- System.out.println();
- int friendBirthMonth = getFriendBirthMonth(sc);
- int friendBirthday = getFriendBirthday(sc);
- int currentDay = getCurrentDay();
- int currentMonth = getCurrentMonth();
- int currentDaysInYear = calculate(currentDay,currentMonth);
- int yourDaysInYear = calculate(yourBirthday, yourBirthMonth);
- int friendDaysInYear = calculate(friendBirthday, friendBirthMonth);
- if (yourBirthMonth > currentMonth && friendBirthMonth < currentMonth)
- {
- yourDaysInYear = yourDaysInYear - currentDaysInYear;
- friendDaysInYear = 365 - friendDaysInYear - currentDaysInYear;
- System.out.println("Your birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if (yourBirthMonth < currentMonth && friendBirthday > currentMonth)
- {
- yourDaysInYear = 365 - yourDaysInYear - currentDaysInYear;
- friendDaysInYear = friendDaysInYear - currentDaysInYear;
- System.out.println("Your friend's birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(yourBirthMonth < currentMonth && friendBirthMonth < currentMonth)
- {
- yourDaysInYear = 365 - yourDaysInYear - currentDaysInYear;
- friendDaysInYear = 365 - friendDaysInYear - currentDaysInYear;
- if (yourBirthMonth < friendBirthMonth)
- {
- System.out.println("Your birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(friendBirthMonth < yourBirthMonth)
- {
- System.out.println("Your friend's birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if (yourBirthday < friendBirthday)
- {
- System.out.println("Your birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(friendBirthday < yourBirthday)
- {
- System.out.println("Your friend's birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else
- {
- System.out.println("You were both born on the same day!");
- }
- }
- else if(yourBirthday > currentMonth && friendBirthMonth > currentMonth)
- {
- yourDaysInYear = yourDaysInYear - currentDaysInYear;
- friendDaysInYear = friendDaysInYear - currentDaysInYear;
- if(yourBirthMonth < friendBirthMonth)
- {
- System.out.println("Your birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(friendBirthMonth < yourBirthMonth)
- {
- System.out.println("Your friend's birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(yourBirthday < friendBirthday)
- {
- System.out.println("Your birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else if(friendBirthday < yourBirthday)
- {
- System.out.println("Your friend's birthday is first!");
- System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
- System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
- }
- else
- {
- System.out.println("You were both born on the same day!");
- }
- }
- System.out.println(); //bottom blank
- }
- public static void intro()
- {
- System.out.println("Program that computes number of days until");
- System.out.println("your birthday and the birthday of a frined.");
- System.out.println("Written by Ian Bartlow.");
- }
- public static int getYourBirthMonth(Scanner sc)
- {
- System.out.print("Enter the month of your birth (1-12): ");
- int yourBirthMonth = sc.nextInt();
- System.out.println(); // for turnin
- return yourBirthMonth;
- }
- public static int getYourBirthday(Scanner sc)
- {
- System.out.print("Enter the day of the month of your birth (1-31): ");
- int yourBirthday = sc.nextInt();
- System.out.println(); //for turnin
- return yourBirthday;
- }
- public static int getFriendBirthMonth(Scanner sc)
- {
- System.out.print("Enter the month of your friend's birth (1-12): ");
- int friendBirthMonth = sc.nextInt();
- System.out.println(); //for turnin
- return friendBirthMonth;
- }
- public static int getFriendBirthday(Scanner sc)
- {
- System.out.print("Enter the day of the month of your friend's birth (1-31): ");
- int friendBirthday = sc.nextInt();
- System.out.println(); //for turnin
- return friendBirthday;
- }
- public static int getCurrentDay()
- {
- //got this from https://stackabuse.com/how-to-get-current-date-and-time-in-java/
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat dayFormatter = new SimpleDateFormat("dd");
- String stringDay = dayFormatter.format(calendar.getTime());
- int currentDay = Integer.parseInt(stringDay);
- return currentDay;
- }
- public static int getCurrentMonth()
- {
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat monthFormatter = new SimpleDateFormat("MM");
- String stringMonth = monthFormatter.format(calendar.getTime());
- int currentMonth = Integer.parseInt(stringMonth);
- return currentMonth;
- }
- public static int calculate(int days, int month)
- {
- int daysInYear = 0;
- if(month == 1)
- {
- daysInYear = 31 + days;
- }
- else if(month == 2)
- {
- daysInYear = 31 + 28 + days;
- }
- else if(month == 3)
- {
- daysInYear = 31 + 28 + 31 + days;
- }
- else if(month == 4)
- {
- daysInYear = 31 * 2 + 28 + 30 + days;
- }
- else if(month == 5)
- {
- daysInYear = 31 * 3 + 30 + 28 + days;
- }
- else if(month == 6)
- {
- daysInYear = 31 * 3 + 30 * 2 + 28 + days;
- }
- else if(month == 7)
- {
- daysInYear = 31 * 4 + 30 * 2 + 28 + days;
- }
- else if(month == 8)
- {
- daysInYear = 31 * 5 + 30 * 2 + 28 + days;
- }
- else if(month == 9)
- {
- daysInYear = 31 * 5 + 30 * 3 + 28 + days;
- }
- else if(month == 10)
- {
- daysInYear = 31 * 6 + 30 * 3 + 28 + days;
- }
- else if(month == 11)
- {
- daysInYear = 31 * 6 + 30 * 4 + 28 + days;
- }
- else if(month == 12)
- {
- daysInYear = 31 * 6 + 30 * 4 + 28 + days;
- }
- return daysInYear;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement