Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.time.LocalDate;
- public class Better
- {
- /*
- This program manages dates, it take user entered keyboard commands.
- There are 4 undeclared date variables d1 (date 1), d2 (date 2), d3 (date 3) and d4 (date 4).
- 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")
- Example dates:
- d1 = 03-15-2018
- d2 = 03-16-2018
- d3 = 03-17-2018
- d4 = 03-18-2018
- The commands the user can leverage (keyboard input) are:
- 1. "set": Used to initialze (declare) a date variable before it can be used. Also used to set the date to... well a date.
- a. Example: "set d1" -- initialize/declare the date variable.
- b. Example: "set d1 03 15 2018" -- sets the date for the variable.
- 2. "minus day": Self explainatory. Removes x number of days from the date.
- a. Example" "minus day d1 5", returns "03/10/2018"
- 3. "minus month": Self explainatory. Removes x number of month from the date.
- a. Example" "minus month d1 1", returns "02/15/2018"
- 4. "minus year": Self explainatory. Removes x number of years from the date.
- a. Example" "minus month d1 9", returns "02/15/2009"
- 5. "plus day": Self explainatory. Adds x number of days from the date.
- a. Example" "plus day d1 5", returns "03/20/2018"
- 6. "plus month": Self explainatory. Adds x number of month from the date.
- a. Example" "plus month d1 1", returns "04/15/2018"
- 7. "plus year": Self explainatory. Adds x number of years from the date.
- a. Example" "plus month d1 9", returns "02/15/2027"
- 8. "show day": Self explainatory. Displays d1 current value for day.
- a. Example" "show day d1 5", returns "03/20/2018"
- 9. "show month": Self explainatory. Displays d1 current value for month.
- a. Example" "show month d1 1", returns "04/15/2018"
- 10. "show year": Self explainatory. Displays d1 current value for year.
- a. Example" "show month d1 9", returns "02/15/2027"
- 11. "show dayOfWeek": Self explainatory. Displays d1 current value for day of the week (Sunday - Saturday).
- a. Example" "show DayOfWeek d1", returns "Thursday"
- 12. "show before": Shows "true" if the Date in first declared variable is before the date in the second.
- a. Example" "show before d1 d2", returns "true"
- b. This means both d1 and d2 were declared and set before this could be called.
- 13. "show after": Shows "true" if the Date in first declared variable is after the date in the second.
- a. Example" "show after d3 d4", returns "false"
- b. This means both d3 and d4 were declared and set before this could be called.
- 12. "show equal": Shows "true" if the Date in first declared variable is the same as the date in the second.
- a. Example" "show equal d1 d2", returns "false"
- b. This means both d1 and d2 were declared and set before this could be called.
- 13. "show value": Shows the date stored in the variable
- a. Example" "show d4", returns "03-18-2018"
- b. This means d4 was declared and set before this could be called.
- */
- private static int getIndex(String str)
- { //
- int val = -10; // default if this shows, shit done broke
- switch(str)
- {
- case "d1": // new years day
- val = 0;
- break;
- case "d2": // independance day
- val = 1;
- break;
- case "d3": // thanksgiving
- val = 2;
- break;
- case "d4": // santamas
- val = 3;
- break;
- default: // these are not the droids you're looking for
- val = -1;
- break;
- }
- // debug lines, comment out before submitting exam
- System.out.println("[DEBUG] str: " +str); // remove me later -- debug
- System.out.println("[DEBUG] val: " +val); // remove me later -- debug
- return val;
- }
- public static void main(String[] args)
- {
- Scanner kbd = new Scanner(System.in); // get they user keyboard input
- 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.
- while(kbd.hasNextLine())
- { // loop though the user entered lines
- String[] word = (kbd.nextLine()).split(" "); // this takes the keyboard value and splits on empty space, so I have an array of "words" (or numbers).
- // parse through the first word typed in by user
- String uCmd = word[0]; // user keyboard command (array item 1)
- int cmdLength = word.length; // get the length of an array (how many items are in the array)
- int a = getIndex(word[0]); // first index in array "word" (should be set/minus/plus/show)
- int b = getIndex(word[1]); // second index in array "word" (should be something else that comes after initial command)
- if((cmdLength == 2 && myDate[b] == null && !(uCmd.equals("set"))) || word[0] == null)
- { // shits done broke.
- if(word[0] == null)
- {
- System.out.println("Invalid Command: "+word[0]); // first var not declared
- }
- else
- {
- System.out.println("Uninitialized Variable: "+word[1]); // didn't set date var
- }
- }
- else
- { // command appears to be properly formatted, run through command check
- if(uCmd.equals("set"))
- { // set command stuff
- System.out.println("set: "+word[1]);
- /*int year = Integer.parseInt(word[2]);
- int month = Integer.parseInt(word[3]);
- int day = Integer.parseInt(word[4]);
- d[a] = LocalDate.of(year, month, day);
- */
- }
- else if(uCmd.equals("minus"))
- { // minus command stuff
- System.out.println("minus: "+word[1]);
- }
- else if(uCmd.equals("plus"))
- { // plus command stuff
- System.out.println("plus: "+word[1]);
- }
- else if(uCmd.equals("show"))
- { // show command stuff
- System.out.println("show: "+word[1]);
- /*if(d[a] == null)
- { // this is checking to see if they even set d1-d4 or if they typed in stupid shit
- System.out.println("Uninitialized Variable: "+word[1]);
- }
- else
- {
- // they got a second command to filter further (show day/month/year/dayOfWeek/before/after/equals)
- if(word[1].equals("day"))
- {
- int index = getIndex(word[2]);
- if(myDate[index] == null)
- {
- System.out.println("Uninitialized Variable: "+word[2]);
- }
- else
- {
- System.out.println("The day is : "+myDate[index]);
- }
- }
- }*/
- }
- else
- {
- System.out.println("Invalid Command: "+word[0]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement