⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schedule.java

📁 The program is used for Classroom Scheduling for tutors and students. It contain gui tools for mana
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        setChanged();
        notifyObservers();
    }
    
    /**
     * Adds a Course to the document
     * @param Course to be added
     */
    
    synchronized public void addCourse(Course course) {
        if(!courses.contains(course)){
            courses.add(course);   	// Add the resource
            Collections.sort(courses);
            setChanged();			// Mark the document as changed
            notifyObservers();			// Tell everybody
        }
    }
    
    /**
     * Removes a Course from the document
     * @param Course to be removed
     */
    
    synchronized public void removeCourse(Course course) {
        courses.remove(course);
        setChanged();
        notifyObservers();
    }
    
    synchronized public void addBook(Textbook book){
        if(!books.contains(book)){
            books.add(book);
            //Collections.sort(books);
            setChanged();
            notifyObservers();
        }
    }
    synchronized public void removeBook(Textbook book){
        books.remove(book);
        setChanged();
        notifyObservers();
    }
    
    /**
     * Adds a SchedCourse to the document
     * @param SchedCourse to be added
     */
    
    synchronized public void addSchedCourse(SchedCourse schedCourse) {
         if(!schedCourses.contains(schedCourse)){
            schedCourse.getCourse().setScheduled(true);
            schedCourses.add(schedCourse);   	// Add the resource
            Collections.sort(schedCourses);
            setChanged();			// Mark the document as changed
            notifyObservers();			// Tell everybody
         }
    }
    
    /**
     * Removes a SchedCourse from the document
     * @param SchedCourse to be removed
     */
    
    synchronized public void removeSchedCourse(SchedCourse schedCourse) {
        schedCourse.getCourse().setScheduled(false);
        schedCourses.remove(schedCourse);
        setChanged();
        notifyObservers();
    }
    
    synchronized public void sort(int key) {
        //CPAVectorSort.sortVector(getSchedCourses(), key);
        setChanged();
        notifyObservers();
    }
    
    synchronized public void changed() {
        setChanged();
        notifyObservers();
    }
    
    /**
     * update the conflicts Vector
     */
    
    synchronized public void findConflicts() {
        // start fresh and find all conflicts
        conflicts.removeAll(conflicts);  // this seems strange to me.
        
        // remove conflict data from the SchedCourse objects
        Iterator it = schedCourses.iterator();
        while (it.hasNext())
            ((SchedCourse) it.next()).setConflict(null);
        
        for (int i = 0; i < schedCourses.size(); i++) {
            for (int j = i + 1; j < schedCourses.size(); j++) {
                SchedCourse sched1 = (SchedCourse) schedCourses.get(i);
                SchedCourse sched2 = (SchedCourse) schedCourses.get(j);
                
                if (sched1.overlap(sched2)) {
                    // same time same prof
                    Professor i1 = sched1.getProfessor();
                    Professor i2 = sched2.getProfessor();
                    if (i1.getLastName().equals(i2.getLastName())) {
                        if (!i1.getLastName().trim().equals("STAFF")) {
                            Conflict c = new Conflict((SchedCourse) schedCourses.get(i),
                            (SchedCourse) schedCourses.get(j),
                            PROF_DOUBLE_BOOKED);
                            conflicts.add(c);
                            sched1.setConflict(c);
                            sched2.setConflict(c);
                            continue; // stop looking when one is found  this
                            //  Favors professor conflicts
                            //System.out.println("Found a conflict \n" + c);
                        }
                    }
                    
                    // same time, same room
                    Classroom cr1 = ((SchedCourse) schedCourses.get(i)).getClassroom();
                    Classroom cr2 = ((SchedCourse) schedCourses.get(j)).getClassroom();
                    
                    if (cr1.getRoomName().equals(cr2.getRoomName()) &&
                    cr1.getBuilding().equals(cr2.getBuilding())) {
                        
                        if (!cr1.getRoomName().trim().equals("TBA")) {
                            Conflict c = new Conflict((SchedCourse) schedCourses.get(i),
                                (SchedCourse) schedCourses.get(j),
                            ROOM_DOUBLE_BOOKED);
                            conflicts.add(c);
                            sched1.setConflict(c);
                            sched2.setConflict(c);
                            
                            continue; // stop looking when one is found
                            //System.out.println("Found a conflict \n" + c);
                        }
                    }
                    
                }
            }    // for j
        }       // for i
    }        
    
    /**
     * Put the schedule in a file that can be read by Excel
     */
    
    synchronized public void exportCsv(File outCsv) throws IOException {
        
        PrintWriter pw = new PrintWriter(
                            new BufferedWriter(
                                new FileWriter(outCsv)));
        
        // put the headers on the cols
        pw.println("CRN,Name,Course Title, Course #, Section #,Credit Hrs,"
        +  "Bldg,Room,Time,MTWRFSN,Cap,Note/Conflict");
        
        Iterator it = schedCourses.iterator();
        
        while (it.hasNext()) {
            SchedCourse item = (SchedCourse) it.next();
            pw.print(item.getProfessor().getLastName().toUpperCase() + ","
                + item.getCrn() + ","
                + item.getCourse().getCourseName() + ","
                + item.getCourse().getField()
                + item.getCourse().getCourseNumber() + ","
                + item.getCourse().getSectionPrefix()
                + item.getCourse().getSectionNumber() + ","                
                + "," // credits
           
                + item.getClassroom().getBuilding() + ","
                + item.getClassroom().getRoomName() + ","
                + item.getTimeSlot().toTimeString() + ","
                + item.getTimeSlot().toDaysString()+ ","
                + item.getClassroom().getCapacity() + ","
                + item.getNote()
                + "  "); 
            
            Conflict conflict = item.getConflict();
            
            if (conflict != null) {
                String strConflict = "Conflict: ";
                SchedCourse inCon = null;
                // show which one is in conflict
                if (conflict.getSched1().equals(item))
                    inCon = conflict.getSched2();
                else
                    inCon = conflict.getSched1();
                
                strConflict += inCon.getCourse().getField()
                + inCon.getCourse().getCourseNumber() + " "
                + inCon.getCourse().getSectionPrefix()
                + inCon.getCourse().getSectionNumber();
                pw.print(strConflict);
            }
            pw.println();
        }
        pw.close();
    }
    
    
    /**
     * Print some text schedcourses on each page
     */
    
    synchronized public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if (pageIndex > getNumberOfPages()) {
            return NO_SUCH_PAGE;    // done printing
        }
        
        draw(g, pageFormat, pageIndex,
        new Rectangle((int)(pageFormat.getPaper().getImageableX()),
        (int)(pageFormat.getPaper().getImageableY()),
        (int)(pageFormat.getPaper().getImageableWidth()),
        (int)(pageFormat.getPaper().getImageableHeight())));
        
        return PAGE_EXISTS;
    }
    
    /**
     * Used as a helper for printing.  Changes the margins of the paper to get
     * more stuff on the page.
     *
     */
    
    
    /**
     * draw the schedule for a page for printing
     *
     */
    
    synchronized private void draw(Graphics g, PageFormat pageFormat, int pageIndex,
    Rectangle r) {
        
        Graphics g2d = g;
        ((Graphics2D)g).setStroke( new BasicStroke(0.5F)); // make thin lines
        
        Font	    titleFont = new Font("Monospaced", Font.BOLD, 10);
        Font	    regFont = new Font("Monospaced", Font.PLAIN, 8);
        Font	    regFontBold = new Font("Monospaced", Font.BOLD, 8);
        FontMetrics fm = g2d.getFontMetrics(titleFont);     // Get font metrics
        String      header = "Class Schedule:  " + scheduleName + updated();
        String      footer = "Total of: " + schedule.getSchedCourses().size()
        + " classes.";
        int	    titleWidth = fm.stringWidth(header);    // Get title width
        int	    titleHeight = fm.getHeight();	    // Get title height
        int	    lineHeight = fm.getHeight();	    // Get line height
        int	    linesPerPage = 40;
        
        //		System.out.println("Lines per page = " + linesPerPage);
        //		System.out.println("There are: " + schedule.getSchedCourses().size()
        //			+ " records\n"
        //			+ "Calc 1.0 * schedule.getSchedCourses().size() / linesPerPage:"
        //			+ 1.0 * schedule.getSchedCourses().size()
        //			/ linesPerPage + "\npageIndex " + pageIndex);
        
        
        g.setFont(titleFont);
        g.drawString(header,
          (int) pageFormat.getImageableX(),
          ((int) pageFormat.getImageableY() + titleHeight));
        
        String pageNumberString = (pageIndex + 1) + "/" + getNumberOfPages();
        //        System.out.println("Page#String:" + pageNumberString + ":to here");
        //        System.out.println("is this wide: " + fm.stringWidth(pageNumberString));
        //        System.out.println("r is: " + r);
        g.drawString(pageNumberString,
        (int) (r.width - fm.stringWidth(pageNumberString) +
        pageFormat.getImageableX()),
        (int) pageFormat.getImageableY() + titleHeight);
        
        fm = g.getFontMetrics(regFont);    // Get font metrics
        
        int ypos = (int) pageFormat.getImageableY() + titleHeight * 2;    // ypos for first line
        
        g.setFont(regFont);
        
        String prev = "";
        int currentRecord = pageIndex * linesPerPage;
        int currentLine = 0;
        
        //		System.out.println("Current record \n" + currentRecord);
        
        g.drawLine(r.x, ypos + 5 - lineHeight,
        r.x + r.width, ypos + 5 - lineHeight);
        
        for (int i = currentRecord; i < currentRecord + linesPerPage; i++) {
            SchedCourse sc = (SchedCourse)(schedule.getSchedCourses().get(i));
            
            if(sc.getConflict() != null)
                g.setFont(regFontBold);
            else
                g.setFont(regFont);            
            g.drawString(sc.toString(), (int) pageFormat.getImageableX(), ypos);
            if ((i+1) % 4 == 0)
                g.drawLine(r.x, ypos + 5, r.x + r.width, ypos + 5);
            
            if(!(sc.getNote().equals("")) && sc.getNote() != null){ // Print the note
                ypos += lineHeight;
                g.drawString("   " + sc.getNote(),(int) pageFormat.getImageableX(), ypos);
                linesPerPage--;  // this is ugly
            }            
            ypos += lineHeight;
            
            // just printed the last record
            if (i == schedule.getSchedCourses().size() - 1) {
                
                ypos += lineHeight;		    // space
                
                g.setFont(titleFont);
                g.drawString(footer, (int) pageFormat.getImageableX(), ypos);
                //				System.out.println("Printing footer at: " + ypos);
                //				System.out.println("Printing last record: i = " + i);
                //				System.out.println(" at location x = "
                //					+ (int) pageFormat.getImageableX()
                //					+ " \n ypos is " + ypos);
                
                break;
            }
        }
    }
    
    // Always 40 courses per page
    synchronized public int getNumberOfPages() {
        return schedCourses.size() / 41 + 1; // for 40 records this is 1.
    }
    
    // Return the Printable object that will render the page
    synchronized public Printable getPrintable(int pageIndex) {
        return this;
    }
    
    // required to implement Pageable  sets the paper to half inch margins and
    //  and imageable area of 7.5 x 10 for max paper useage
    
    synchronized public PageFormat getPageFormat(int pageIndex) {
        // Get the default page format and its Paper object.
        PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
        Paper paper = pageFormat.getPaper();
        pageFormat.setPaper(paper);   // Restore the paper
        return pageFormat;            // Return the page format
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -