authorauthorversion
package com.markcrocker.thoughtworks;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Dates {
private int debugLevel = 0;
paramargsparamiexceptionreturn
private static MyDate parseDates(String[] args, int i) throws BadDateException {
int year;
MyDate date = null;
if ((args.length - i > 2) &&
(args[i].endsWith(",")) &&
(args[i+1].endsWith(",")) ) {
int month = Integer.parseInt(args[i].substring(0,args[i].length()-1));
int day = Integer.parseInt(args[++i].substring(0,args[i].length()-1));
if (args[i+1].endsWith(",")) { year = Integer.parseInt(args[++i].substring(0,args[i].length()-1)); } else {
year = Integer.parseInt(args[++i].substring(0,args[i].length())); }
date = new MyDate(year-1900,month,day);
}
return date;
}
paramfileNameexception
private static void parseFile(String fileName) throws FileNotFoundException,
IOException,
BadDateException {
int debugLevel = 0; StringTokenizer st;
BufferedReader in = new BufferedReader( new FileReader(fileName));
String line = new String();
int lineNo = 0;
while((line = in.readLine())!= null) {
lineNo++;
if (debugLevel > 1) { System.out.println ("Line " + lineNo + " is: " + line); }
st = new StringTokenizer(line);
String token = st.nextToken();
int startMonth = Integer.parseInt(token.substring(0,token.length()-1));
token = st.nextToken();
int startDay = Integer.parseInt(token.substring(0,token.length()-1));
token = st.nextToken();
int startYear = Integer.parseInt(token.substring(0,token.length()-1));
token = st.nextToken();
int endMonth = Integer.parseInt(token.substring(0,token.length()-1));
token = st.nextToken();
int endDay = Integer.parseInt(token.substring(0,token.length()-1));
token = st.nextToken();
int endYear = Integer.parseInt(token);
MyDate startDate = new MyDate(startYear-1900,startMonth,startDay);
MyDate endDate = new MyDate(endYear-1900,endMonth,endDay);
System.out.println( endDate.between(startDate));
}
in.close();
}
private static void syntaxMessage() {
System.out.println("Syntax:\n" +
" Dates MM, DD, YYYY, mm, dd, yyyy\n" +
" Dates -f inputFile\n" +
" Dates [-t|--test|-e|--exceptions|-h|--help]\n" +
" Where: MM and mm represent month numbers from 1-12.\n" +
" DD and dd represent day of the month numbers\n" +
" from 1-28, 29, 30 or 31 depending on the month.\n" +
" YYYY and yyyy represent year numbers from 1900-2099, inclusive.\n" +
" The first case produces the number of days between the two dates.\n" +
" The second case reads dates in the same format as in case on from\n" +
" file inputFile.\n" +
" The third case produces test output, exception test output or this message.\n" +
" These switches may be used in combination with each other and multiple times.\n");
}
public static void main(String[] args) throws FileNotFoundException,
IOException,
BadDateException {
int debugLevel = 0;
if (args.length < 1) {
syntaxMessage();
} else {
for (int i = 0; i < args.length; i++) { if ( args[i].equals("-t") ||
args[i].equals("--test")) {
if (debugLevel > 1) { System.out.println("Test mode flag detected"); }
System.out.println("Tests passed = " + TestMyDate.testStandardDates());
} else if ( args[i].equals("-e") ||
args[i].equals("--exceptions")) {
if (debugLevel > 1) { System.out.println("Exception test mode flag detected"); }
TestMyDate.testDateExceptions();
} else if ( args[i].equals("-h") ||
args[i].equals("--help")) {
if (debugLevel > 1) { System.out.println("Help mode flag detected"); }
syntaxMessage();
} else if ( (args.length - i > 1) &&
(args[i].equals("-f")) ) {
if (debugLevel > 1) { System.out.println("File input from file " + args[i+1] + " requested"); }
parseFile(args[++i]);
} else {
MyDate startDate;
MyDate endDate;
if (((startDate = parseDates(args, i)) != null) &&
((endDate = parseDates(args, i+3)) != null)) {
System.out.println( endDate.between(startDate));
i += 5;
} else { syntaxMessage();
break;
}
}
}
}
}
}