mardi 5 janvier 2016

Give a checkbox an integer value and store in JavaDB (Netbeans)

I am currently working on a small program that allows the ordering of items and the total cost to be stored in a JavaDB.

I have managed to successfully store integer values into the database but the cost amount for the items are not correct. It seems like when the check box is selected it is counting that as a value on top of the set value. Would anyone be able to take a look and see where I am going wrong?

Any help would be greatly appreciated!

Screenshot: http://ift.tt/1PKJ0cj

Code:

 package gui;

 import connection.DBConnect;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import javax.swing.JOptionPane;

 public class Order extends javax.swing.JFrame {

 Connection con;
 Statement stmt;
 ResultSet rs;
 int curRow = 0;

 int fanta = 1;
 int crisps = 1;
 int beer = 2;
 int wine = 3;
 int water = 0;

/**
 * Creates new form Order
 */
public Order() {
    initComponents();
    DoConnect();
}

    public void DoConnect() {

    try {

    // Make a connection to the database
    String host = "jdbc:derby://localhost:1527/Airplane";
    String uName = "staff";
    String uPass= "pass";    
    Connection con = DriverManager.getConnection( host, uName, uPass );

    //Execute SQL and load the records
    stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
    String SQL = "SELECT * FROM Orders";
    rs = stmt.executeQuery( SQL );         
}              
catch ( SQLException err ) {
JOptionPane.showMessageDialog(this, err.getMessage());    
}       
}

/**
 * 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">                          
private void initComponents() {

    btnMenu = new javax.swing.JButton();
    btnOrder = new javax.swing.JButton();
    LblSelectSeat = new javax.swing.JLabel();
    btnFanta = new javax.swing.JCheckBox();
    jLabel2 = new javax.swing.JLabel();
    btnCrisps = new javax.swing.JCheckBox();
    jLabel3 = new javax.swing.JLabel();
    btnBeer = new javax.swing.JCheckBox();
    jLabel4 = new javax.swing.JLabel();
    btnWine = new javax.swing.JCheckBox();
    jLabel5 = new javax.swing.JLabel();
    btnWater = new javax.swing.JCheckBox();
    jLabel6 = new javax.swing.JLabel();
    LblSelectItem = new javax.swing.JLabel();
    btnSeatNo = new javax.swing.JComboBox();
    btnRow = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    btnMenu.setText("Return to Menu");
    btnMenu.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnMenuActionPerformed(evt);
        }
    });

    btnOrder.setText("Order Items");
    btnOrder.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOrderActionPerformed(evt);
        }
    });

    LblSelectSeat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    LblSelectSeat.setText("Select your row and seat");

    btnFanta.setText(" Can of Fanta           |");

    jLabel2.setText(" Cost: £1");

    btnCrisps.setText(" Packet of Crisps     |");
    btnCrisps.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCrispsActionPerformed(evt);
        }
    });

    jLabel3.setText(" Cost: £1");

    btnBeer.setText(" Bottle of Beer          |");
    btnBeer.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnBeerActionPerformed(evt);
        }
    });

    jLabel4.setText(" Cost: £2");

    btnWine.setText(" Glass of Wine          |");
    btnWine.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnWineActionPerformed(evt);
        }
    });

    jLabel5.setText(" Cost: £3");

    btnWater.setText(" Bottle of Water       |");
    btnWater.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnWaterActionPerformed(evt);
        }
    });

    jLabel6.setText(" Cost: Free");

    LblSelectItem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    LblSelectItem.setText("Please select the items you wish to order");

    btnSeatNo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" }));
    btnSeatNo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSeatNoActionPerformed(evt);
        }
    });

    btnRow.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "B", "C", "D", "E", "F" }));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(LblSelectItem, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(151, 151, 151))
        .addGroup(layout.createSequentialGroup()
            .addGap(186, 186, 186)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(btnSeatNo, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(38, 38, 38)
                    .addComponent(btnRow, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnCrisps, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnFanta)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnBeer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnWine, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(btnWater, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel6)))
                    .addGap(181, 181, 181))))
        .addGroup(layout.createSequentialGroup()
            .addGap(217, 217, 217)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(btnOrder))
                .addComponent(btnMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(LblSelectSeat)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(LblSelectItem, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnFanta)
                .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnCrisps)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnBeer)
                .addComponent(jLabel4))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnWine)
                .addComponent(jLabel5))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnWater)
                .addComponent(jLabel6))
            .addGap(41, 41, 41)
            .addComponent(LblSelectSeat, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSeatNo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnRow, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
            .addComponent(btnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(btnMenu)
            .addGap(22, 22, 22))
    );

    pack();
}// </editor-fold>                        

private void btnMenuActionPerformed(java.awt.event.ActionEvent evt) {                                        
    this.setVisible(false); // Make this frame not visible
    new Main().setVisible(true); // Show new frame once button is clicked
}                                       

private void btnOrderActionPerformed(java.awt.event.ActionEvent evt) {                                         
    boolean itemfanta = btnFanta.isSelected();
    boolean itemcrisps = btnCrisps.isSelected();
    boolean itembeer = btnBeer.isSelected();
    boolean itemwine = btnWine.isSelected();
    boolean itemwater = btnWater.isSelected();
    int seatrow = Integer.parseInt((String)btnSeatNo.getSelectedItem());
    String seat = (String)btnRow.getSelectedItem();    



    try {
        rs.moveToInsertRow();

        rs.updateBoolean("Fanta", itemfanta);
        rs.updateBoolean("Crisps", itemcrisps);
        rs.updateBoolean("Beer", itembeer);
        rs.updateBoolean("Wine", itemwine);
        rs.updateBoolean("Water", itemwater);
        rs.updateInt("SeatRow", seatrow); 
        rs.updateString( "Seat", seat);

        if(btnFanta.isSelected()) fanta++;
        if(btnCrisps.isSelected()) crisps++;
        if(btnBeer.isSelected()) beer++;
        if(btnWine.isSelected()) wine++;
        if(btnWater.isSelected()) water++;
        rs.updateInt("TotalCost", fanta + crisps + beer + wine + water);


        rs.insertRow();

        String SQL = "SELECT * FROM Orders";
        rs = stmt.executeQuery( SQL );

        rs.next();

        JOptionPane.showMessageDialog(null, "Thank you for your order. You will receive your items from a member of staff shortly.", "Order Accepted", JOptionPane.INFORMATION_MESSAGE);
    }
    catch (SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());   
    }
}                                        

private void btnCrispsActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

private void btnBeerActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void btnWineActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void btnWaterActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void btnSeatNoActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

/**
 * @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://ift.tt/1cNmMj1 
     */
    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(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Order.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 Order().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel LblSelectItem;
private javax.swing.JLabel LblSelectSeat;
private javax.swing.JCheckBox btnBeer;
private javax.swing.JCheckBox btnCrisps;
private javax.swing.JCheckBox btnFanta;
private javax.swing.JButton btnMenu;
private javax.swing.JButton btnOrder;
private javax.swing.JComboBox btnRow;
private javax.swing.JComboBox btnSeatNo;
private javax.swing.JCheckBox btnWater;
private javax.swing.JCheckBox btnWine;
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;
// End of variables declaration                   
}




Aucun commentaire:

Enregistrer un commentaire