Monday, 24 September 2012

cout << "First tutorial." << endl;

As the semester starts to pick up its momentum, our group has the chance to experience the joy of learning C++, or basically learn how to transfer our previous knowledge to different programming languages.

Because of the fact that our group members have had different experience with C++ (or haven’t had any), there were multiple opinions regarding the questions raised on our Friday tutorial.
No matter what, the first week’s project helped us to refresh our Java skills, as they had rusted during the summer. Of course ‘googling’ and checking last year’s assignments helped us to solve the few problems that we encountered with. 

During the development of our software the team didn’t had any major arguments or difficulties working together, thought next time the organization could be done better by having a better documentation of what we want to achieve and how it should be done.

The first lecture gave us a good overview of what to expect from this course, how our work will be marked and it reminded us about plagiarism and code re-use.

Comparing Java and C++ versions of ‘Hello world’ the first thing that was noticed is that for every Java project you have to declare a new class and set the access level modifier for the main method, none of that should be done for the C++ version. That is because Java is OOP an opposite approach for C++ is that you can decide whether to code object oriented or not. There are few other things that were different in those two examples, like the fact that C++ returns 0 if the method has executed correctly. Also in Java there was no need to include extra libraries to print out something in the console, comparing to C++ where it is needed to include iostream library. C++ has a different way to output streams to console using “cout” and “<<”, plus now to unite strings and variables when outputting them you have to use “<<” or “>>” instead of “+” as we were used to do in Java. All in all, it is still programming and the principles are kept the same in almost every programming language, the most noticeable thing that a changed is syntax.
The C++ compiler gives you enough information to find errors and warnings.
As for the variables, there were not many differences that we noticed, the ones that we did were
* Need to include “string” library to use strings.
* Define a strings using “string” not like in Java “String”.
* New ways of defining strings – typdef, enum.

The syntax for defining constants in Java and C++ differs. In the first case we would use “final static” in front of our variable, but in C++ you use just “const”, this constant cannot be changed (that is why it Is a constant).

As I mentioned before, the output is a bit different in C++ than it was in Java, you have to do a std::cout << “somethinghere” << endl; instead of System.out.println(“somethinghere”);, it’s more difficult to tell about the input differences, because last year we were using Genio library which helped us to get the input from users, now we just use cin >> string; ( it is important to remember the difference between >> and << ).
Also loops are the same as they were in Java.

More or less there was nothing surprising during the last weeks lecture because of the previous programming knowledge, though I am sure that it will get more interesting during the next weeks.

Friday, 21 September 2012

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;  
   }  
 }  

Monday, 17 September 2012

Week 2 Begins

Ok, its now week 2, our program is functioning as per the basic requirements. However, certain features that we were wanting to implement are not quite finished, such as making the employee time sheet files encrypted/hidden for example. We have also all agreed that the Class design we are using is a bit messy, the Time class can be incorporated into the Control class and we are currently using 2 separate Classes for the employee GUI and the admin GUI. These should be merged into one Class, GUI. This messy class design is partly due to the fact that Ron had already written some of the code over summer and we have just "hacked" the rest of the methods to make the program run correctly. Ideally we should write the whole program again from scratch, leading to a far more elegant solution.

Friday, 14 September 2012

Class Design

Here is a Visio drawing of our Class Design, including fields, methods and parameters.


Thursday, 13 September 2012

It's Alive!

So yeah, I forgot to upload a post for the meeting we had yesterday. Probably because it was a 9am class. We finished our class design and had a look at a prototype version of the program that Ron had already written a few months ago. It was all in German, but it was a start. We decided what methods we were going to work on each, then drank coffee, lots of it. Ron's in charge of the GUI, I'm working on the clock in / file write methods and Jekabs is working on the Time and Control class.

Today in our 2 hour lab session we created the rest of the required methods; employeeClockIn and employeeClockOut, we also coded the file read and write to update the employee time record sheets. The GUI is almost done, the back end methods are all complete so all we need to do now is couple the GUI to the program.



Tuesday, 11 September 2012

First Meeting

Richard is currently still in Latvia, so we have taken an executive decision and have begun to design our program. Ron needs a program for his mother to track attendance of employees at her business, we felt this would be easy to implement in java, and also gives scope to add complexity. We have started the requirements design and aim to start coding at tomorrows lab at 9am.

Welcome!

Welcome to our Blog! We are all Applied Computing students at Dundee University. Here we will share our program developments and insights! Enjoy!