Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. //File: Birthdays.java
  2. //Author: Ian Bartlow
  3. //Date: 2/20/20
  4. //Purpose: To compare 2 peoples birthdays and see which one is closest,
  5. //then display the amoutn of days until their birthday
  6. import java.util.*;
  7. import java.text.SimpleDateFormat;//got this to format the calendar api date,
  8. //from http://tutorials.jenkov.com/java-internationalization/simpledateformat.html
  9.  
  10.  
  11.  
  12. public class Birthdays
  13. {
  14.  
  15.  
  16.  
  17. public static void main(String args[])
  18. {
  19. Scanner sc = new Scanner(System.in);
  20.  
  21. System.out.println(); //top blank line
  22. intro();
  23. System.out.println();
  24. int yourBirthMonth = getYourBirthMonth(sc);
  25. int yourBirthday = getYourBirthday(sc);
  26. System.out.println();
  27. int friendBirthMonth = getFriendBirthMonth(sc);
  28. int friendBirthday = getFriendBirthday(sc);
  29. int currentDay = getCurrentDay();
  30. int currentMonth = getCurrentMonth();
  31. int currentDaysInYear = calculate(currentDay,currentMonth);
  32. int yourDaysInYear = calculate(yourBirthday, yourBirthMonth);
  33. int friendDaysInYear = calculate(friendBirthday, friendBirthMonth);
  34.  
  35.  
  36.  
  37.  
  38. if (yourBirthMonth > currentMonth && friendBirthMonth < currentMonth)
  39. {
  40. yourDaysInYear = yourDaysInYear - currentDaysInYear;
  41. friendDaysInYear = 365 - friendDaysInYear - currentDaysInYear;
  42.  
  43. System.out.println("Your birthday is first!");
  44. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  45. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  46.  
  47. }
  48.  
  49. else if (yourBirthMonth < currentMonth && friendBirthday > currentMonth)
  50. {
  51. yourDaysInYear = 365 - yourDaysInYear - currentDaysInYear;
  52. friendDaysInYear = friendDaysInYear - currentDaysInYear;
  53.  
  54. System.out.println("Your friend's birthday is first!");
  55. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  56. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  57.  
  58. }
  59.  
  60. else if(yourBirthMonth < currentMonth && friendBirthMonth < currentMonth)
  61. {
  62. yourDaysInYear = 365 - yourDaysInYear - currentDaysInYear;
  63. friendDaysInYear = 365 - friendDaysInYear - currentDaysInYear;
  64.  
  65. if (yourBirthMonth < friendBirthMonth)
  66. {
  67. System.out.println("Your birthday is first!");
  68. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  69. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  70.  
  71. }
  72.  
  73. else if(friendBirthMonth < yourBirthMonth)
  74. {
  75. System.out.println("Your friend's birthday is first!");
  76. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  77. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  78.  
  79. }
  80. else if (yourBirthday < friendBirthday)
  81. {
  82.  
  83. System.out.println("Your birthday is first!");
  84. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  85. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  86.  
  87. }
  88.  
  89. else if(friendBirthday < yourBirthday)
  90. {
  91.  
  92. System.out.println("Your friend's birthday is first!");
  93. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  94. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  95.  
  96. }
  97.  
  98. else
  99. {
  100. System.out.println("You were both born on the same day!");
  101. }
  102. }
  103.  
  104. else if(yourBirthday > currentMonth && friendBirthMonth > currentMonth)
  105. {
  106. yourDaysInYear = yourDaysInYear - currentDaysInYear;
  107. friendDaysInYear = friendDaysInYear - currentDaysInYear;
  108.  
  109. if(yourBirthMonth < friendBirthMonth)
  110. {
  111. System.out.println("Your birthday is first!");
  112. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  113. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  114.  
  115. }
  116. else if(friendBirthMonth < yourBirthMonth)
  117. {
  118. System.out.println("Your friend's birthday is first!");
  119. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  120. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  121.  
  122. }
  123.  
  124. else if(yourBirthday < friendBirthday)
  125. {
  126. System.out.println("Your birthday is first!");
  127. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  128. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  129.  
  130. }
  131.  
  132. else if(friendBirthday < yourBirthday)
  133. {
  134. System.out.println("Your friend's birthday is first!");
  135. System.out.println("You have " + yourDaysInYear + " day(s) to wait until your birthday (" + yourBirthday + "/" + yourBirthMonth + ").");
  136. System.out.println("Your friend has " + friendDaysInYear + " day(s) to wait until their birthday (" + friendBirthday + "/" + friendBirthMonth + ").");
  137.  
  138. }
  139.  
  140. else
  141. {
  142. System.out.println("You were both born on the same day!");
  143. }
  144.  
  145.  
  146. }
  147. System.out.println(); //bottom blank
  148.  
  149.  
  150.  
  151. }
  152.  
  153. public static void intro()
  154. {
  155. System.out.println("Program that computes number of days until");
  156. System.out.println("your birthday and the birthday of a frined.");
  157. System.out.println("Written by Ian Bartlow.");
  158.  
  159. }
  160.  
  161. public static int getYourBirthMonth(Scanner sc)
  162. {
  163. System.out.print("Enter the month of your birth (1-12): ");
  164. int yourBirthMonth = sc.nextInt();
  165. System.out.println(); // for turnin
  166. return yourBirthMonth;
  167.  
  168. }
  169.  
  170. public static int getYourBirthday(Scanner sc)
  171. {
  172. System.out.print("Enter the day of the month of your birth (1-31): ");
  173. int yourBirthday = sc.nextInt();
  174. System.out.println(); //for turnin
  175. return yourBirthday;
  176. }
  177.  
  178. public static int getFriendBirthMonth(Scanner sc)
  179. {
  180. System.out.print("Enter the month of your friend's birth (1-12): ");
  181. int friendBirthMonth = sc.nextInt();
  182. System.out.println(); //for turnin
  183. return friendBirthMonth;
  184. }
  185.  
  186. public static int getFriendBirthday(Scanner sc)
  187. {
  188. System.out.print("Enter the day of the month of your friend's birth (1-31): ");
  189. int friendBirthday = sc.nextInt();
  190. System.out.println(); //for turnin
  191. return friendBirthday;
  192. }
  193.  
  194. public static int getCurrentDay()
  195. {
  196. //got this from https://stackabuse.com/how-to-get-current-date-and-time-in-java/
  197. Calendar calendar = Calendar.getInstance();
  198. SimpleDateFormat dayFormatter = new SimpleDateFormat("dd");
  199. String stringDay = dayFormatter.format(calendar.getTime());
  200. int currentDay = Integer.parseInt(stringDay);
  201. return currentDay;
  202. }
  203.  
  204. public static int getCurrentMonth()
  205. {
  206.  
  207. Calendar calendar = Calendar.getInstance();
  208. SimpleDateFormat monthFormatter = new SimpleDateFormat("MM");
  209. String stringMonth = monthFormatter.format(calendar.getTime());
  210. int currentMonth = Integer.parseInt(stringMonth);
  211. return currentMonth;
  212.  
  213. }
  214.  
  215. public static int calculate(int days, int month)
  216. {
  217. int daysInYear = 0;
  218. if(month == 1)
  219. {
  220. daysInYear = 31 + days;
  221. }
  222.  
  223. else if(month == 2)
  224. {
  225. daysInYear = 31 + 28 + days;
  226. }
  227.  
  228. else if(month == 3)
  229. {
  230. daysInYear = 31 + 28 + 31 + days;
  231. }
  232. else if(month == 4)
  233. {
  234. daysInYear = 31 * 2 + 28 + 30 + days;
  235. }
  236. else if(month == 5)
  237. {
  238. daysInYear = 31 * 3 + 30 + 28 + days;
  239. }
  240. else if(month == 6)
  241. {
  242. daysInYear = 31 * 3 + 30 * 2 + 28 + days;
  243. }
  244. else if(month == 7)
  245. {
  246. daysInYear = 31 * 4 + 30 * 2 + 28 + days;
  247. }
  248. else if(month == 8)
  249. {
  250. daysInYear = 31 * 5 + 30 * 2 + 28 + days;
  251. }
  252. else if(month == 9)
  253. {
  254. daysInYear = 31 * 5 + 30 * 3 + 28 + days;
  255. }
  256. else if(month == 10)
  257. {
  258. daysInYear = 31 * 6 + 30 * 3 + 28 + days;
  259. }
  260. else if(month == 11)
  261. {
  262. daysInYear = 31 * 6 + 30 * 4 + 28 + days;
  263. }
  264. else if(month == 12)
  265. {
  266. daysInYear = 31 * 6 + 30 * 4 + 28 + days;
  267. }
  268.  
  269.  
  270. return daysInYear;
  271. }
  272.  
  273.  
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement