001    package net.sf.cpsolver.exam.reports;
002    
003    import java.text.DecimalFormat;
004    import java.util.Enumeration;
005    import java.util.Vector;
006    
007    import net.sf.cpsolver.exam.model.ExamModel;
008    import net.sf.cpsolver.exam.model.ExamPeriod;
009    import net.sf.cpsolver.exam.model.ExamStudent;
010    import net.sf.cpsolver.ifs.util.CSVFile;
011    import net.sf.cpsolver.ifs.util.CSVFile.CSVField;
012    
013    /**
014     * Export distribution of number of students by number of meetings per day into a CSV file.
015     * <br><br>
016     * Usage:<br>
017     * <code>
018     * &nbsp;&nbsp;&nbsp;&nbsp;new ExamNbrMeetingsPerDay(model).report().save(file);
019     * </code>
020     * <br><br>
021     * 
022     * @version
023     * ExamTT 1.1 (Examination Timetabling)<br>
024     * Copyright (C) 2008 Tomáš Müller<br>
025     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026     * Lazenska 391, 76314 Zlin, Czech Republic<br>
027     * <br>
028     * This library is free software; you can redistribute it and/or
029     * modify it under the terms of the GNU Lesser General Public
030     * License as published by the Free Software Foundation; either
031     * version 2.1 of the License, or (at your option) any later version.
032     * <br><br>
033     * This library is distributed in the hope that it will be useful,
034     * but WITHOUT ANY WARRANTY; without even the implied warranty of
035     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
036     * Lesser General Public License for more details.
037     * <br><br>
038     * You should have received a copy of the GNU Lesser General Public
039     * License along with this library; if not, write to the Free Software
040     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
041     */
042    public class ExamNbrMeetingsPerDay {
043        private ExamModel iModel = null;
044        
045        /**
046         * Constructor
047         * @param model examination timetabling model
048         */
049        public ExamNbrMeetingsPerDay(ExamModel model) {
050            iModel = model;
051        }
052        
053        /**
054         * generate report
055         */
056        public CSVFile report() {
057            CSVFile csv = new CSVFile();
058            Vector header = new Vector();
059            header.add(new CSVField("Date"));
060            header.add(new CSVField("None"));
061            for (int i=1;i<=5;i++) 
062                header.add(new CSVField(i==5?"5+":String.valueOf(i)));
063            header.add(new CSVField("Back-To-Back"));
064            csv.setHeader(header);
065            DecimalFormat df = new DecimalFormat("0.0");
066            int[] nrExamsTotal = new int[6];
067            for (int i=0;i<=5;i++) nrExamsTotal[i]=0;
068            int btbTotal = 0;
069            for (int d=0;d<iModel.getNrDays();d++) {
070                ExamPeriod period = null; 
071                for (Enumeration e=iModel.getPeriods().elements();e.hasMoreElements();) {
072                    period = (ExamPeriod)e.nextElement();
073                    if (period.getDay()==d) break;
074                }
075                int[] nrExams = new int[6];
076                for (int i=0;i<=5;i++) nrExams[i]=0;
077                int btb = 0;
078                for (Enumeration f=iModel.getStudents().elements();f.hasMoreElements();) {
079                    ExamStudent student = (ExamStudent)f.nextElement();
080                    int ex = student.getExamsADay(d).size();
081                    nrExams[ex<=5?ex:5]++;
082                    ExamPeriod p = period;
083                    while (p.next()!=null && (iModel.isDayBreakBackToBack()?p:p.next()).getDay()==d) {
084                        btb+=student.getExams(p).size()*student.getExams(p.next()).size();
085                        p = p.next();
086                    }
087                }
088                Vector line = new Vector();
089                line.add(new CSVField(period.getDayStr()));
090                for (int i=0;i<=5;i++) {
091                    line.add(new CSVField(nrExams[i]));
092                    nrExamsTotal[i]+=nrExams[i];
093                }
094                line.add(new CSVField(btb));
095                btbTotal+=btb;
096                csv.addLine(line);
097            }
098            Vector line = new Vector();
099            line.add(new CSVField("Total"));
100            for (int i=0;i<=5;i++)
101                line.add(new CSVField(nrExamsTotal[i]));
102            line.add(new CSVField(btbTotal));
103            csv.addLine(line);
104            return csv;
105        }
106    }