Netbeans - Una resta

 
Vista:
Imágen de perfil de Carlos

Una resta

Publicado por Carlos (1 intervención) el 05/06/2017 15:56:36
Hola a todos , la verdad es que mi problema quizás sea simple para ustedes, pero recién empiezo en el área de programación soy estudiante de 2do ciclo. eh creado un jframe que son ordenes o pedidos todo lo eh hecho con jComboBox y con botones para q sea mas facil para el usuario, la cosa es q al momento, el momento esta todo bien y al momento de sumar los precios tbn, pero ahora mi probema es q cuando quiero quitar un elemento de jTable, quiero q se valla restando el precio del q se va seleccionando lo eh hecho de una mandera pero me salta error al momento de compilarlo, si me pudieran ayudar les agradeceria mucho.
les dejo el codigo completo



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package polleria;
 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
 
/**
 *
 * @author Carlos Zr
 */
public class orden extends javax.swing.JFrame {
DefaultTableModel t = new DefaultTableModel();
String Dato[]=new String[4];
int to=t.getRowCount();
        double re=0;
        int c=0;
 
 
    /**
     * Creates new form orden
     */
    public orden() {
        initComponents();
        t.addColumn("Cantidad");
        t.addColumn("Tipo");
        t.addColumn("Eleccion");
        t.addColumn("Precio");
        labelIndex.setText("0");
 
        this.tabla.setModel(t);
 
    }
    private void guardaTabla(){
        try {
 
            String sucursalesCSVFile = "src/Ordenes/DatosTabla.txt";
            BufferedWriter bfw = new BufferedWriter(new FileWriter(sucursalesCSVFile ));
 
            for (int i = 0 ; i < t.getRowCount(); i++) //realiza un barrido por filas.
            {
                for(int j = 0 ; j < t.getColumnCount();j++) //realiza un barrido por columnas.
                {
                    bfw.write((String)(t.getValueAt(i,j)));
                    if (j < t.getColumnCount() -1) { //agrega separador "," si no es el ultimo elemento de la fila.
                        bfw.write(",");
                    }
                }
                bfw.newLine(); //inserta nueva linea.
            }
 
            bfw.close(); //cierra archivo!
            System.out.println("El archivo fue salvado correctamente!");
        } catch (IOException e) {
            System.out.println("ERROR: Ocurrio un problema al salvar el archivo!" + e.getMessage());
        }
    }
 
 
    private void octavoActionPerformed(java.awt.event.ActionEvent evt) {
 
        double precio=7.00;
        String cantidad ="1/8",tipo="Pollo";
 
        String Eleccion=(String) octavo.getSelectedItem();
        int eleccion= octavo.getSelectedIndex();
        switch(eleccion) {
            case 0:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;
            case 1:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;
            case 2:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;
            case 3:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;}
        do
         try{
             int f=c++;
             double n1=Double.parseDouble(t.getValueAt(f,3).toString());
             double n2 =Double.parseDouble(labelIndex.getText());
                re=n1+n2;
             labelIndex.setText(String.valueOf(re));
         } catch (NumberFormatException e){
         }
        while(c<to);
 
    }
 
    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
        double precio=13.00;
        String cantidad ="1/4",tipo="Pollo";
        String Eleccion=(String) octavo.getSelectedItem();
        int eleccion= octavo.getSelectedIndex();
        switch(eleccion) {
            case 0:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;
            case 1:
            Dato[0]=cantidad;
            Dato[1]=tipo;
            Dato[2]=Eleccion;
            Dato[3]=String.valueOf(precio);
            t.addRow(Dato);
            break;}
            do
            try{
                    int f=c++;
                    double n1=Double.parseDouble(t.getValueAt(f,3).toString());
                    double n2 =Double.parseDouble(labelIndex.getText());
                    re=n1+n2;
                    labelIndex.setText(String.valueOf(re));
                } catch (NumberFormatException e){
                }
            while(c<to);
    }
 
    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {
            guardaTabla();        // TODO add your handling code here:
    }
 
    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {
int eliminar=tabla.getSelectedRow();
if(eliminar>=0){
    t.removeRow(eliminar);
    try{int f=c++;
                    double n1=Double.parseDouble(t.getValueAt(f,3).toString());
                    double n2 =Double.parseDouble(labelIndex.getText());
 
                        n2=re-n1;
                    labelIndex.setText(String.valueOf(n2));
                } catch (NumberFormatException e){
                }
}else {JOptionPane.showMessageDialog(null,"Seleccione Pedido a eliminar");
 
}
    }
 
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {
int eliminart=tabla.getRowCount();
for(int i=eliminart-1;i>=0;i--){
t.removeRow(i);
re=0;
labelIndex.setText(String.valueOf(re));
}
    }
 
    /**
     * @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(orden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(orden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(orden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(orden.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 orden().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JComboBox<String> jComboBox5;
    private javax.swing.JComboBox<String> jComboBox6;
    private javax.swing.JComboBox<String> jComboBox7;
    private javax.swing.JComboBox<String> jComboBox8;
    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.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelIndex;
    private javax.swing.JComboBox<String> octavo;
    private javax.swing.JTable tabla;
    // End of variables declaration
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder