/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17mar30; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jcmtiernan */ public class Spr17Mar30 { /** * @param args the command line arguments */ public static void main(String[] args) { Logger logger = Logger.getLogger(LoggingExamples.class.getName()); logger.setLevel(Level.OFF); //INFO is on; OFF is off Scanner input = new Scanner(System.in); PrintWriter outfile; try { outfile = new PrintWriter("timesOut.txt"); } catch (FileNotFoundException fnf) { outfile = new PrintWriter(System.out); } Scanner keybd = new Scanner(System.in); JCMTtimeMar29 myTime = new JCMTtimeMar29(); System.out.println("A default time value is "+myTime.toStringMDYHM()); /* JCMTtimeMar29 feb1Time = new JCMTtimeMar29(2017, 2, 28); System.out.println("A Feb 28 2017 value is "+feb1Time.toStringMDYHM()); outfile.println("A Feb 28 2017 value is "+feb1Time.toStringMDYHM()); JCMTtimeMar29 feb2Time = new JCMTtimeMar29(2017, 2, 29); System.out.println("A Feb 29 2017 value is "+feb2Time.toStringMDYHM()); outfile.println("A Feb 29 2017 value is "+feb2Time.toStringMDYHM()); JCMTtimeMar29 feb3Time = new JCMTtimeMar29(2016, 2, 29); System.out.println("A Feb 29 2016 value is "+feb3Time.toStringMDYHM()); outfile.println("A Feb 29 2016 value is "+feb3Time.toStringMDYHM()); JCMTtimeMar29 feb4Time = new JCMTtimeMar29(2016, 2, 30); System.out.println("A Feb 30 2016 value is "+feb4Time.toStringMDYHM()); outfile.println("A Feb 30 2016 value is "+feb4Time.toStringMDYHM()); JCMTtimeMar29 laterTime = new JCMTtimeMar29(); System.out.println("Original later time value is "+laterTime.toStringMDYHM()); laterTime.setHourInMil(22); System.out.println("laterTime value is "+laterTime.toStringMDYHM() +" hour = 22"); laterTime.setYear(1989); System.out.println("laterTime value is "+laterTime.toStringMDYHM() +" year = 1989"); laterTime.setMonth(2); System.out.println("laterTime value is "+laterTime.toStringMDYHM() +" month = 2"); System.out.println(""); int inMo = -1; int inDay = -1; int inYr = -1; int inHr = -1; int inMin = -1; int inSec = -1; System.out.println("Please enter the expiration date and time of your coupon.\n"); System.out.println("The format should be month (space) day (space) year (space) hour (space) min"); System.out.println("like MM dd yyyy HH mm. The coupon year must be between 1970 and 2020. "); System.out.println("The hour should be in military time from 0 to 23. Invalid values will be "); System.out.println("replaced with the current time values. Example: For a coupon that expires one"); System.out.println("minute before midnight on Halloween the input would be 10 31 2017 23 59. \n"); System.out.print("Enter your coupon expiration date and time here: "); int ss = 0; int mm = 0; int dd = 0; int hh = 0; int mMM = 0; int yy = 0; String sp = " "; boolean inputCorrect = false; while (!inputCorrect) { try { mMM = keybd.nextInt(); dd = keybd.nextInt(); yy = keybd.nextInt(); hh = keybd.nextInt(); mm = keybd.nextInt(); ss = keybd.nextInt(); inputCorrect = true; System.out.println("Your inputs are: "+yy+sp+mMM+sp+dd+sp+hh+sp+mm+sp+ss); } catch (InputMismatchException im) { System.out.println("One or more of your input values could not be read.\n" + "Please re-enter your inputs as: yyyy MM dd HH mm ss "); } } //myTime.MONTH = 9; System.out.println("\nYour input was "+inMo +"/"+inDay+"/"+inYr+" at "+inHr+":"+inMin); outfile.println("\nYour input was "+inMo +"/"+inDay+"/"+inYr+" at "+inHr+":"+inMin); JCMTtimeMar29 expDate = new JCMTtimeMar29(inYr, inMo, inDay, inHr, inMin); System.out.println("Your error-checked input is "+expDate.toStringMDYHM()); outfile.println("Your error-checked input is "+expDate.toStringMDYHM()); */ /* System.out.println("\nNow we will check if this coupon is still valid and how many\n" + "days longer it is valid or that it has been expired. "); */ outfile.close(); /* Read char input, test char input p. 324 Use char to decide which action to take */ // read char input - ask user which action to take with input // a) check coupon date // b) print coupon date // c) input coupon value input.useDelimiter(""); System.out.println("Please indicate with a letter which choice you want:"); System.out.println(" a) check coupon date"); System.out.println(" b) print coupon date"); System.out.println(" c) input coupon value"); System.out.print("Your choice is: "); char choice = input.next().charAt(0); // test for digit, char, punc System.out.println("You chose "+choice); boolean digit = Character.isDigit(choice); System.out.println("input is digit? "+digit); boolean up = Character.isUpperCase(choice); System.out.println("input is uppercase? "+up); boolean alnum = Character.isLetterOrDigit(choice); System.out.println("input is letter or digit? "+alnum); // if char, use to choose an action if ((choice == 'a') || (choice == 'A')) { System.out.println("Do action a"); choice++; //DO NOT DO THIS } if (choice == 'b') { System.out.println("Choice is b"); } System.out.println("input is "+choice+" which increments to "+(++choice)); } /** * Method finds the name of the given month number * @param month - input number of month as 0 to 11 * @return a String with the full name of the month */ private static int numDaysInMonth(int month, int year) { // Month numbers need to be 0 (Jan) - 11 (Dec) switch ((month-1)) { case 0: return 31; case 1: { if (leapYear(year)) { return 29; } else { return 28; } } case 2: return 31; case 3: return 30; case 4: return 31; case 5: return 30; case 6: return 31; case 7: return 31; case 8: return 30; case 9: return 31; case 10: return 30; case 11: return 31; default: return 0; } } public static boolean leapYear(int year) { boolean leap = false; if ((year % 4) == 0) { if ((year % 100 ) != 0) // leap year b/c yr is div by 4 but not by 100 { leap = true; } else if ((year % 400) == 0) // leap year b/c yr is div by 400 { leap = true; } } return leap; } }