Java tutorial
package govt_import_export; /* * 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 Nitesh */ import com.mongodb.BasicDBObject; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.GroupCommand; import com.mongodb.MapReduceCommand; import com.mongodb.MapReduceOutput; import com.mongodb.MongoException; import com.mongodb.MongoClient; import static java.lang.Math.pow; import java.text.DecimalFormat; import java.text.NumberFormat; import javax.swing.JLabel; public class export extends javax.swing.JFrame { /** * Creates new form export */ static String model_selected; static String manufac; static String r; String z; int cost; long total_cost; String db_rate, db_balance; long unitsXcost; long db_rate_long; double newbalance; double newrate, oldrate; double ratio; NumberFormat nf = new DecimalFormat("############"); public export(String item1, String item2) { initComponents(); model_selected = item1; manufac = item2; MongoClient mongo = null; try { mongo = new MongoClient("localhost", 27017); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } DB db = mongo.getDB("automobile_expo"); DBCollection table = db.getCollection(manufac); BasicDBObject doc = new BasicDBObject(); BasicDBObject doc1 = new BasicDBObject(); String s = new String(); doc.put("model_name", model_selected); doc1.append("_id", 0); DBCursor c = table.find(doc, doc1); //while(c.hasNext()) { DBObject doc2 = c.next(); //System.out.println(doc2.toString()); jLabel6.setText("<html>Model " + (doc2.get("model_name").toString()) + "<br> date " + (doc2.get("date").toString()) + " <br>EngineType" + (doc2.get("EngineType").toString()) + "<br>EngineDisplacement" + (doc2.get("EngineDisplacement").toString()) + "<br>Maximum_torque" + (doc2.get("Maximum_torque").toString()) + "<br>Noof_cylinders" + (doc2.get("Noof_cylinders").toString()) + "<br>Seating_Capacity" + (doc2.get("Seating_Capacity").toString()) + "<br>Length" + (doc2.get("Length").toString()) + "<br>Width" + (doc2.get("Width").toString()) + "<br>Height" + (doc2.get("Height").toString()) + "<br>Cost" + (doc2.get("Cost").toString()) + "</html>"); //jTextArea1.setText(s); } String[] split = s.split(","); //jTextArea1.setText(s); doc.put("model_name", model_selected); BasicDBObject sp = new BasicDBObject(); sp.put("Cost", 1); sp.put("_id", 0); DBCursor cursor = table.find(doc, sp); while (cursor.hasNext()) z = cursor.next().toString(); z = z.substring(z.indexOf("st\" : \"") + 7); z = z.substring(0, z.indexOf("INR")); String s2; //while(cursor.hasNext()) //BasicDBObject h=(BasicDBObject)cursor.next(); //s2=h.get("Cost").toString(); //insert cost = Integer.parseInt(z); total_cost = 1000 * cost; String st; st = String.valueOf(total_cost); jTextField1.setText(st); //jTextField1.setText(z); } /** * 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() { jPanel1 = new javax.swing.JPanel(); jLabel6 = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jComboBox2 = new javax.swing.JComboBox(); jComboBox3 = new javax.swing.JComboBox(); jLabel4 = new javax.swing.JLabel(); jComboBox4 = new javax.swing.JComboBox(); jLabel5 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Details of car")); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel6) .addContainerGap(346, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jLabel6) .addContainerGap(74, Short.MAX_VALUE))); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1000", "2000", "5000", "10000" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jLabel1.setText("Choose how many units u want to export"); jLabel2.setText("Export to"); jButton2.setText("Export now"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel3.setText("month"); jButton3.setText("Show total units"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Asia Pacific", "Australia", "Europe", "North-Central America", "South America", "Russia" })); jComboBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox2ActionPerformed(evt); } }); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" })); jLabel4.setText("year"); jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2012", "2013", "2014" })); jLabel5.setText("Total Cost"); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(243, 243, 243)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout .createSequentialGroup().addGap(25, 25, 25) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30).addComponent(jLabel4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(106, 106, 106).addComponent(jLabel5) .addGap(18, 18, 18).addComponent(jTextField1)))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addContainerGap() .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2))) .addGap(24, 24, 24))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(14, 14, 14) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2))) .addGroup(jPanel2Layout.createSequentialGroup().addGap(3, 3, 3) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(18, 18, 18) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3).addComponent(jButton2)) .addContainerGap())); jButton1.setText("go back"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(14, 14, 14).addComponent(jButton1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGap(28, 28, 28) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jButton1)) .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) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(138, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: autoexpo a = new autoexpo(); a.setVisible(true); this.setVisible(false); }//GEN-LAST:event_jButton1ActionPerformed private void sort_cars() { try { MongoClient mongo = new MongoClient("localhost", 27017); //get database DB db = mongo.getDB("AUTOMOBILEXPO"); String batches = (String) jComboBox1.getSelectedItem(); //get collections DBCollection table = db.getCollection(manufac); BasicDBObject search = new BasicDBObject(); BasicDBObject sp = new BasicDBObject(); //sp.put("Units",1); sp.put("_id", 0); search.put("model", model_selected); DBCursor cursor = table.find(search, sp).sort(new BasicDBObject("Units", 1)); while (cursor.hasNext()) { r = cursor.next().toString(); System.out.println(r); } /*String map="function(){"+"String model;"+"model=col;"+"emit(model,{Units:this.Units});}"; String reduce="function(key,values){"+"int sum=0;"+"values.forEach(function(doc){" + "sum += this.Units; " + "}); " +"return {table: sum};} "; MapReduceCommand cm = new MapReduceCommand(table, map,reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = table.mapReduce(cm); System.out.println("Mapreduce results"); for (DBObject o : out.results()) { System.out.println(o.toString()); }*/ } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: MongoClient mongo = null; try { mongo = new MongoClient("localhost", 27017); //get database } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } DB db = mongo.getDB("AUTOMOBILEXPO"); //DB db2=mongo.getDB("automobile_expo"); String batches = (String) jComboBox1.getSelectedItem(); int unit, month, year; unit = Integer.parseInt(batches); //get collections DBCollection table = db.getCollection(manufac); //DBCollection table1=db2.getCollection(db1); BasicDBObject document1 = new BasicDBObject(); String st = (String) jComboBox2.getSelectedItem(); String st1 = (String) jComboBox3.getSelectedItem(); String st2 = (String) jComboBox4.getSelectedItem(); month = Integer.parseInt(st1); year = Integer.parseInt(st2); cost = cost * unit; //jTextField1.setText(z); DB db0 = mongo.getDB("admindata"); DBCollection collection0 = db0.getCollection("admin"); DBCursor c = collection0.find(); //yes DBObject o; //DBCursor c=collection0.find(); //yes while (c.hasNext()) { o = c.next(); if (o.get("rate") != null) { db_rate = o.get("rate").toString(); } } c = collection0.find(); while (c.hasNext()) { o = c.next(); if (o.get("balance") != null) { db_balance = nf.format(o.get("balance")); } } ratio = ((double) cost) / Double.parseDouble(db_balance); ratio = -ratio * .1; newbalance = Long.parseLong(db_balance) - cost; newrate = Double.parseDouble(db_rate) * pow(Math.E, ratio); System.out.println("'newrate'" + newrate); //System.out.println(newbalance); String q = nf.format(newbalance); //System.out.println(q); //--------------------setting the updated values in the db---------------------- BasicDBObject searchupdate = new BasicDBObject(); searchupdate.append("rate", Double.parseDouble(db_rate)); //finds the document(s) where "date":last_activity BasicDBObject update = new BasicDBObject(); update.append("$set", new BasicDBObject("rate", /*Double.toString(newrate)*/newrate)); collection0.update(searchupdate, update); //----- BasicDBObject searchupdate1 = new BasicDBObject(); searchupdate1.append("balance", Long.parseLong(db_balance)); BasicDBObject update1 = new BasicDBObject(); update1.append("$set", new BasicDBObject("balance", Long.parseLong(q))); collection0.update(searchupdate1, update1); //DBObject o; BasicDBObject document = new BasicDBObject(); BasicDBObject prev = new BasicDBObject(); document.append("manufacturer", manufac); document.append("model", model_selected); document.append("country", st); document.append("Units", unit); document.append("Cost", cost); document.append("Month", month); document.append("Year", year); table.insert(document); //long x=table.count(); //find and display //System.out.println(x); GroupCommand cmd = new GroupCommand(table, null, new BasicDBObject("model", model_selected), new BasicDBObject("Units", 0), "function(document,prev){prev.Units=prev.Units+this.Units;}", null); o = table.group(cmd); //System.out.println(o); sort_cars(); javax.swing.JOptionPane.showMessageDialog(export.this, "Waiting for acceptance"); st = String.valueOf(total_cost); jTextField1.setText(st); System.out.println("donee"); }//GEN-LAST:event_jButton2ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: String st = (String) jComboBox1.getSelectedItem(); int i = Integer.parseInt(st); total_cost = i * cost; st = String.valueOf(total_cost); jTextField1.setText(st); }//GEN-LAST:event_jComboBox1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: JLabel p = new JLabel(); MongoClient mongo = null; try { mongo = new MongoClient("localhost", 27017); //get database } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } DB db = mongo.getDB("AUTOMOBILEXPO"); DBCollection table = db.getCollection(manufac); String map = "function () {" + "emit(this.model,this.Units);" + "}"; String reduce = "function (key, values) { " + "return Array.sum(values)}"; MapReduceCommand cmd = new MapReduceCommand(table, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = table.mapReduce(cmd); for (DBObject o : out.results()) { System.out.println(o.toString()); } }//GEN-LAST:event_jButton3ActionPerformed private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jComboBox2ActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed /** * @param args the command line arguments */ 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(export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(export.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() { new export(model_selected, manufac).setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JComboBox jComboBox3; private javax.swing.JComboBox jComboBox4; 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.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }