This is the entire Java Code of Version 8 of our Program. Its fully tested and works to our satisfaction.
----------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
/**
* This is a program to track the time workers have been working
*
* @author (Jekabs Stikans, Rihards Sillins, Duncan Hamilton, Ron Schonberg)
* @version (21.09.2012)
*/
public class Clock extends JFrame implements ActionListener
{
protected Container ca;
private SimpleDateFormat completeTime, hourMinute;
JLabel lblTitle;
JButton btnAdmin;
JButton[] workers = new JButton[10];
String [] cbContent;
protected Employee[] employees;
protected Controller controll;
public static void main(String [] args)
{
Clock thisClass = new Clock();
}
public Clock()
{
controll = new Controller();
employees = controll.getEmployees();
for(int i = 0; i < workers.length; i++)
{
workers[i] = new JButton();
}
cbContent = new String[employees.length]; // really just defining the size here (all for admin panel)
completeTime = new SimpleDateFormat("HH:mm:ss");
hourMinute = new SimpleDateFormat("mm:ss");
screen();
}
public void screen()
{
//set the dimension of the screen and other identities
setSize(600,600);
setVisible(true);
//this sets the game to the middle of the screen
setLocationRelativeTo(null);
//need to define a Container to place objects onto the frame
ca=getContentPane();
ca.setSize(600,600);
ca.setBackground(Color.white);
ca.setLayout(null);
for(int i = 0; i < employees.length; i++)
{
workers[i] = new JButton(employees[i].getName() + " is out");
workers[i].setBounds(100,60 + 50 * i, 200, 40);
workers[i].addActionListener(this);
workers[i].setBackground(Color.red);
ca.add(workers[i]);
}
//add the Main title
lblTitle = new JLabel("Time Catcher");
lblTitle.setFont(new Font("Serif", Font.BOLD, 30));
lblTitle.setForeground(Color.black);
lblTitle.setBounds(180,0,250,50);
ca.add(lblTitle);
btnAdmin = new JButton("Admin Panel");
btnAdmin.setBounds(10,10, 130, 40);
btnAdmin.addActionListener(this);
btnAdmin.setBackground(Color.green);
ca.add(btnAdmin);
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < workers.length; i++)
{
if (e.getSource() == workers[i])
{
if(employees[i].getStatus() == false)
{
controll.checkEmployeeIn(i);
workers[i].setBackground(Color.green);
workers[i].setBounds(300, 60 + 50 * i, 200, 40);
workers[i].setText(employees[i].getName() + " is in");
}else
{
controll.checkEmployeeOut(i);
workers[i].setBackground(Color.red);
workers[i].setBounds(100, 60 + 50 * i, 200, 40);
workers[i].setText(employees[i].getName() + " is out");
}
}
}
if (e.getSource() == btnAdmin)
{
AdminGUI adminWindow = new AdminGUI();
}
}
}
------------------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import javax.swing.Popup;
import javax.swing.JTextArea;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import java.awt.image.*;
import java.util.Random;
import java.util.Calendar;
import java.io.*;
/**
*
* @author (Ron Schonberg, Jekabs Stikans, Rihards Sillins, Duncan Hamilton)
* @version (21.09.2012)
*/
public class AdminGUI extends JFrame implements ActionListener
{
protected Container ca;
protected static String LOGIN_FAIL = "Your login has failed. Please try again.";
protected static String THREE_TIMES_LOGIN_FAIL = "Your login has failed. Your account is locked.";
JLabel lblTitle;
JLabel lblHowClose;
JButton btnLogin;
JButton btnSave;
JLabel lblName;
JLabel lblPass;
JTextField txtName;
JPasswordField txtPass;
JTextArea txtTimeArea;
protected JScrollPane JSPTimeArea;
JTextArea txtTotalArea;
protected JScrollPane JSPTotalArea;
JComboBox cBemployees;
public int loginAttempt = 0;
public int employeeSize = 0;
protected String[] cbContent;
protected Controller controll;
protected Employee[] employees;
public AdminGUI()
{
controll = new Controller();
employees = controll.getEmployees();
employeeSize = employees.length;
cbContent = new String[employeeSize];
screen();
}
public void screen()
{
setSize(600,600);
setVisible(true);
setLocationRelativeTo(null);
ca=getContentPane();
ca.setSize(600,600);
ca.setBackground(Color.white);
ca.setLayout(null);
lblTitle = new JLabel("Admin Panel");
lblTitle.setFont(new Font("Serif", Font.BOLD, 30));
lblTitle.setForeground(Color.black);
lblTitle.setBounds(180,0,250,50);
ca.add(lblTitle);
lblHowClose = new JLabel("Close window to exit Menu");
lblHowClose.setFont(new Font("Serif", Font.BOLD, 10));
lblHowClose.setForeground(Color.black);
lblHowClose.setBounds(450,0,150,50);
ca.add(lblHowClose);
lblName = new JLabel("User name:");
lblName.setBounds(30, 150, 200, 50);
ca.add(lblName);
txtName = new JTextField("");
txtName.setBounds(150, 160, 200, 30);
ca.add(txtName);
lblPass = new JLabel("Password:");
lblPass.setBounds(30, 200, 200, 50);
ca.add(lblPass);
txtPass = new JPasswordField("");
txtPass.setBounds(150, 210, 200, 30);
ca.add(txtPass);
txtTimeArea = new JTextArea("");
txtTimeArea.setRows(1);
txtTimeArea.setColumns(2);
txtTimeArea.setLineWrap(true);
txtTimeArea.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.gray));
txtTimeArea.setBounds(10, 100, 320, 400);
txtTimeArea.setVisible(false);
ca.add(txtTimeArea);
JSPTimeArea = new JScrollPane(txtTimeArea);
JSPTimeArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSPTimeArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JSPTimeArea.setBounds(10, 100, 320, 400);
JSPTimeArea.setVisible(false);
ca.add(JSPTimeArea);
txtTotalArea = new JTextArea("");
txtTotalArea.setRows(1);
txtTotalArea.setColumns(2);
txtTotalArea.setLineWrap(true);
txtTotalArea.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.gray));
txtTotalArea.setBounds(340, 100, 100, 400);
txtTotalArea.setVisible(false);
ca.add(txtTotalArea);
JSPTotalArea = new JScrollPane(txtTotalArea);
JSPTotalArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSPTotalArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JSPTotalArea.setBounds(340, 100, 100, 400);
JSPTotalArea.setVisible(false);
ca.add(JSPTotalArea);
btnLogin = new JButton("Login");
btnLogin.setBounds(150,260, 200, 40);
btnLogin.addActionListener(this);
btnLogin.setBackground(Color.green);
ca.add(btnLogin);
btnSave = new JButton("Save");
btnSave.setBounds(340, 60, 100, 30);
btnSave.addActionListener(this);
btnSave.setBackground(Color.green);
btnSave.setVisible(false);
ca.add(btnSave);
for(int i = 0; i < employees.length ; i++)
{
cbContent[i] = employees[i].getName();
}
cBemployees = new JComboBox(cbContent);
cBemployees.setBounds(10, 60, 130, 30);
cBemployees.setSelectedIndex(0);
cBemployees.addActionListener(this);
cBemployees.setVisible(false);
ca.add(cBemployees);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnLogin)
{
if(txtName.getText().equals("test") && txtPass.getText().equals("test"))
{
setupAdminScreen();
}
else
{
loginAttempt++;
txtName.setText("");
txtPass.setText("");
if(loginAttempt > 2) // if entered wrong 3 times
{
txtName.setEnabled(false);
txtPass.setEnabled(false);
JOptionPane.showMessageDialog(this, THREE_TIMES_LOGIN_FAIL, "", JOptionPane.WARNING_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this, LOGIN_FAIL, "", JOptionPane.WARNING_MESSAGE);
}
}
}
if (e.getSource() == btnSave)
{
controll.editFile(cbContent[cBemployees.getSelectedIndex()]+"\n"+txtTimeArea.getText(), cBemployees.getSelectedIndex());
}
for(int j = 0; j < cbContent.length; j++)
{
if(cBemployees.getSelectedItem() == cbContent[j])
{
String[] data = controll.getUserLog(j);
txtTimeArea.setText(data[0]);
txtTotalArea.setText(data[1]);
}
}
}
public void setupAdminScreen()
{ //hide the login stuff
txtName.setVisible(false);
lblName.setVisible(false);
lblPass.setVisible(false);
txtPass.setVisible(false);
btnLogin.setVisible(false);
//show the admin stuff
cBemployees.setVisible(true);
txtTimeArea.setVisible(true);
btnSave.setVisible(true);
JSPTimeArea.setVisible(true);
txtTotalArea.setVisible(true);
JSPTotalArea.setVisible(true);
}
}
----------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.util.Calendar;
import java.text.*;
/**
* This class will manage the work between the GUI and employee objects.
*
* @author (Jekabs Stikans, Rihards Sillins, Duncan Hamilton, Ron Schonberg)
* @version (21.09.2012)
*/
public class Controller
{
private Employee[] employee;
private SimpleDateFormat fullDate,hourMinute;
/**
* Constructor for objects of class Controller
*/
public Controller()
{
this.employee = null;
this.hourMinute = new SimpleDateFormat("HH:mm:ss");
this.fullDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
loadEmployees();
}
public void loadEmployees()
{
// List all employee files.
File directory = new File("data");
File[] files = directory.listFiles();
// Create an array of employee objects.
int employeeCount = files.length;
employee = new Employee[employeeCount];
// Create employee objects.
for(int i=0;i<employeeCount;i++)
{
if(files[i].isFile())
{
String fileName = files[i].getName();
try
{
FileReader fr = new FileReader("data/"+fileName);
BufferedReader br = new BufferedReader(fr);
String name = br.readLine();
employee[i] = new Employee(name,fileName);
}
catch(IOException ioe)
{
System.out.println("IO Exception while reading the file!");
}
}
}
}
public Employee[] getEmployees()
{
return this.employee;
}
public boolean checkEmployeeIn(int employeeId)
{
boolean done = false;
long startTime = getTime();
String formatTime = fullDate.format(startTime);
// Set the current status for the employee as false.
employee[employeeId].setStatus(true);
BufferedWriter writer = null;
String fileToEdit = employee[employeeId].getFileName();
employee[employeeId].setStartTime(startTime);
try{
writer = new BufferedWriter(new FileWriter("data/"+fileToEdit, true));
writer.newLine();
writer.write(formatTime+"\t");
writer.flush();
writer.close();
done=true;
}
catch(IOException ioe)
{
System.out.println("IO Exception while writing time data to file!");
}
return done;
}
/**
* A method which will end the users session, update date in the
* DB file and return the spent time.
*
* @param The id (location in the employee array) of the employee.
* @return A string with the spent time.
*/
public void checkEmployeeOut(int id)
{
// Set the current status for the employee as false.
employee[id].setStatus(false);
// Get current time.
long currentTime = getTime();
String formatTime = fullDate.format(currentTime);
// Get the name of the user file.
String fileName = employee[id].getFileName();
BufferedWriter writer = null;
// Get the last line.
try{
writer = new BufferedWriter(new FileWriter("data/"+fileName, true));
writer.write(formatTime+"");
writer.flush();
writer.close();
}
catch(IOException ioe)
{
System.out.println("IO Exception while reading the file!");
}
}
public String[] getUserLog(int id)
{
String fileName = employee[id].getFileName();
String line;
String[] data = new String[2];
String[] args;
String checkInString = null;
String checkOutString = null;
Date dateIn;
Date dateOut;
long checkInTime;
long checkOutTime;
long timeDiff;
String spentTime;
try
{
FileReader fr = new FileReader("data/"+fileName);
BufferedReader br = new BufferedReader(fr);
// Skip the first line that contains user name;
line = br.readLine();
line = br.readLine();
data[0] = "";
data[1] = "";
while(line != null)
{
args = line.split("\t");
// If the line is not an empty line. ( because of someone has edited it incorreclty )
if(!args[0].equals("\n"))
{
checkInString = args[0];
// If I would add the new lines just with data[1] = spenTime+"\n"; Then at the end of the string there
// would be a new line, which later would be written to the log..
if(!data[0].equals(""))
{
data[0] += "\n";
}
data[0] += checkInString+"\t"; // Part of array containing check in and check out times
if(args.length > 1)
{
checkOutString = args[1];
data[0] += checkOutString;
}
else
{
checkOutString = null;
data[0] += "\n";
}
}
else
{
checkInString = null;
checkOutString = null;
data[0] += "\n";
// data[1] += "\n";
}
// Lets calculate the time spent in each day.
if(checkInString != null && checkOutString != null)
{
// Now, when we have check in and check out dates convert them to long.
try
{
dateIn = fullDate.parse(checkInString);
dateOut = fullDate.parse(checkOutString);
checkInTime = dateIn.getTime();
checkOutTime = dateOut.getTime();
timeDiff = checkOutTime-checkInTime;
spentTime = hourMinute.format(timeDiff);
// If I would add the new lines just with data[1] = spenTime+"\n"; Then at the end of the string there
// would be a new line, which later would be written to the log..
if(!data[1].equals(""))
{
data[1] += "\n";
}
data[1] += spentTime;
}
catch(ParseException e)
{
data[1] += "\nn/a";
}
}
else
{
data[1] += "\nn/a";
}
line = br.readLine();
}
}
catch(IOException ioe)
{
System.out.println("IO Exception while reading the file!");
}
return data;
}
public void editFile(String textFromWindow, int employeeId)
{
PrintWriter writer = null;
String fileToEdit = employee[employeeId].getFileName();
try
{
writer = new PrintWriter(new File("data/"+fileToEdit));
writer.write(textFromWindow);
writer.print(" ");
writer.flush();
writer.close();
}
catch(IOException ioe)
{
System.out.println("IO Exception while writing to the file!");
}
}
/**
* This method returns the Long of milliseconds since the first of January 1970
* Using this we can calculate the current Time
*
* @param NONE
* @return long Time
*/
public long getTime()
{
Calendar now = Calendar.getInstance();
long time = now.getTimeInMillis();
return time;
}
}
----------------------------------------------------------------------------------
/**
* This class represents an Employee
*
* @author (Ron Schonberg, Jekabs Stikans, Rihards Sillins, Duncan Hamilton)
* @version (21.09.2012)
*/
public class Employee
{
private String name;
private String fileName;
private long startTime;
private boolean status;
/**
* Constructor for objects of class Employee
*/
public Employee(String name,String fileName)
{
this.name = name;
this.fileName = fileName;
this.status = false;
this.startTime = 0;
}
/**
*
*
* @param
* @return
*/
public String getName()
{
return name;
}
/**
*
*
* @param
* @return
*/
public boolean getStatus()
{
return status;
}
public void setStatus(boolean x)
{
this.status = x;
}
public long getStartTime()
{
return startTime;
}
public void setStartTime(long x)
{
startTime = x;
}
public int getTimeSpent()
{
return 1;
}
public String getFileName()
{
return this.fileName;
}
}