001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/user/QuizData.java,v 1.6 2004/07/01 05:50:21 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.user;
021    
022    import java.util.Date;
023    import org.apache.log4j.*;
024    
025    
026    /**
027     * Data describing a single quiz on a single chapter.
028     *
029     * @author <a href="mailto:tj_willis@users.sourceforge.net"> T.J. Willis </a>
030     * @version 1.0
031     */
032    public class QuizData {
033        private int right;
034        private int wrong;
035        private int total;
036        private Date start;
037    
038        /**
039         * Creates a new <code>QuizData</code> instance with no
040         * answers and quiz starting right now.
041         *
042         */
043        public QuizData() {
044            right = 0;
045            wrong = 0;
046            total = 0;
047            start = new Date();
048        }
049    
050    
051        /**
052         * Describe <code>setRight</code> method here.
053         *
054         * @param r an <code>int</code> value
055         */
056        public void setRight(final int r) {
057            right = r;
058        }
059    
060        /**
061         * Describe <code>setWrong</code> method here.
062         *
063         * @param r an <code>int</code> value
064         */
065        public void setWrong(final int r) {
066            wrong = r;
067        }
068    
069        /**
070         * Describe <code>setTotal</code> method here.
071         *
072         * @param r an <code>int</code> value
073         */
074        public void setTotal(final int r) {
075            total = r;
076        }
077    
078        /**
079         * Describe <code>setStart</code> method here.
080         *
081         * @param when a <code>long</code> value
082         */
083        public void setStart(final long when) {
084            start = new Date(when);
085        }
086    
087        /**
088         * Describe <code>addRight</code> method here.
089         *
090         */
091        public void addRight() {
092            right++;
093            total = right + wrong; // just in case
094        }
095    
096        /**
097         * Describe <code>addWrong</code> method here.
098         *
099         */
100        public void addWrong() {
101            wrong++;
102            total = right + wrong; // just in case
103        }
104    
105        /**
106         * Describe <code>getPercentage</code> method here.
107         *
108         * @return a <code>double</code> value
109         */
110        public double getPercentage() {
111            return ((1.0 * right) / (1.0 * total) * 100.0);
112        }
113    
114        /**
115         * Describe <code>getRight</code> method here.
116         *
117         * @return an <code>int</code> value
118         */
119        public int getRight() {
120            return right;
121        }
122    
123        /**
124         * Describe <code>getTotal</code> method here.
125         *
126         * @return an <code>int</code> value
127         */
128        public int getTotal() {
129            return total;
130        }
131    
132        /**
133         * Describe <code>getWrong</code> method here.
134         *
135         * @return an <code>int</code> value
136         */
137        public int getWrong() {
138            return wrong;
139        }
140    
141        /**
142         * Describe <code>getStart</code> method here.
143         *
144         * @return a <code>Date</code> value
145         */
146        public Date getStart() {
147            return start;
148        }
149    
150    
151        /**
152         * From start of quiz to now, only makes sense for quiz in progress.
153         * @return a <code>String</code> value
154         */
155        public String getElapsedTimeString() {
156            Date x = new Date();
157            long elapsed = x.getTime() - start.getTime();
158            long secs = elapsed / 1000;
159            
160            int hours = (int) (secs / (60 * 60));
161    
162            secs -= hours * (secs / (60 * 60));
163    
164            int mins = (int) (secs / (60));
165    
166            secs -= mins * (secs / (60));
167    
168            if (hours > 0){
169                return hours + ":" + mins + ":" + secs;//^
170            }
171            return mins + ":" + secs;//^
172        }
173    
174        /**
175         * Describe <code>toXML</code> method here.
176         *
177         * @param writer a <code>java.io.Writer</code> value
178         * @exception java.io.IOException if an error occurs
179         */
180        public void toXML(java.io.Writer writer)
181            throws java.io.IOException {
182            if (total == 0) return;
183            writer.write("\t\t<QUIZ DATE=\"" + start.getTime() + "\" RIGHT=\"" + right + "\" WRONG=\"" + wrong + "\" TOTAL=\"" + total + "\"></QUIZ>\n");
184        }  
185    
186    }
187