Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.time.LocalDate;
  3.  
  4. public class Better
  5. {
  6. /*
  7. This program manages dates, it take user entered keyboard commands.
  8. There are 4 undeclared date variables d1 (date 1), d2 (date 2), d3 (date 3) and d4 (date 4).
  9. These 4 date vars do not have values set. The user must declare the variable (e.g. "set d1") then set the value (e.g. "Set d1 03 15 2018")
  10. Example dates:
  11. d1 = 03-15-2018
  12. d2 = 03-16-2018
  13. d3 = 03-17-2018
  14. d4 = 03-18-2018
  15.  
  16. The commands the user can leverage (keyboard input) are:
  17. 1. "set": Used to initialze (declare) a date variable before it can be used. Also used to set the date to... well a date.
  18. a. Example: "set d1" -- initialize/declare the date variable.
  19. b. Example: "set d1 03 15 2018" -- sets the date for the variable.
  20. 2. "minus day": Self explainatory. Removes x number of days from the date.
  21. a. Example" "minus day d1 5", returns "03/10/2018"
  22. 3. "minus month": Self explainatory. Removes x number of month from the date.
  23. a. Example" "minus month d1 1", returns "02/15/2018"
  24. 4. "minus year": Self explainatory. Removes x number of years from the date.
  25. a. Example" "minus month d1 9", returns "02/15/2009"
  26. 5. "plus day": Self explainatory. Adds x number of days from the date.
  27. a. Example" "plus day d1 5", returns "03/20/2018"
  28. 6. "plus month": Self explainatory. Adds x number of month from the date.
  29. a. Example" "plus month d1 1", returns "04/15/2018"
  30. 7. "plus year": Self explainatory. Adds x number of years from the date.
  31. a. Example" "plus month d1 9", returns "02/15/2027"
  32. 8. "show day": Self explainatory. Displays d1 current value for day.
  33. a. Example" "show day d1 5", returns "03/20/2018"
  34. 9. "show month": Self explainatory. Displays d1 current value for month.
  35. a. Example" "show month d1 1", returns "04/15/2018"
  36. 10. "show year": Self explainatory. Displays d1 current value for year.
  37. a. Example" "show month d1 9", returns "02/15/2027"
  38. 11. "show dayOfWeek": Self explainatory. Displays d1 current value for day of the week (Sunday - Saturday).
  39. a. Example" "show DayOfWeek d1", returns "Thursday"
  40. 12. "show before": Shows "true" if the Date in first declared variable is before the date in the second.
  41. a. Example" "show before d1 d2", returns "true"
  42. b. This means both d1 and d2 were declared and set before this could be called.
  43. 13. "show after": Shows "true" if the Date in first declared variable is after the date in the second.
  44. a. Example" "show after d3 d4", returns "false"
  45. b. This means both d3 and d4 were declared and set before this could be called.
  46. 12. "show equal": Shows "true" if the Date in first declared variable is the same as the date in the second.
  47. a. Example" "show equal d1 d2", returns "false"
  48. b. This means both d1 and d2 were declared and set before this could be called.
  49. 13. "show value": Shows the date stored in the variable
  50. a. Example" "show d4", returns "03-18-2018"
  51. b. This means d4 was declared and set before this could be called.
  52. */
  53.  
  54. private static int getIndex(String str)
  55. { //
  56. int val = -10; // default if this shows, shit done broke
  57. switch(str)
  58. {
  59. case "d1": // new years day
  60. val = 0;
  61. break;
  62.  
  63. case "d2": // independance day
  64. val = 1;
  65. break;
  66.  
  67. case "d3": // thanksgiving
  68. val = 2;
  69. break;
  70.  
  71. case "d4": // santamas
  72. val = 3;
  73. break;
  74.  
  75. default: // these are not the droids you're looking for
  76. val = -1;
  77. break;
  78. }
  79. // debug lines, comment out before submitting exam
  80. System.out.println("[DEBUG] str: " +str); // remove me later -- debug
  81. System.out.println("[DEBUG] val: " +val); // remove me later -- debug
  82.  
  83. return val;
  84. }
  85.  
  86. public static void main(String[] args)
  87. {
  88. Scanner kbd = new Scanner(System.in); // get they user keyboard input
  89. LocalDate[] myDate = new LocalDate[4]; // teacher, a programmer of 15 years doesn't know wtf array value 4 equals and cannot find this on google. explain this please.
  90.  
  91. while(kbd.hasNextLine())
  92. { // loop though the user entered lines
  93. String[] word = (kbd.nextLine()).split(" "); // this takes the keyboard value and splits on empty space, so I have an array of "words" (or numbers).
  94.  
  95. // parse through the first word typed in by user
  96. String uCmd = word[0]; // user keyboard command (array item 1)
  97. int cmdLength = word.length; // get the length of an array (how many items are in the array)
  98.  
  99. int a = getIndex(word[0]); // first index in array "word" (should be set/minus/plus/show)
  100. int b = getIndex(word[1]); // second index in array "word" (should be something else that comes after initial command)
  101. if((cmdLength == 2 && myDate[b] == null && !(uCmd.equals("set"))) || word[0] == null)
  102. { // shits done broke.
  103. if(word[0] == null)
  104. {
  105. System.out.println("Invalid Command: "+word[0]); // first var not declared
  106. }
  107. else
  108. {
  109. System.out.println("Uninitialized Variable: "+word[1]); // didn't set date var
  110. }
  111. }
  112. else
  113. { // command appears to be properly formatted, run through command check
  114. if(uCmd.equals("set"))
  115. { // set command stuff
  116. System.out.println("set: "+word[1]);
  117. /*int year = Integer.parseInt(word[2]);
  118. int month = Integer.parseInt(word[3]);
  119. int day = Integer.parseInt(word[4]);
  120. d[a] = LocalDate.of(year, month, day);
  121. */
  122. }
  123. else if(uCmd.equals("minus"))
  124. { // minus command stuff
  125. System.out.println("minus: "+word[1]);
  126. }
  127. else if(uCmd.equals("plus"))
  128. { // plus command stuff
  129. System.out.println("plus: "+word[1]);
  130. }
  131. else if(uCmd.equals("show"))
  132. { // show command stuff
  133. System.out.println("show: "+word[1]);
  134. /*if(d[a] == null)
  135. { // this is checking to see if they even set d1-d4 or if they typed in stupid shit
  136. System.out.println("Uninitialized Variable: "+word[1]);
  137. }
  138. else
  139. {
  140. // they got a second command to filter further (show day/month/year/dayOfWeek/before/after/equals)
  141. if(word[1].equals("day"))
  142. {
  143. int index = getIndex(word[2]);
  144. if(myDate[index] == null)
  145. {
  146. System.out.println("Uninitialized Variable: "+word[2]);
  147. }
  148. else
  149. {
  150. System.out.println("The day is : "+myDate[index]);
  151. }
  152. }
  153. }*/
  154. }
  155. else
  156. {
  157. System.out.println("Invalid Command: "+word[0]);
  158. }
  159. }
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement