UserHistoryGUI.java Source code

Java tutorial

Introduction

Here is the source code for UserHistoryGUI.java

Source

import java.util.HashMap;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author JayJung
 */
public class UserHistoryGUI extends javax.swing.JFrame {
    String username;
    MongoConnect mongo = new MongoConnect();
    DB db;

    /**
     * Creates new form UserHistoryGUI
     * @throws ParseException 
     */
    public UserHistoryGUI(String username) throws ParseException {
        this.username = username;
        db = mongo.getDatabase();
        initComponents();
        initScores();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        Title = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        BeginnerLabel = new javax.swing.JLabel();
        EasyLabel = new javax.swing.JLabel();
        MediumLabel = new javax.swing.JLabel();
        HardLabel = new javax.swing.JLabel();
        Total = new javax.swing.JLabel();
        Correct = new javax.swing.JLabel();
        Rate = new javax.swing.JLabel();
        BeginnerTotal = new javax.swing.JLabel();
        BeginnerCorrect = new javax.swing.JLabel();
        BeginnerRate = new javax.swing.JLabel();
        EasyRate = new javax.swing.JLabel();
        EasyCorrect = new javax.swing.JLabel();
        EasyTotal = new javax.swing.JLabel();
        MedTotal = new javax.swing.JLabel();
        MedRate = new javax.swing.JLabel();
        MedCorrect = new javax.swing.JLabel();
        HardTotal = new javax.swing.JLabel();
        HardCorrect = new javax.swing.JLabel();
        HardRate = new javax.swing.JLabel();
        TotalScoreLabel = new javax.swing.JLabel();
        TotalScore = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        Answer = new javax.swing.JLabel();
        CorrectorNo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        Key = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100, Short.MAX_VALUE));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        Title.setText("User Statistics");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap(21, Short.MAX_VALUE).addComponent(Title).addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout
                        .createSequentialGroup().addContainerGap().addComponent(Title,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(26, Short.MAX_VALUE)));

        BeginnerLabel.setText("Beginner Questions:");

        EasyLabel.setText("Easy Questions");

        MediumLabel.setText("Medium Questions");

        HardLabel.setText("Hard Questions");

        Total.setText("Total");

        Correct.setText("Correct");

        Rate.setText("Rate");

        TotalScoreLabel.setText("Total Score:");

        jLabel6.setText("Last Question Answered:");

        jLabel7.setText("Answer");

        jLabel8.setText("Correct?");

        jTextArea2.setEditable(false);
        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel6)
                        .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup().addGroup(
                                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(BeginnerLabel).addComponent(EasyLabel)
                                                .addComponent(MediumLabel).addComponent(HardLabel))
                                        .addGap(37, 37, 37)
                                        .addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(Total).addComponent(BeginnerTotal)
                                                .addComponent(EasyTotal).addComponent(MedTotal)
                                                .addComponent(HardTotal)))
                                .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Answer)))
                                .addGap(48, 48, 48)
                                .addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel8)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(CorrectorNo))
                                        .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(Correct).addComponent(BeginnerCorrect)
                                                .addComponent(EasyCorrect).addComponent(MedCorrect)
                                                .addComponent(HardCorrect)).addGap(44, 44, 44)
                                                .addGroup(jPanel2Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(MedRate).addComponent(Rate)
                                                        .addComponent(BeginnerRate).addComponent(EasyRate)
                                                        .addComponent(HardRate)))))
                        .addGroup(jPanel2Layout.createSequentialGroup().addComponent(TotalScoreLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(TotalScore))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 286,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(32, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Total).addComponent(Correct).addComponent(Rate))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(BeginnerLabel).addComponent(BeginnerTotal)
                                .addComponent(BeginnerCorrect).addComponent(BeginnerRate))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(EasyLabel).addComponent(EasyRate).addComponent(EasyCorrect)
                                .addComponent(EasyTotal))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(MediumLabel).addComponent(MedTotal).addComponent(MedRate)
                                .addComponent(MedCorrect))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(HardLabel).addComponent(HardTotal).addComponent(HardCorrect)
                                .addComponent(HardRate, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18).addComponent(jLabel6).addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                        .addGroup(
                                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(Answer, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addComponent(CorrectorNo))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(TotalScoreLabel).addComponent(TotalScore))
                        .addGap(23, 23, 23)));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Score Values");

        jLabel2.setText("Beginner: 10 Points");

        jLabel3.setText("Easy: 20 Points");

        jLabel4.setText("Medium: 30 Points");

        jLabel5.setText("Hard: 50 Points");

        javax.swing.GroupLayout KeyLayout = new javax.swing.GroupLayout(Key);
        Key.setLayout(KeyLayout);
        KeyLayout.setHorizontalGroup(KeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(KeyLayout.createSequentialGroup().addGap(23, 23, 23).addGroup(KeyLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(KeyLayout.createSequentialGroup()
                                .addGroup(KeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel4).addComponent(jLabel3).addComponent(jLabel5))
                                .addGap(6, 6, 6))
                        .addGroup(KeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(jLabel2)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        KeyLayout.setVerticalGroup(KeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(KeyLayout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addGap(18, 18, 18).addComponent(jLabel2).addGap(18, 18, 18).addComponent(jLabel3)
                        .addGap(18, 18, 18).addComponent(jLabel4).addGap(18, 18, 18).addComponent(jLabel5)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                layout.createSequentialGroup().addGap(149, 149, 149).addComponent(jPanel1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(Key, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(Key, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));

        pack();
    }

    /**
     * @param args the command line arguments
     * @throws ParseException 
     */

    private void initScores() throws ParseException {
        DBCollection collection = db.getCollection("Users");

        DBObject query = new BasicDBObject();
        query.put("Info.Username", username);
        DBObject fields = new BasicDBObject();
        fields.put("Beginner", 1);
        fields.put("Easy", 1);
        fields.put("Medium", 1);
        fields.put("Hard", 1);
        fields.put("Last", 1);

        DBCursor scoreCursor = collection.find(query, fields);
        JSONParser parser = new JSONParser();
        int totalScore = 0;
        int totalAnswered = 0;
        int totalCorrect = 0;

        while (scoreCursor.hasNext()) {
            String json = scoreCursor.next().toString();
            JSONObject jsonFile = (JSONObject) parser.parse(json);

            //beginner scores
            JSONObject beginner = (JSONObject) jsonFile.get("Beginner");
            int beginnerTotal = Integer.parseInt(beginner.get("Total").toString());
            totalAnswered += beginnerTotal;
            int beginnerCorrect = Integer.parseInt(beginner.get("Correct").toString());
            totalCorrect += beginnerCorrect;
            totalScore += beginnerCorrect * 10;
            float beginnerRate = 0.0f;
            if (beginnerTotal != 0) {
                beginnerRate = (float) beginnerCorrect / beginnerTotal;
            }

            //easy scores
            JSONObject easy = (JSONObject) jsonFile.get("Easy");
            int easyTotal = Integer.parseInt(easy.get("Total").toString());
            int easyCorrect = Integer.parseInt(easy.get("Correct").toString());
            totalAnswered += easyTotal;
            totalCorrect += easyCorrect;
            totalScore += easyCorrect * 20;

            float easyRate = 0.0f;
            if (easyTotal != 0) {
                easyRate = (float) easyCorrect / easyTotal;
            }

            //med scores
            JSONObject med = (JSONObject) jsonFile.get("Medium");
            int medTotal = Integer.parseInt(med.get("Total").toString());
            int medCorrect = Integer.parseInt(med.get("Correct").toString());

            totalAnswered += medTotal;
            totalCorrect += medCorrect;
            totalScore += medCorrect * 30;

            float medRate = 0.0f;
            if (medTotal != 0) {
                medRate = (float) medCorrect / medTotal;
            }

            // hard scores
            JSONObject hard = (JSONObject) jsonFile.get("Hard");
            int hardTotal = Integer.parseInt(hard.get("Total").toString());
            int hardCorrect = Integer.parseInt(hard.get("Correct").toString());

            totalAnswered += hardTotal;
            totalCorrect += hardCorrect;
            totalScore += hardCorrect * 50;

            float hardRate = 0.0f;
            if (hardTotal != 0) {
                hardRate = (float) hardCorrect / hardTotal;
            }

            // Question
            JSONObject q = (JSONObject) jsonFile.get("Last");
            String question = q.get("Question").toString();
            String answer = q.get("Answer").toString();
            String correct = q.get("Correct").toString();

            BeginnerTotal.setText("" + beginnerTotal);
            BeginnerCorrect.setText("" + beginnerCorrect);
            BeginnerRate.setText("" + beginnerRate);

            EasyTotal.setText("" + easyTotal);
            EasyCorrect.setText("" + easyCorrect);
            EasyRate.setText("" + easyRate);

            MedTotal.setText("" + medTotal);
            MedCorrect.setText("" + medCorrect);
            MedRate.setText("" + medRate);

            HardTotal.setText("" + hardTotal);
            HardCorrect.setText("" + hardCorrect);
            HardRate.setText("" + hardRate);

            TotalScore.setText("" + totalScore);
            jTextArea2.setText(question);
            Answer.setText(answer);
            CorrectorNo.setText(correct);
        }

    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UserHistoryGUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UserHistoryGUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UserHistoryGUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UserHistoryGUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new UserHistoryGUI("JayJung").setVisible(true);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Answer;
    private javax.swing.JLabel BeginnerCorrect;
    private javax.swing.JLabel BeginnerLabel;
    private javax.swing.JLabel BeginnerRate;
    private javax.swing.JLabel BeginnerTotal;
    private javax.swing.JLabel Correct;
    private javax.swing.JLabel CorrectorNo;
    private javax.swing.JLabel EasyCorrect;
    private javax.swing.JLabel EasyLabel;
    private javax.swing.JLabel EasyRate;
    private javax.swing.JLabel EasyTotal;
    private javax.swing.JLabel HardCorrect;
    private javax.swing.JLabel HardLabel;
    private javax.swing.JLabel HardRate;
    private javax.swing.JLabel HardTotal;
    private javax.swing.JPanel Key;
    private javax.swing.JLabel MedCorrect;
    private javax.swing.JLabel MedRate;
    private javax.swing.JLabel MedTotal;
    private javax.swing.JLabel MediumLabel;
    private javax.swing.JLabel Rate;
    private javax.swing.JLabel Title;
    private javax.swing.JLabel Total;
    private javax.swing.JLabel TotalScore;
    private javax.swing.JLabel TotalScoreLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration            
}