Skip to content
Snippets Groups Projects
Schedule.java 6.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • eyan_'s avatar
    eyan_ committed
    package com.example.schedulerapp;
    
    
    eyan_'s avatar
    eyan_ committed
    import java.sql.*;
    
    eyan_'s avatar
    eyan_ committed
    public class Schedule {
    
    eyan_'s avatar
    eyan_ committed
        private final Connection dbConnection;
    
    eyan_'s avatar
    eyan_ committed
        protected Schedule(Connection c) {
            this.dbConnection = c;
        }
    
    eyan_'s avatar
    eyan_ committed
        /*
        Name: addShift
        Parameters:
            int ID: The employees unique ID.
            Date: The date as a string in the form of "yyyy-mm-dd".
            start: The employees shift start time.
            end: The employees shift end time.
        Description: Adds a new shift to the database, referencing the employees ID, and assigning a unique shift number.
        Return: none
         */
    
    eyan_'s avatar
    eyan_ committed
        protected void addShift(int ID, String date, int start, int end){
            Date newDate = Date.valueOf(date);
            try {
                Statement myStatement = dbConnection.createStatement();
                myStatement.executeUpdate("INSERT INTO shifts (employee_id, full_date, start_time, end_time) " +
                        "VALUES ('" + ID +"', '"+ newDate +"', '"+ start +"', '"+ end +"')");
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
    eyan_'s avatar
    eyan_ committed
        /*
    
        Name: removeShiftByID
    
    eyan_'s avatar
    eyan_ committed
        Parameters:
    
            int shiftID: The shift unique ID.
        Description: Removes the employees shift matching the shift date.
    
    eyan_'s avatar
    eyan_ committed
        Return: none
         */
    
        protected void removeShiftByID(String shiftID){
    
    eyan_'s avatar
    eyan_ committed
            try {
                Statement myStatement = dbConnection.createStatement();
    
                myStatement.executeUpdate("DELETE FROM shifts WHERE (shift_id =" + shiftID + ")");
    
    eyan_'s avatar
    eyan_ committed
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
        /*
        Name: removeShiftByID
        Parameters:
            int shift_id: The shift unique ID.
            String date: The date of the shift in the form "YYYY-MM-DD".
            String start: The start time of the shift in 24-hour time "HHMM".
            String end: The end time of the shift in 24-hour time "HHMM".
        Description: Edits the time and date of the of a shift if it has the same shift id
        Return: none
     */
    
        protected void editShift(Integer shift_id, String date, String start,String end){
            Date newDate = Date.valueOf(date);
            System.out.println(newDate);
    
            try {
                Statement myStatement = dbConnection.createStatement();
    
                myStatement.executeUpdate("update shifts set full_date='" + newDate + "',start_time=" + start + ",end_time=" + end
                        + " where shift_id=" + shift_id);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    
    eyan_'s avatar
    eyan_ committed
        /*
        Name: getShifts
        Parameters:
            Date: The date as a string in the form of "yyyy-mm-dd".
        Description: Returns info for all shifts on a given date.
        Return: String
         */
        protected String getShifts(String date){
            Date newDate = Date.valueOf(date);
            String response = "getShifts";
            try {
                Statement myStatement = dbConnection.createStatement();
                ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date = " + newDate +")");
                while (myRs.next()) {
                    response = response + "/" + myRs.getString("full_date") + "." +
                            myRs.getString("employee_id") + "." + myRs.getString("start_time") +
                            "." + myRs.getString("end_time");
                }
    
    eyan_'s avatar
    eyan_ committed
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return response;
    
        /*
        Name: getShiftsByWeek
        Parameters:
            String startDate: The start date of the week. In the form "YYYY-MM-DD".
            String endDate: The last date of the week. In the form "YYYY-MM-DD".
        Description: Returns info for all shifts with in the given start date and end date .
        Return: String
         */
    
    eyan_'s avatar
    eyan_ committed
        protected String getShiftsByWeek(String startDate, String endDate){
            Date newStart = Date.valueOf(startDate);
            Date newEnd = Date.valueOf(endDate);
            String response = "getShiftsByWeek";
            try {
                Statement myStatement = dbConnection.createStatement();
                ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date >= " + newStart +")" +
                        "AND WHERE (full_date <= " + newEnd +")");
                while (myRs.next()) {
                    response = response + "/" + myRs.getString("full_date") + "." +
                            myRs.getString("employee_id") + "." + myRs.getString("start_time") +
                            "." + myRs.getString("end_time");
                }
    
    eyan_'s avatar
    eyan_ committed
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return response;
    
        /*
        Name: getEmployeeShiftsByWeek
        Parameters:
            Int ID: An employee id.
            String startDate: The start date of the week. In the form "YYYY-MM-DD".
            String endDate: The last date of the week. In the form "YYYY-MM-DD".
        Description: Returns info for all shifts with in the given start date and end date, for the given employee.
        Return: String
         */
    
    eyan_'s avatar
    eyan_ committed
        protected String getEmployeeShiftsByWeek(int ID, String startDate, String endDate){
            Date newStart = Date.valueOf(startDate);
            Date newEnd = Date.valueOf(endDate);
            String response = "getEmployeeShiftsByWeek";
            try {
                Statement myStatement = dbConnection.createStatement();
                ResultSet myRs = myStatement.executeQuery("SELECT FROM shifts WHERE (full_date >= " + newStart +")" +
                        "AND WHERE (full_date <= " + newEnd +") AND WHERE (employee_id = "+ ID + ")");
                while (myRs.next()) {
                    response = response + "/" + myRs.getString("full_date") + "." +
                            myRs.getString("employee_id") + "." + myRs.getString("start_time") +
                            "." + myRs.getString("end_time");
                }
    
    eyan_'s avatar
    eyan_ committed
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return response;
    
        /*
            Name: allShifts
            Parameters:
                None
            Description: Returns a list of all shifts in the database. Where each '/' split is a shift, and each '.' split
            is a detail of the shift.
            Return: String
             */
    
    eyan_'s avatar
    eyan_ committed
        protected String allShifts(){
            String response = "allShifts";
            try {
                Statement myStatement = dbConnection.createStatement();
                ResultSet myRs = myStatement.executeQuery("SELECT * FROM shifts");
                while (myRs.next()) {
                    response = response + "/" + myRs.getString("employee_id" ) + "." +
                            myRs.getString("full_date") + "." + myRs.getString("start_time") +
    
                            "." + myRs.getString("end_time") + "." + myRs.getString("shift_id");
    
    eyan_'s avatar
    eyan_ committed
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return response;
        }
    
    eyan_'s avatar
    eyan_ committed
    }