Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */

package com.mycompany.assign_final;

import java.util.List;

/**
 *
 * @author atom
 */
public class Assign_final {
 public static void main(String[] args) {
                new EventSchedulerGUI();

}
}

Assign_final.java
Displaying Room.java.Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.assign_final;

/**
 *
 * @author atom
 */
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

public class EventSchedulerGUI extends JFrame {

    private Scheduler scheduler;
    private JTable seminarTable;
    private JTable schedulingTable;
    private JTable conflictTable;
    private DefaultTableModel seminarTableModel;
    private DefaultTableModel schedulingTableModel;
    private DefaultTableModel conflictTableModel;

    public EventSchedulerGUI() {
        scheduler = new Scheduler();
        createGUI();
    }

    private void createGUI() {
        setLayout(new BorderLayout());

        JPanel seminarPanel = new JPanel();
        seminarPanel.setLayout(new BorderLayout());
        
        String[] seminarColumns = {"Seminar", "Speaker", "Attendees"};
        seminarTableModel = new DefaultTableModel(seminarColumns, 0);
        seminarTable = new JTable(seminarTableModel);

        JScrollPane seminarScrollPane = new JScrollPane(seminarTable);

        // Seminar File Upload Button
        JButton seminarFileUploadButton = new JButton("Upload Seminar File");
        seminarFileUploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                uploadSeminarFile();
            }
        });

        seminarPanel.add(seminarScrollPane, BorderLayout.CENTER);
        seminarPanel.add(seminarFileUploadButton, BorderLayout.SOUTH);

        // Scheduling Panel
        JPanel schedulingPanel = new JPanel();
        schedulingPanel.setLayout(new BorderLayout());

      
        String[] schedulingColumns = {"Seminar", "Room", "Time Slot"};
        schedulingTableModel = new DefaultTableModel(schedulingColumns, 0);
        schedulingTable = new JTable(schedulingTableModel);

        JScrollPane schedulingScrollPane = new JScrollPane(schedulingTable);

        // Scheduling File Upload Button
        JButton schedulingFileUploadButton = new JButton("Upload Scheduling File");
        schedulingFileUploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                uploadSchedulingFile();
            }
        });

        schedulingPanel.add(schedulingScrollPane, BorderLayout.CENTER);
        schedulingPanel.add(schedulingFileUploadButton, BorderLayout.SOUTH);

        JPanel conflictPanel = new JPanel();
        conflictPanel.setLayout(new BorderLayout());

        String[] conflictColumns = {"Conflict"};
        conflictTableModel = new DefaultTableModel(conflictColumns, 0);
        conflictTable = new JTable(conflictTableModel);

        JScrollPane conflictScrollPane = new JScrollPane(conflictTable);

        // Analyze Button
        JButton analyzeButton = new JButton("Analyze");
        analyzeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                analyzeConflicts();
            }
        });

        conflictPanel.add(conflictScrollPane, BorderLayout.CENTER);
        conflictPanel.add(analyzeButton, BorderLayout.SOUTH);

        // Tabbed Pane
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Seminars", seminarPanel);
        tabbedPane.addTab("Scheduling", schedulingPanel);
        tabbedPane.addTab("Conflicts", conflictPanel);

        add(tabbedPane, BorderLayout.CENTER);

        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void uploadSeminarFile() {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            scheduler.loadSeminars(selectedFile.getAbsolutePath());
            updateSeminarTable();
        }
    }

    private void uploadSchedulingFile() {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            scheduler.loadScheduling(selectedFile.getAbsolutePath());
            updateSchedulingTable();
        }
    }

    private void analyzeConflicts() {
        List<String> conflicts = scheduler.findAllConflicts();
        updateConflictTable(conflicts);
    }

    private void updateSeminarTable() {
        seminarTableModel.setRowCount(0);
        for (Seminar seminar : scheduler.seminars) {
            String[] row = {seminar.getName(), seminar.getSpeaker(), seminar.getAttendeeIds().toString()};
            seminarTableModel.addRow(row);
        }
    }

    private void updateSchedulingTable() {
        schedulingTableModel.setRowCount(0);
        for (Seminar seminar : scheduler.seminars) {
            if (seminar.getRoom() != null && seminar.getSlot() != null) {
                String[] row = {seminar.getName(), seminar.getRoom().getRoomNumber(), String.valueOf(seminar.getSlot().getSlotId())};
                schedulingTableModel.addRow(row);
            }
        }
    }

    private void updateConflictTable(List<String> conflicts) {
        conflictTableModel.setRowCount(0);
        for (String conflict : conflicts) {
            String[] row = {conflict};
            conflictTableModel.addRow(row);
        }
    }

    
}

EventSchedulerGUI.java
Displaying Room.java.Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.assign_final;


public class Room {
    private String roomNumber;

    public Room(String roomNumber) {
        this.roomNumber = roomNumber;
    }

    // Getter
    public String getRoomNumber() {
        return roomNumber;
    }

    @Override
    public String toString() {
        return "Room{" +
                "roomNumber='" + roomNumber + '\'' +
                '}';
    }
}

Room.java
Displaying Room.java.Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.assign_final;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Scheduler {
    public List<Seminar> seminars;
    private List<Room> rooms;
    private List<Slot> slots;

    public Scheduler() {
        seminars = new ArrayList<>();
        rooms = new ArrayList<>();
        slots = new ArrayList<>();
    }

   

  public void loadSeminars(String filepath) {
        try (BufferedReader bf = new BufferedReader(new FileReader(filepath))) {
            String line;
            while ((line = bf.readLine()) != null) {
                String[] parts = line.strip().split(",");
                if (parts.length < 4) {
                    System.err.println("Invalid seminar entry (not enough fields): " + line);
                    continue;
                }
     //0 index = seminar name
    //1 index = speaker
    //2-5 index = attendee ids
    
                String seminarName = parts[0].trim();
                
                String speaker = parts[1].trim();
                
                Seminar seminar = new Seminar(seminarName, speaker);
                for (int i = 2; i < parts.length; i++) {
                    try {
                        Integer attendeeId = Integer.parseInt(parts[i].trim());
                        seminar.addAttendee(attendeeId);
                    } catch (NumberFormatException e) 
                    {
                        
                        System.err.println("Invalid attendee ID \"" + parts[i] + "\" in seminar \"" + seminarName + "\".");
                    }
                }
                seminars.add(seminar);
            }
        } catch (IOException e) {
            System.err.println("Error reading seminars file: " + e.getMessage());
        }
    }
  public void loadScheduling(String filepath) {
        try (BufferedReader bf = new BufferedReader(new FileReader(filepath))) {
            String line;
            while ((line = bf.readLine()) != null) {
                String[] parts = line.strip().split(",");
                if (parts.length != 3) 
                {
                    System.err.println("Invalid scheduling entry (incorrect number of fields): " + line);
                    continue;
                }
                
                // 0 index = seminar 
                // 1 index = room number
                // 2 index = timeslots
                String seminarName = parts[0].trim();
                String roomNumber = parts[1].trim();
                String slotStr = parts[2].trim();

                Seminar seminar = findSeminarByName(seminarName);
                if (seminar == null) {
                    
                    System.err.println("Seminar \"" + seminarName + "\" not found. Skipping scheduling.");
                    continue;
                }

                Room room = findRoomByNumber(roomNumber);
                if (room == null) {
                    
                    
                    room = new Room(roomNumber);
                    rooms.add(room);
                }

                int slotId;
                try {
                    
                    slotId = Integer.parseInt(slotStr);
                } catch (NumberFormatException e) {
                    
                    System.err.println("Invalid slot ID \"" + slotStr + "\" for seminar \"" + seminarName + "\".");
                    continue;
                }

                Slot slot = findSlotById(slotId);
                if (slot == null) {
                    slot = new Slot(slotId);
                    slots.add(slot);
                }

                seminar.setRoom(room);
                seminar.setSlot(slot);
            }
        } catch (IOException e) {
            
            System.err.println("Error reading scheduling file: " + e.getMessage());
        }
    }

    private Seminar findSeminarByName(String name) {
        for (Seminar seminar : seminars) {
            if (seminar.getName().equalsIgnoreCase(name)) {
                return seminar;
            }
        }
        return null;
    }

  
    private Room findRoomByNumber(String roomNumber) {
        for (Room room : rooms) {
            if (room.getRoomNumber().equalsIgnoreCase(roomNumber)) {
                return room;
            }
        }
        return null;
    }

   
    private Slot findSlotById(int slotId) {
        for (Slot slot : slots) {
            if (slot.getSlotId() == slotId) {
                return slot;
            }
        }
        return null;
    }

    public void printSchedule() {
        System.out.println("----- Scheduled Seminars -----");
        for (Seminar seminar : seminars) {
            System.out.println(seminar);
        }
        System.out.println("  ------------------------------   ");
    }

   public List<String> findAttendeeAndSlotConflicts(String targetSeminarName) 
   {
        List<String> conflicts = new ArrayList<>();
        
        Seminar targetSeminar = findSeminarByName(targetSeminarName);
        if (targetSeminar == null) {
            System.err.println("Seminar \"" + targetSeminarName + "\" not found.");
            return conflicts;
        }

        Slot targetSlot = targetSeminar.getSlot();
        if (targetSlot == null)
        {
            System.err.println("Seminar \"" + targetSeminarName + "\" is not assigned to any slot.");
            return conflicts;
        }

        for (Seminar seminar : seminars) {
            if (seminar.equals( targetSeminar))
            {
                continue;
            }
            if (seminar.getSlot() != null && seminar.getSlot().getSlotId() == targetSlot.getSlotId()) {
                // Check for any common 
                for (Integer attendeeId : seminar.getAttendeeIds()) {
                    if (targetSeminar.getAttendeeIds().contains(attendeeId)) {
                        String conflictInfo = " Conflict: \"" + targetSeminar.getName() + "\" and \"" +
                                seminar.getName() + "\" share attendee ID " + attendeeId +
                                " and are scheduled in slot " + targetSlot.getSlotId() + ".";
                        conflicts.add(conflictInfo);
                        break; 
                    }
                }
            }
        }

        return conflicts;
    }

    /**
     * Finds and returns a list of seminars that are scheduled in the same room and slot as the given seminar.
    
     */
    public List<String> findRoomAndSlotConflicts(String targetSeminarName) 
    {
        List<String> conflicts = new ArrayList<>();
        Seminar targetSeminar = findSeminarByName(targetSeminarName);
        if (targetSeminar == null) {
            System.err.println("Seminar \"" + targetSeminarName + "\" not found.");
            return conflicts;
        }

        Room targetRoom = targetSeminar.getRoom();
        Slot targetSlot = targetSeminar.getSlot();

        if (targetRoom == null || targetSlot == null) 
        {
            System.err.println("Seminar \"" + targetSeminarName + "\" is not fully scheduled.");
            return conflicts;
        }

        for (Seminar seminar : seminars) {
            if (seminar.equals(targetSeminar)) {
                continue;
            }
            if (seminar.getRoom() != null && seminar.getSlot() != null) {
                
                if (seminar.getRoom().getRoomNumber().equalsIgnoreCase(targetRoom.getRoomNumber())
                        && seminar.getSlot().getSlotId() == targetSlot.getSlotId()) {
                    String conflictInfo = "Conflict: \"" + targetSeminar.getName() + "\" and \"" +
                            seminar.getName() + "\" are both scheduled in room \"" +
                            targetRoom.getRoomNumber() + "\" at slot " + targetSlot.getSlotId() + ".";
                    conflicts.add(conflictInfo);
                    
                }
            }
        }

        return conflicts;
    }

    
   public List<String> findAllConflicts() {
    List<String> conflicts = new ArrayList<>();

    // Check for conflicts between seminars in the same room and time slot
    for (Seminar seminar1 : seminars) {
        for (Seminar seminar2 : seminars) {
            if (seminar1 != seminar2 && seminar1.getRoom().equals(seminar2.getRoom()) && seminar1.getSlot().equals(seminar2.getSlot())) {
                conflicts.add("Conflict between " + seminar1.getName() + " and " + seminar2.getName() + " in room " + seminar1.getRoom().getRoomNumber() + " at time slot " + seminar1.getSlot().getSlotId());
            }
        }
    }

    // Check for conflicts between seminars with the same attendee
    for (Seminar seminar1 : seminars) {
        for (Seminar seminar2 : seminars) {
            if (seminar1 != seminar2) {
                for (Integer attendeeId : seminar1.getAttendeeIds()) {
                    if (seminar2.getAttendeeIds().contains(attendeeId)) {
                        conflicts.add("Conflict between " + seminar1.getName() + " and " + seminar2.getName() + " for attendee " + attendeeId);
                    }
                }
            }
        }
    }

    return conflicts;
}
}

Scheduler.java
Displaying Scheduler.java.Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.assign_final;


import java.util.ArrayList;
import java.util.List;

public class Seminar {
    private String name;
    private String speaker;
    private List<Integer> attendeeIds;
    private Room room; //cuurent room and slot
    private Slot slot; 

    public Seminar(String name, String speaker) {
        
        this.name = name;
        this.speaker = speaker;
        this.attendeeIds = new ArrayList<>();
        
    }

    // Getters and Setters
    public String getName() {
        
        return name;
    }

    public String getSpeaker() {
        return speaker;
    }

    public List<Integer> getAttendeeIds() {
        
        return attendeeIds;
    }

    public void addAttendee(Integer attendeeId) {
        
        attendeeIds.add(attendeeId);
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

    public Slot getSlot() {
        return slot;
    }

    public void setSlot(Slot slot) {
        
        this.slot = slot;
    }

    @Override
    public String toString() {
        return "Seminar{" +
                "name='" + name + '\'' +
                ", speaker='" + speaker + '\'' +
                ", attendeeIds=" + attendeeIds +
                ", room=" + (room != null ? room.getRoomNumber() : "Unassigned") +
                ", slot=" + (slot != null ? slot.getSlotId() : "Unassigned") +
                '}';
    }
}

Seminar.java
Displaying Seminar.java.Skip to main content
Google ClassroomClassroom
SCD-A (Fall 2024)A
O
T
S
O
S
O
S
F
S
O
A
E
Assignment-2
Assignment-2
farooq ahmed
•
Oct 10
40 points
Due Oct 21, 11:59 PM
Implement using Java Swing
Assignment-2.pdf
PDF
Class comments
Your work
Turned in
Assign_final.java
Java
EventSchedulerGUI.java
Java
Room.java
Java
Scheduler.java
Java
Seminar.java
Java
Slot.java
Java
Private comments

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.assign_final;


public class Slot {
    private int slotId;

    public Slot(int slotId) {
        this.slotId = slotId;
    }

    public int getSlotId() {
        return slotId;
    }

    @Override
    public String toString() {
        return "Slot{" +
                "slotId=" + slotId +
                '}';
    }
}

Slot.java
Displaying Slot.java. todo: Event Scheduling
(a) Layered Architecture: Extend Assignment-2 to support data storage in a relational database (Sql
Server, Sqlite, etc.) using a layered architecture:
1. Save the seminar and attendees information (uploaded from a file) into database. Show the
already stored information on UI after loading from the storage and allow to update and delete.
2. Save the scheduling information into database. Show the already stored information (Seminar,
Room, Time-slot) on UI after loading from the storage and allow to update and delete.
(b) Unit Tests: Write unit tests (using JUnit), specifically to test the Overlapping Attendees and Time
Conflicts requirements.