• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Report employee time clock cannot find corresponding dates in and out

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this assignment to build an employee time clock. By using a menu you can enter an employee, enter punch in and out, and report.
When I created the punch screen I wrote the in and out records to a file. Because there can be several employees punching in or out the file wrote each occurrence on different lines. Now I am attempting to write the report
which you enter an employee Id and loop through the file to find the in and out date and then calculate the time (hours worked). I loaded the file into an Arraylist but now I cannot figure out how to loop through find the In and Out date - calculate the hours worked and then move on to the next day. here is the format of the file - employee id, place holder I or O, date, time, day. and sample
111221111 i 3/2/14 5:10 PM Sunday
111221111 o 3/2/14 5:10 PM Sunday
111331111 i 3/2/14 5:25 PM Sunday
111331111 o 3/3/14 12:47 PM Monday
111221111 i 3/3/14 12:48 PM Monday
111221111 o 3/3/14 2:23 PM Monday
111441111 i 3/4/14 1:30 PM Tuesday
111441111 o 3/4/14 4:51 PM Tuesday
111441111 i 3/5/14 11:03 AM Wednesday
111221111 i 3/5/14 11:03 AM Wednesday
111331111 i 3/5/14 11:03 AM Wednesday
111551111 i 3/5/14 11:03 AM Wednesday
111551111 o 3/5/14 1:00 PM Wednesday
111221111 o 3/5/14 1:00 PM Wednesday
111331111 o 3/5/14 1:00 PM Wednesday
111441111 o 3/5/14 1:00 PM Wednesday
111441111 i 3/6/14 12:00 PM Thursday


here is my code from the main screen.
here is the piece in question
 
Marshal
Posts: 80212
423
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that method is much much too long, even after I deleted some of the blank lines which don't enhance legibility. I also added code tags, which usually make the code look a lot better, but unfortunately they here highlight your inconsistent indentation. Don't use /* … */ for commenting out. Use // on each line, the only situation where // on several lines is permissible.
You are going to have to divide that method up; it should make about ten methods.

This looks like code where you have dived in and written code before you worked out what you wanted to do. What is in your Employee class? Do you have any way of getting times in and times out? Do you have a TimePunch class, recording whether they came in or out and the time? Do you know how to create exactly n objects of a particular type? Do you know about classes which record date and time as a single value? Did you know that SimpleDateFormat can parse date and time together? Try one pair of time punch objects and work out how to print the difference between 3/8/14 8.00PM and 3/8/14 4.00AM. When you have done that you can consider putting several time punch objects into a list.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tanya Stolz wrote: I loaded the file into an Arraylist...


You did? All that code appears to be commented out (a very bad habit, IMO).

...but now I cannot figure out how to loop through find the In and Out date - calculate the hours worked and then move on to the next day.


Because you're trying to do too much at once. Campbell's absolutely right, that method looks like you've just done a mind-dump into Java code; and that will NEVER, EVER WORK.

Try and remember this rule: If you've written a method that is longer than 20 lines or so; chances are that it's wrong - or at the very least, could be written a lot better. It's not universal, but it's a great maxim to follow, because as soon as you start writing more, you should feel "uncomfortable".

Programming is about breaking down problems, not writing reams and reams of procedure; and the only way to do that is to understand WHAT you're doing, not HOW you're going to code it.

Campbell's already given you a good start. You should probably also read the StopCoding (←click) page.

When you've done that, look at your code and try to work out, in English, WHAT you need to do. It's just a guess, but I think you probably have a least half a dozen different "things" in that code - possibly more - and they should all be in separate methods.

HIH

Winston
 
Tanya Stolz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it is very messy all of that became because I was so stressed. I just started putting things in there just to see if and what was in the index. I should have removed all of that before submitting.
I do have two classes Employee for Id, first and last name. And TimeClock which holds the emp ID, place holder punch I or O, date, time, day. Should I be using the classes and not the file?
I did the entire project with no problems until here - I sent everything to a file in string format with tabs. I thought it would be easy to bring it back in and compare not the case. I tried to bring the string date back
in and convert to date and it converted but to no avail did not do what I expected.
I understand how to write simple things but when it came to this part I cannot wrap my mind around the How To -

I guess I really am not grasping the looping and comparing methodology...
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tanya Stolz wrote:I did the entire project with no problems until here - I sent everything to a file in string format with tabs. I thought it would be easy to bring it back in and compare not the case. I tried to bring the string date back
in and convert to date and it converted but to no avail did not do what I expected.
I understand how to write simple things but when it came to this part I cannot wrap my mind around the How To -


And that goes back to what we both have been saying: you need to understand 'WHAT to' before you can possibly understand 'HOW to'. You're too obsessed with how to code this, rather than analysing (and breaking down) the processes involved in doing it.

Suppose someone asked you to make a cabinet for them? Would you start by getting wood and simply banging in nails? Because that's what you're doing right now in Java terms. Before you write ONE LINE of Java code, you need to have a plan.

I know it is very messy all of that became because I was so stressed. I just started putting things in there just to see if and what was in the index. I should have removed all of that before submitting.


Actually, I disagree, because what you've showed us gives us a very good idea of what your problem actually is. The trouble is, you can't see it yet.

And don't worry: The reason we know is because we've ALL been there.

I do have two classes Employee for Id, first and last name. And TimeClock which holds the emp ID, place holder punch I or O, date, time, day. Should I be using the classes and not the file?


Possibly, but I really think you need to get back to basics. This is presumably an exercise for a class, so:
Do you have a description of the problem?

If so, show it to us. That will help us a lot to advise you.

I guess I really am not grasping the looping and comparing methodology...


Again, you're getting ahead of yourself - and from what you've written, I don't think that's the problem at all.
Your problem is that you've written too much code without a plan.

Jean-Paul Sartre was a great writer, but he'd have made a lousy programmer.

Winston
 
Tanya Stolz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the guidelines for this project
When we were given the project I created a blueprint and had a good idea of how it all would come together. I got all of it done without a hitch until the report section. I think I just became so overwhelmed with deadlines and additional homework that I just got lost. I am going to tear it apart and try adding methods to grab the date and time. I have done all the basics throughout the class and really found it easy until this... I thank you for the advice and will do my best - do you have any further advice since you can see what I need to accomplish?

Punch In/Out Screen Guidelines and Rubric

Overview
A company hires you to write a program to track hourly employee arrival and departure times from work. In essence, you are tasked to make an online time clock. The time clock shall keep a history of an employee’s hours for a two-week pay period. The application shall have the following functionality:
The ‘punch in/out screen’ shall:
• Save the punch in or punch out date and time of the employee to a memory object (or file).
o The date and time, ‘I’ for Punched In or ‘O’ for punched out along with the Employee ID shall be saved to a memory object called timeclock (or file called timeclock.txt).
o The recorded date for ‘punched in’ and ‘punched out’ shall be the method for matching corresponding records.
o The program shall test to ensure that there is a ‘Punched in’ record for the corresponding day before a ‘punched out’ record is saved. If none is found, prompt the user to enter a ‘punched in’ time.
o When the user has punched in or out, the program shall display a message indicating that the employee has punched in or out, the employee ID, date and time.
o In console based applications the screen shall display “Press any key to continue”
o In console based applications the program shall return to the main menu after a key is pressed when the “Press any key to continue” prompt is displayed.
o Optional functionality: Add the day of the week to the data saved.

A company hires you to write a program to track hourly employee arrival and departure times from work. In essence, you are tasked to make an online time clock. The time clock shall keep a history of an employee’s hours for a two-week pay period. The application shall have the following functionality:
The ‘report screen’ shall:
(Hint: If you are writing a console application, java.io.PrintWriter may be useful.)
• Allow the user to display a work history report for an individual or for all employees for the two weeks prior to the report request.
o The screen shall display a prompt to enter ‘I’ for individual employee report, ‘A’ for all employees report.
o If the selected value is ‘I’, prompt the user to enter the employee’s ID number.
 If ‘I’ is selected the display shall show the employee’s name and ID, list out each day worked in chronological order, the number of hours worked that day and a total number of hours worked in the two week period.
 The report shall prompt the user to re-enter an employee ID of it does not exist in the employee file.
 Optional Functionality: If the user inputs a nonexistent employee ID more than 3 times, prompt the user and then return to the main screen.
o If the selected value is ‘A’, output the information to the console for the past two weeks.
o The end of the report shall display “Press any key to continue” to return to the main menu.
o Optional Functionality: Allow the user to print the report to a printer.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tanya Stolz wrote:I am going to tear it apart and try adding methods to grab the date and time. I have done all the basics throughout the class and really found it easy until this...


In which case that is precisely what you SHOULDN'T do - at least not until you have taken a copy of what you have NOW.

You clearly have a lot of good work associated with this. You don't need to bin it all. You just need to look it over again. Thoroughly. And you need to analyse it.

Existential programming works just so far - and unfortunately, for smart people, that "far" can be a lot further than it should be - but there comes a point at which the weight of what you're trying to achieve simply overwhelms you, because you're thinking about it only as a procedure, and not as a rational plan. Either that, or your "plan" is so cast in stone that you can't work out how to modify it.

Need to add a couple of display shelves, or a mirror, to the top of your "cabinet" to turn it into a dresser? Change the plan. And don't write a line of extra code until your plan conforms to WHAT you need to do. It's probably not what you want to hear, because you're just dying to get THAT CODE working, but it's absolutely essential. Otherwise, you're just banging nails into your nice, solid cabinet.

Give me a bit, and I'll look over your requirements to see if I can see something obvious that you're missing.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tanya Stolz wrote:Here are the guidelines for this project...


OK. Now we've got something to work with (sorry, but I had other things to do yesterday).

One thing that you can do with requirements is what I call "lexical analysis". It sounds horrendous, but it's actually very simple:

Go through the requirements (several times if necessary) and pick out all the nouns and verbs. Nouns will generally equate to "things", and so probably classes or attributes; verbs will equate to "actions", and so probably methods. It's by no means foolproof or complete, but it generally gives you a good "first cut" at the sort of things your system (or program) is going to have to manage.
It also takes a bit of practise, and the chances are that you will write down too much to begin with, but with a bit of time you will learn to spot "keywords" in your requirements. You also need to get used to nouns like "arrival" which actually denote an action (arrive). The main thing is to get rid of all the "woffle".

My first cut at your paragraph above might be something like:
Company (noun), Employee (noun), ID (noun), hourly (? probably noun), arrival (noun, but probably an "action" (see above)), departure (action), Clock/TimeClock (noun), punch in/punch out (verbs), Date (noun), Time(noun), save (verb), Day (noun), Day of week (noun), User (noun; probably the same as Employee), Record (noun).

Do you see how this helps? You have an instant list of things that might be classes (Employee?). And also of some methods (punchIn()?) you might want to put in those classes.

As I say, it's not by any means complete, but it does give you a place to start, and now you can start to think about the sorts of things that are like to happen in your system:
  • What happens when an Employee comes in? What classes will be involved? What actions will happen?
  • What happens when an Employee punches out? Same as before, but note the check that was stipulated in your requirements: "The program shall [...] ensure that there is a ‘Punched in’ record for the corresponding day before a ‘punched out’ record is saved.". That might make you think a bit further: What happens if an Employee punches IN twice without an intervening "OUT"?
  • ...
  • Note that all this process is incremental, and it requires you think to think about WHAT is happening, not HOW you're going to code it. And it requires a lot of paper and pencils.

    A few things I can note that you don't appear to have:
  • a Company class - what do you think that might contain?
  • a Record class.
  • punchIn()/punchOut() methods - or arrive()/depart() - or possibly, BOTH.
  • a class that holds ALL your Records (TimeLog?) - alternatively, you might want to hold those by Employee (might be easier for your "duplicate" IN/OUT check).

  • You have lots of decisions to make, and unfortunately, putting all your logic into one method is not the way to do it.

    HIH

    Winston
     
    Tanya Stolz
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you so much for all of your help -
    I am off to a good start
    I created several methods to get the In Date, Out Date
    Compare the two dates
    Get the hours worked
    Get the total (work in progress)

    I can display ONLY one date - whichever is the latest date.
    Even though there are 2 records for the employee in the timclocks file when I want to display it is only showing me the latest.

    I am pretty sure I need to maybe make arrays but not sure how to go about doing so - this particular function I am very weak at understanding.
    Here is the updated main Employee Time Clock and the TimeClock class (where I am doing all the methods) it works fine but only is displaying one record. And the file I store the punches timeclocks.txt
    111221111 i 3/10/14 10:23 AM Monday
    111331111 i 3/10/14 10:23 AM Monday
    111441111 i 3/10/14 10:23 AM Monday
    111551111 i 3/10/14 10:23 AM Monday
    111661111 i 3/10/14 10:23 AM Monday
    111771111 i 3/10/14 10:24 AM Monday
    111221111 o 3/10/14 12:35 PM Monday
    111331111 o 3/10/14 12:35 PM Monday
    111441111 o 3/10/14 12:35 PM Monday
    111551111 o 3/10/14 12:35 PM Monday
    111661111 o 3/10/14 12:35 PM Monday
    111771111 o 3/10/14 12:35 PM Monday
    111221111 i 3/11/14 8:50 AM Tuesday
    111331111 i 3/11/14 8:50 AM Tuesday
    111441111 i 3/11/14 8:50 AM Tuesday
    111551111 i 3/11/14 8:50 AM Tuesday
    111771111 i 3/11/14 8:50 AM Tuesday
    111221111 o 3/11/14 11:20 AM Tuesday
    111331111 o 3/11/14 11:20 AM Tuesday
    111441111 o 3/11/14 11:20 AM Tuesday
    111551111 o 3/11/14 11:20 AM Tuesday
    111661111 o 3/11/14 11:21 AM Tuesday
    111771111 o 3/11/14 11:21 AM Tuesday






    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;

    import java.text.DateFormat;
    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Scanner;
    import java.util.*;


    public class EmployeeTimeClock
    {
    // declare class variables
    private static EmployeeDAO employeeDAO = null;
    private static TimeClockDAO timeclockDAO = null;
    private static Scanner sc = null;
    public static void main(String[] args) throws IOException, ParseException
    {
    // set the class variables
    employeeDAO = DAOFactory.getEmployeeDAO();
    timeclockDAO = DAOFactory.getTimeClockDAO();
    sc = new Scanner(System.in);

    // display the command menu
    displayMenu();

    // perform 1 or more actions
    String action = "";
    while (!action.equalsIgnoreCase("d"))
    {
    // get the input from the user
    action = Validator.getString(sc,
    "Enter a choice: ");
    System.out.println();


    if (action.equalsIgnoreCase("a"))
    addEmployee();
    /* System.out.println("Employee");*/
    else if (action.equalsIgnoreCase("b"))
    addTimeClocks();
    /* System.out.println("PunchIn Out");*/
    else if (action.equalsIgnoreCase("c"))
    displayPunches();
    /* System.out.println("report");*/
    else if (action.equalsIgnoreCase("d"))
    System.out.println("Exit.\n");
    else
    System.out.println("Error! Not a valid command.\n");
    }

    }
    public static void displayMenu()
    {
    System.out.println("Main Menu");
    System.out.println("a - Add New Employee");
    System.out.println("b - Punch In-Out");
    System.out.println("c - Report");
    System.out.println("d - Exit this application\n");
    }

    public static void addEmployee()
    { String choice = "y";
    while (!choice.equalsIgnoreCase("n"))
    {
    String employeeId = Validator.getString(
    sc, "Enter employee Id(SS# no dashes): ");
    if (employeeId.length() != 9)
    {System.out.println("Employee Id must be 9. Try Again ");
    continue; }
    String firstName = Validator.getLine(
    sc, "Enter First Name: ");
    if ((firstName.length() == 0) || (!firstName.matches("[A-Za-z]+")) )
    {System.out.println("Name must be Alpha and "
    + "cannot be blank-Try Again.");
    continue; }
    String lastName = Validator.getLine(
    sc, "Enter Last Name: ");
    if ((lastName.length() == 0) || (!lastName.matches("[A-Za-z]+")) )
    {System.out.println("Name must be Alpha and "
    + "cannot be blank-Try Again.");
    continue;
    }

    Employee employee = new Employee();
    employee.setEmployeeId(employeeId);
    employee.setFirstName(firstName);
    employee.setLastName(lastName);
    employeeDAO.addEmployees(employee);

    System.out.println();
    System.out.println(
    " Employee has been added.\n");

    // see if the user wants to enter more employees
    choice = Validator.getString(sc, "Enter another Employee? (y/n): ");
    System.out.println();
    }
    /*while choice y*/
    displayMenu();
    } /*addemployee*/


    public static void addTimeClocks() throws FileNotFoundException, IOException
    { boolean isValid = false;
    while (isValid == false)
    {
    String employeeId = Validator.getString(
    sc, "Enter employee Id(SS# no dashes): ");
    Employee p = employeeDAO.getEmployee(employeeId);
    try
    {
    System.out.println("Name:" + p.getFirstName() + " " + p.getLastName());
    isValid = true;
    }
    catch(NullPointerException npe)
    {System.out.println("EmployeeId is not Valid. Try Again");
    isValid = false;
    continue;
    }

    String punchIn = Validator.getLine(sc, "Enter I if you would like to clock in?:"
    + "Enter O if you would like to clock out?: ");

    String[] strDays = new String[]{
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
    };

    Calendar now = Calendar.getInstance();
    Date d = now.getTime();
    DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
    DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);
    if (punchIn.equalsIgnoreCase("I") )
    {
    String dateIn = df1.format(d);
    String timeIn = df2.format(d);
    String dayIn = (strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);
    TimeClock timeclock = new TimeClock();
    timeclock.setEmployeeId(employeeId);
    timeclock.setPunchIn(punchIn);
    timeclock.setDateIn(dateIn);
    timeclock.setTimeIn(timeIn);
    timeclock.setDayIn(dayIn);
    timeclockDAO.addTimeClocks(timeclock);
    }

    if (punchIn.equalsIgnoreCase("O"))
    { String dataFileName = "timeclocks.txt";
    BufferedReader bReader = new BufferedReader(
    new FileReader(dataFileName));
    String line;
    TimeClock b = timeclockDAO.getTimeClock(employeeId);

    String indate = ("i" + df1.format(d));
    if ((indate==null)
    || (b ==null)) {
    System.out.println( "Error! YOU MUST PUNCH IN BEFORE OUT \n" );
    sc.nextLine();
    displayMenu();
    continue;
    }
    else if(punchIn.equalsIgnoreCase("O"))
    {
    String dateIn = df1.format(d);
    String timeIn = df2.format(d);
    String dayIn = (strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);
    TimeClock timeclock = new TimeClock();
    timeclock.setEmployeeId(employeeId);
    timeclock.setPunchIn(punchIn);
    timeclock.setDateIn(dateIn);
    timeclock.setTimeIn(timeIn);
    timeclock.setDayIn(dayIn);
    timeclockDAO.addTimeClocks(timeclock);
    }

    }



    System.out.println();
    String dateIn = df1.format(d);
    String timeIn = df2.format(d);

    System.out.println(" Punch Complete. " + p.getEmployeeId() + " " +
    dateIn + " " + timeIn + " " + "PRESS ENTER TO CONTINUE");
    Scanner keyboard = new Scanner(System.in);
    keyboard.nextLine();
    displayMenu();

    }
    } /*addtimeclock*/


    public static void displayPunches()
    throws FileNotFoundException, IOException, ParseException
    { boolean isValid = false;
    String choice = "y";
    String emp = Validator.getLine(sc, "Enter I -Individual or A -All ");

    if (emp.equalsIgnoreCase("A"))
    {
    String dataFileName = "timeclocks.txt";
    BufferedReader bReader = new BufferedReader(
    new FileReader(dataFileName));
    String line;
    while ((line = bReader.readLine()) != null)
    {
    // create an array of string objs
    String datavalue[] = line.split("\t");
    String value1 = datavalue[0];
    String value2 = datavalue[1];
    String value3 = datavalue[2];
    String value4 = datavalue[3];
    String value5 = datavalue[4];
    Arrays.sort(datavalue);

    Employee p = employeeDAO.getEmployee(value1);
    {

    System.out.println("Id\t\tName\t\tDateTime\t\tDay\t\tPunch");

    String s = value1 + "\t " + p.getFirstName() + p.getLastName()
    + "\t" + value3 + "\t" + value4 + "\t"
    + value5 + "\t" + "\t" + value2;
    System.out.println(s);
    System.out.println();
    }

    } /*while read*/

    bReader.close();
    } /*if A*/


    while (!choice.equalsIgnoreCase("n"))
    {
    // get the input from the user
    if (emp.equalsIgnoreCase("I"))
    { String employeeId = Validator.getString(
    sc, "Enter employee Id(SS# no dashes): ");
    try
    { Employee p = employeeDAO.getEmployee(employeeId);
    System.out.println("Name:" + p.getFirstName() + " " + p.getLastName());
    isValid = true;
    }
    catch(NullPointerException npe)
    {System.out.println("EmployeeId is not Valid. Try Again");
    isValid = false;
    continue;
    }
    Employee p = employeeDAO.getEmployee(employeeId);
    TimeClock b = timeclockDAO.getTimeClock(employeeId);
    String dateI = TimeClock.getPunchDate(employeeId, emp);
    String dateO = TimeClock.getPunchDate(employeeId, emp);
    String punchIn = b.getPunchIn();
    String dateIn = b.getDateIn();
    String timeIn =b.getTimeIn();
    String dayIn = b.getDayIn();
    String hoursWorked = TimeClock.getHours(employeeId, emp);
    int totalWorked = TimeClock.getTotal(employeeId, emp);
    int hours = (totalWorked /3600);
    int rhours = (int)(totalWorked%3600);
    int minutes = (rhours / 60);
    String totalTime = hours + ":" + minutes;
    /* long time = totalWorked / 1000;
    String seconds = Integer.toString((int)(time % 60));
    String minutes = Integer.toString((int)((time % 3600) / 60));
    String hours = Integer.toString((int)(time / 3600));
    String totalTime = hours + ":" + minutes + ":" + seconds; */



    //get invoice object
    //CREATE INVOICE OBJECT
    TimeClock timeclock = new TimeClock(employeeId, punchIn,
    dateIn, timeIn, dayIn, dateI,
    dateO, hoursWorked, totalWorked);
    String m = timeclock.getTimeClock();
    System.out.println(m);

    System.out.println(
    "\n\t\t\t\tTotal Hours:\t" +
    totalTime + "\n");
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();

    } /*if selection I*/
    } /*choice*/


    System.out.println();
    System.out.println(" Report Complete. PRESS ENTER KEY TO CONTINUE");
    Scanner keyboard = new Scanner(System.in);
    keyboard.nextLine();
    displayMenu();


    } /*report*/


    } /*end entire timeclock*/



    /** Tanya Dinsmore
    * IT 501
    * Project
    * Employee Time Clock
    *
    *
    */


    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.NumberFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import java.util.Comparator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class TimeClock
    {

    String employeeId;
    String punchIn;
    String dateIn;
    String dayIn;
    String timeIn;
    String dateI;
    String dateO;
    String hoursWorked;
    int totalWorked;
    private ArrayList<TimeClock> timeclocks;
    public TimeClock()
    {

    this.employeeId = "";
    this.punchIn = "";
    this.dateIn = "";
    this.dayIn = "";
    this.timeIn = "";
    this.dateI = "";
    this.dateO = "";
    this.hoursWorked = "";
    this.totalWorked = 0;
    timeclocks = new ArrayList<>();
    }



    public TimeClock( String employeeId, String punchIn,
    String dateIn, String timeIn, String dayIn,String dateI,
    String dateO, String hoursWorked, int totalWorked
    )
    {

    this.employeeId = employeeId;
    this.punchIn = punchIn;
    this.dateIn = dateIn;
    this.dayIn = dayIn;
    this.timeIn = timeIn;
    this.dateI = dateI;
    this.dateO = dateO;
    this.hoursWorked = hoursWorked;
    this.totalWorked = totalWorked;


    }
    //THE SET AND GET ACCESSORS FOR THE VARIABLES

    public void setEmployeeId(String employeeId)
    { if (employeeId == null)
    this.employeeId = "(Empty)";
    else
    this.employeeId = employeeId;
    }

    public String getEmployeeId()
    {
    return employeeId;
    } public void setPunchIn(String punchIn)
    {
    this.punchIn = punchIn;
    }
    public String getPunchIn()
    {
    return punchIn;
    }
    public void setDateIn(String dateIn)
    {
    this.dateIn = dateIn;
    }
    public String getDateIn()
    {
    return dateIn;
    }
    public void setDayIn(String dayIn)
    {
    this.dayIn = dayIn;
    }
    public String getDayIn()
    {
    return dayIn;
    }
    public void setTimeIn(String timeIn)
    {
    this.timeIn = timeIn;
    }
    public String getTimeIn()
    {
    return timeIn;
    }
    public void setDateI(String dateI)
    {
    this.dateI = dateI;
    }
    public String getDateI()
    {
    return dateI;
    }
    public void setDateO(String dateO)
    {
    this.dateO = dateO;
    }
    public String getDateO()
    {
    return dateO;
    }
    public void sethoursWorked(String hoursWorked)
    {
    this.hoursWorked = hoursWorked;
    }
    public String getHoursWorked()
    {
    return hoursWorked;
    }
    public void setTotalWorked(int totalWorked)
    {
    this.totalWorked = totalWorked;
    }
    public int getTotalWorked()
    {
    return totalWorked;
    }
    public boolean equals(Object object)
    {
    if (object instanceof TimeClock)
    {
    TimeClock timeclock2 = (TimeClock) object;
    if
    (
    employeeId.equals(timeclock2.getEmployeeId()) &&
    punchIn.equals(timeclock2.getPunchIn()) &&
    dateIn.equals(timeclock2.getDateIn()) &&
    timeIn.equals(timeclock2.getTimeIn()) &&
    dayIn.equals(timeclock2.getDayIn())

    )
    return true;
    }
    return false;

    }



    public String toString()
    {
    return "Employee Id:" + employeeId + "\t" +
    "PunchIn:" + punchIn + "\t" +
    "Date In:" + dateIn + "\t" +
    "Time In:" + timeIn + "\t" +
    "Day In:" + dayIn ;

    }



    public static String getPunchDate(String employeeId, String emp) throws IOException
    {
    String datei= "";
    String dataFileName = "timeclocks.txt";
    BufferedReader bReader = new BufferedReader(
    new FileReader(dataFileName));
    String line;
    while ((line = bReader.readLine()) != null)
    {

    String datavalue[] = line.split("\t");
    List<String> list2 = new ArrayList<String>();
    list2.add(datavalue[0]);
    list2.add(datavalue[1]);
    list2.add(datavalue[2]);
    list2.add(datavalue[3]);
    list2.add(datavalue[4]);

    String value1 = datavalue[0];
    String value2 = datavalue[1];
    String value3 = datavalue[2];
    String value4 = datavalue[3];
    String value5 = datavalue[4];

    if (employeeId.equals(list2.get(0)) &&
    (list2.get(1).equalsIgnoreCase("i")))

    { datei = value3 + " " + value4;

    }
    }
    return (datei);

    }

    public static String getPunchDate2(String employeeId, String emp) throws IOException
    { String dateo= "";
    String dataFileName = "timeclocks.txt";
    BufferedReader bReader = new BufferedReader(
    new FileReader(dataFileName));
    String line;
    while ((line = bReader.readLine()) != null)
    {

    String datavalue[] = line.split("\t");
    List<String> list2 = new ArrayList<String>();
    list2.add(datavalue[0]);
    list2.add(datavalue[1]);
    list2.add(datavalue[2]);
    list2.add(datavalue[3]);
    list2.add(datavalue[4]);

    String value1 = datavalue[0];
    String value2 = datavalue[1];
    String value3 = datavalue[2];
    String value4 = datavalue[3];
    String value5 = datavalue[4];

    if (employeeId.equals(list2.get(0)) &&
    (list2.get(1).equalsIgnoreCase("o")))

    {
    dateo = value3 + " " + value4;

    }

    }
    return (dateo);
    }




    public static String getHours(String employeeId, String emp ) throws IOException
    { String hourWorked = "";
    try {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
    String datei1 = (TimeClock.getPunchDate(employeeId, emp));
    String dateo2 = (TimeClock.getPunchDate2(employeeId, emp));
    Date date1 = sdf.parse(datei1);
    Date date2 = sdf.parse(dateo2);

    if(date1.compareTo(date2)==0){

    SimpleDateFormat formatter =
    new SimpleDateFormat("MM/dd/yy HH:mm");
    String dateInString = TimeClock.getPunchDate(employeeId, emp);
    String dateInString2 = TimeClock.getPunchDate2(employeeId, emp);
    Date dated = null;
    Date timed = null;
    dated = formatter.parse(dateInString);
    timed = formatter.parse(dateInString2);
    int diff = (int) (timed.getTime() - dated.getTime());
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000)% 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;

    hourWorked = (diffHours + ":" + diffMinutes);

    }

    } catch (ParseException e) {
    e.printStackTrace();

    }

    return (hourWorked);

    }
    public static int getTotal(String employeeId, String emp) throws IOException, ParseException
    { String h=( TimeClock.getHours(employeeId, emp));
    String[] h1=h.split(":");
    int hour=Integer.parseInt(h1[0]);
    int minute=Integer.parseInt(h1[1]);
    int htotal = (60 * minute) + (3600 * hour);
    int total = 0;
    total += htotal;
    return total;

    }


    public ArrayList getAll() throws IOException, ParseException
    {
    ArrayList timeclocks = new ArrayList<>();
    timeclocks.add(TimeClock.getPunchDate(employeeId, dateIn));
    timeclocks.add(TimeClock.getHours(employeeId, dateIn));
    timeclocks.add(TimeClock.getTotal(employeeId, dateIn));
    return timeclocks;
    }

    public String getTimeClock() throws ParseException, IOException
    {
    // format the values

    String semployeeId = employeeId;
    SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yy hh:mm");
    String dateworked = TimeClock.getPunchDate(employeeId, dateIn);
    Date worked = null;
    worked = df1.parse(dateworked);

    SimpleDateFormat sdf1 = new SimpleDateFormat(" E MM/dd/yy");
    sdf1.format(worked);
    String shoursworked = (hoursWorked);
    int stotalWorked = (int) (totalWorked);
    ArrayList timeclocks = new ArrayList();
    timeclocks.add(sdf1.format(worked));
    timeclocks.add(hoursWorked);
    timeclocks.add(totalWorked);
    System.out.println("Id\t\tDate\t\t\tDailyTotal");
    System.out.println("---------\t-----------\t\t-----");
    String message = semployeeId
    + "\t" + sdf1.format(worked)
    + "\t" + "\t" + hoursWorked + timeclocks;



    return message;
    }


    }



     
    Campbell Ritchie
    Marshal
    Posts: 80212
    423
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    O good grief!
    Do you really expect people to read all those hundreds of lines of code, particularly without code tags? If you don't isolate the problem a bit, nobody else will.
     
    What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic