66import java .util .ArrayList ;
77import java .time .LocalDate ;
88import java .time .format .DateTimeFormatter ;
9+ import java .text .ParseException ;
10+ import java .text .SimpleDateFormat ;
11+ import java .util .Calendar ;
12+ import java .util .Locale ;
913
1014
1115public class Main {
@@ -56,30 +60,20 @@ public static Project createProject(Scanner sc, ArrayList<Project> oArrayList, H
5660 String pname = getPNameString (oArrayList , sc , hSet );
5761 Date pEndDate = new Date ();
5862 Date pStartDate = new Date ();
59- //please refactor later this is not optimal, there should be a recoursive solution
60- //try {
61- String start = "start" ;
62- String end = "end" ;
63- pStartDate = scanDate (sc , start );
64- pEndDate = scanDate (sc , end );
65- while ( ! pStartDate .isEarlierDate (pEndDate )){
66- System .out .println ("---------------------------------------------------------------" );
67- System .out .println ("there might have been an error let's check that again" );
68- System .out .println ("---------------------------------------------------------------" );
69- pStartDate = scanDate (sc , start );
70- pEndDate = scanDate (sc , end );
71- }
72- /*} catch (ArrayIndexOutOfBoundsException | InputMismatchException | NumberFormatException e) {
73- System.out.println("----------------------");
74- System.err.println("|inproper date format|");
75- System.out.println("----------------------");
76- System.out.println("| restarting creator |");
77- System.out.println("----------------------");
78- return createProject(sc, oArrayList, hSet);
79- }*/
63+ String start = "start" ;
64+ String end = "end" ;
65+ pStartDate = scanDate (sc , start );
66+ pEndDate = scanDate (sc , end );
67+ while ( ! pStartDate .isEarlierDate ( pEndDate ) ){
68+ System .out .println ("---------------------------------------------------------------" );
69+ System .out .println ("there might have been an error let's check that again" );
70+ System .out .println ("---------------------------------------------------------------" );
71+ pStartDate = scanDate ( sc , start );
72+ pEndDate = scanDate ( sc , end );
73+ }
8074 int pid = 0 ;
8175 if (oArrayList .size () == 0 ) pid = 0 ;
82- else pid = oArrayList .get (oArrayList .size ()- 1 ).getId () + 1 ;
76+ else pid = oArrayList .get ( oArrayList .size () - 1 ).getId () + 1 ;
8377 Project createProject = new Project (pid , pname , pStartDate , pEndDate );
8478 System .out .println ("---------------------------------------------------------------" );
8579 System .out .println ("you created project is" );
@@ -91,24 +85,35 @@ public static Project createProject(Scanner sc, ArrayList<Project> oArrayList, H
9185
9286 public static Date scanDate ( Scanner sc , String dateType ) throws ArrayIndexOutOfBoundsException {
9387 System .out .println ("so whats the " + dateType + " date" );
94- System .out .println ("please enter date like (YYYY/MM/DD) " );
88+ System .out .println ("please enter date like dd-MM-yyyy " );
9589 Date scDate = new Date ();
9690 try {
91+ String todayString = sc .nextLine ();
92+ String [] todayStringArr = todayString .split ("-" );
9793
98- String [] inDate = sc . nextLine (). split ( "/" );
94+ Calendar cals = parseTimestamp ( todayString );
9995
100- scDate = new Date (Integer .parseInt (inDate [0 ]), Integer .parseInt (inDate [1 ]), Integer .parseInt (inDate [2 ]) );
101-
102- // litte date validation, not in depth but the biggest mistakes will be stoped
103- if ( Integer .parseInt (inDate [1 ]) > 12 || Integer .parseInt (inDate [2 ]) > 31 || Integer .parseInt (inDate [1 ]) > 0 || Integer .parseInt (inDate [2 ]) > 0 ) return scanDate (sc , dateType );
96+ scDate = new Date (Integer .parseInt (todayStringArr [2 ]), Integer .parseInt (todayStringArr [1 ]),Integer .parseInt (todayStringArr [0 ])) ;
97+ //pDate.printDate();
10498
105- } catch ( ArrayIndexOutOfBoundsException | InputMismatchException | NumberFormatException e ) {
99+ } catch ( ArrayIndexOutOfBoundsException | InputMismatchException | NumberFormatException | ParseException e ) {
100+
106101 return scanDate (sc , dateType );
102+
107103 }
108104
109105 return scDate ;
110106 }
111-
107+
108+ public synchronized static Calendar parseTimestamp (String timestamp ) throws ParseException {
109+ SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy" , Locale .GERMAN );
110+ sdf .setLenient (false );
111+ java .util .Date d = sdf .parse (timestamp );
112+ Calendar cal = Calendar .getInstance ();
113+ cal .setTime (d );
114+ return cal ;
115+ }
116+
112117 public static String getPNameString (ArrayList <Project > oArrayList , Scanner sc , HashSet <String > hSet ){
113118 System .out .println ("please enter a project name" );
114119 String pname = sc .nextLine ();
0 commit comments