Java tutorial
/* * 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. */ package Interface.Caruser; import Business.Organization.Organization; import Business.UserAccount.UserAccount; import Business.WorkQueue.CarOwnerWorkRequest; import Business.WorkQueue.CarUserWorkRequest; import Business.WorkQueue.WorkRequest; import Interface.CarOwner.CarOwnerMenuPageJPanel; import java.text.Format; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.table.DefaultTableModel; /**********************************/ import com.twilio.sdk.TwilioRestClient; import com.twilio.sdk.TwilioRestException; import com.twilio.sdk.resource.factory.MessageFactory; import com.twilio.sdk.resource.instance.Message; import java.awt.CardLayout; import java.awt.Color; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; /** * * @author Vivek */ public class Pendingrequest extends javax.swing.JPanel { /** * Creates new form Pendingrequest */ Organization organization; UserAccount useraccount; JPanel container; public static final String ACCOUNT_SID = "AC7e65aefcb879488ff579e7d40157dbec"; public static final String AUTH_TOKEN = "6205a0d9e739b3f36c441e8d4ce27fb7"; public Pendingrequest(JPanel container, UserAccount useraccount, Organization organization) { initComponents(); this.organization = organization; this.useraccount = useraccount; this.container = container; populatetable(); populatecartypecombobox(); } /** * 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() { jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); incomingjTable = new javax.swing.JTable(); jLabel2 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); cartypesjComboBox = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); commentjTextField = new javax.swing.JTextField(); submitjButton = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel1.setText("Incoming Request"); incomingjTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); incomingjTable.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N incomingjTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Car", "Sender", "Receiver", "Source", "Destination", "Request Date", "Status" })); jScrollPane1.setViewportView(incomingjTable); jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/incoming.jpg"))); // NOI18N jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel6.setText("Car Type"); cartypesjComboBox.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N cartypesjComboBox.setForeground(new java.awt.Color(255, 255, 255)); cartypesjComboBox.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cartypesjComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cartypesjComboBoxActionPerformed(evt); } }); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel3.setText("Comment"); submitjButton.setBackground(new java.awt.Color(255, 255, 255)); submitjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Submitblackbutton.png"))); // NOI18N submitjButton.setBorder(null); submitjButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { submitjButtonActionPerformed(evt); } }); jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/back.png"))); // NOI18N jButton2.setBorder(null); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(231, 231, 231) .addComponent(submitjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(51, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGap(86, 86, 86) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(commentjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cartypesjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(229, 229, 229)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 641, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(38, 38, 38))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(23, 23, 23) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(38, 38, 38))) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cartypesjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(commentjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(submitjButton)) .addGap(26, 26, 26))); }// </editor-fold>//GEN-END:initComponents private void cartypesjComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cartypesjComboBoxActionPerformed HashMap<String, Integer> cartype = new HashMap<String, Integer>(); cartype.put("Nisaan Rogue", 6); cartype.put("Nisaan Chevrolet", 4); cartype.put("Versa", 2); cartype.put("Ford Fiesta", 4); cartype.put("Toyota corolla", 5); cartype.put("Honda Civic", 3); cartype.put("GMC Canyon", 2); cartype.put("Chevrolet silverado", 4); }//GEN-LAST:event_cartypesjComboBoxActionPerformed public void sendsms(UserAccount user, Date d, String messagea) { try { TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("Body", "Hey " + user.toString() + ", You cab has been booked for " + d + ". " + messagea)); params.add(new BasicNameValuePair("To", "+16177128475")); params.add(new BasicNameValuePair("From", "+16179350189")); MessageFactory messageFactory = client.getAccount().getMessageFactory(); Message message = messageFactory.create(params); } catch (TwilioRestException e) { } } private void submitjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitjButtonActionPerformed Object strr = (Object) cartypesjComboBox.getSelectedItem(); int selectedRow = incomingjTable.getSelectedRow(); if (selectedRow < 0) { JOptionPane.showMessageDialog(this, "Please select a row.", "Error", JOptionPane.ERROR_MESSAGE); return; } WorkRequest request = (WorkRequest) incomingjTable.getValueAt(selectedRow, 0); if (!request.getStatus().equals("Completed")) { request.setcar(strr.toString()); request.setReceiver(useraccount); request.setStatus("Completed"); request.getSender().setTrips(true); request.setcomment(commentjTextField.getText()); useraccount.setTrips(true); useraccount.setPayment(useraccount.getPayment() + ((CarOwnerWorkRequest) request).getRideamount()); request.getSender() .setPayment(request.getSender().getPayment() - ((CarOwnerWorkRequest) request).getRideamount()); sendsms(request.getSender(), request.getRequestDate(), strr.toString() + " is coming to pick you up."); } else { JOptionPane.showMessageDialog(this, "Appointment is already booked", "Error", JOptionPane.ERROR_MESSAGE); } commentjTextField.setText(""); populatetable(); }//GEN-LAST:event_submitjButtonActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed container.remove(this); CardLayout layout = (CardLayout) container.getLayout(); layout.previous(container); }//GEN-LAST:event_jButton2ActionPerformed public void populatetable() { DefaultTableModel model = (DefaultTableModel) incomingjTable.getModel(); model.setRowCount(0); for (WorkRequest request : organization.getWorkQueue().getWorkRequestList()) { Object[] row = new Object[7]; row[0] = request; row[1] = request.getSender(); row[2] = request.getReceiver(); row[3] = request.getSource(); row[4] = request.getDestination(); Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); row[5] = formatter.format(request.getRequestDate()); row[6] = request.getStatus(); model.addRow(row); } incomingjTable.setSelectionBackground(Color.getHSBColor(0.2F, 0.1F, 0.2F)); incomingjTable.setSelectionForeground(Color.white); } public void populatecartypecombobox() { HashMap<String, Integer> cartype = new HashMap<String, Integer>(); cartype.put("Nisaan Rogue", 6); cartype.put("Nisaan Chevrolet", 4); cartype.put("Versa", 2); cartype.put("Ford Fiesta", 4); cartype.put("Toyota corolla", 5); cartype.put("Honda Civic", 3); cartype.put("GMC Canyon", 2); cartype.put("Chevrolet silverado", 4); cartypesjComboBox.removeAllItems(); Set set = cartype.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mentry = (Map.Entry) iterator.next(); cartypesjComboBox.addItem(mentry.getKey()); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox cartypesjComboBox; private javax.swing.JTextField commentjTextField; private javax.swing.JTable incomingjTable; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton submitjButton; // End of variables declaration//GEN-END:variables }