Semana 14
Materia: Taller de Programación Orientada a Objetos
Hora: Jueves M1-M2
setTitle("Nutrilife"); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nutrilife/gui/logo_1201.png"))); // NOI18N jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 48)); // NOI18N jLabel2.setText("Bienvenido a Nutrilife! "); jButton1.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton1.setText("NUEVO PACIENTE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { PacienteGUI form = new PacienteGUI(); form.setVisible(true); }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { PacienteGUI form = new PacienteGUI(); form.setVisible(true); this.dispose(); }
private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Nutrilife"); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nutrilife/gui/logo_1201.png"))); // NOI18N jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 48)); // NOI18N jLabel2.setText("Bienvenido a Nutrilife! "); jButton1.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton1.setText("NUEVO PACIENTE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton2.setText("MODIFICAR PACIENTE"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton3.setText("BORRAR PACIENTE"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton4.setText("VER PACIENTES"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton5.setText("NUEVA DIETA"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton6.setText("MODIFICAR DIETA"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton7.setText("BORRAR DIETA"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton8.setFont(new java.awt.Font("DejaVu Sans Light", 1, 18)); jButton8.setText("VER DIETAS"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup() .addGap(92, 92, 92) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(171, 171, 171) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE) .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE) .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE) .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)) .addGap(61, 61, 61))) .addGap(47, 47, 47)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(jLabel1) .addGap(63, 63, 63)) .addGroup(layout.createSequentialGroup() .addContainerGap(129, Short.MAX_VALUE) .addComponent(jLabel2) .addGap(32, 32, 32))) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(131, 131, 131)) ); pack(); }//
ppublic class Carbohidrato { double lecheDescremada; double lecheSemidescremada; double lecheEntera; double lecheConAzucar; double fruta; double verdura; double azucarSinGrasa; double azucarConGrasa; double leguminosa; double cerealSinGrasa; double cerealConGrasa; double alimentoMuyMagro; double alimentoMagro; double alimentoSemigraso; double alimentoGraso; double aceiteSinProteina; double aceiteConProteina; double subtotal1; double subtotal2; double subtotal3; double subtotal4; int intercambioLecheDescremada = 1; int intercambioLecheSemidescremada = 0; int intercambioLecheEntera = 0; int intercambioLecheConAzucar = 0; int intercambioFruta = 3; int intercambioVerdura = 3; int intercambioAzucarSinGrasa = 2; int intercambioAzucarConGrasa = 1; int intercambioLeguminosa = 3; int intercambioCerealConGrasa = 1; int intercambioCerealSinGrasa = 2; int intercambioAlimentoMuyMagro = 3; int intercambioAlimentoMagro = 1; int intercambioAlimentoSemigraso = 0; int intercambioAlimentoGraso = 0; int intercambioAceiteSinProteina = 1; int intercambioAceiteConProteina = 0; //Metodos set public void setLecheDescremada() { lecheDescremada = (intercambioLecheDescremada * 12); } public void setLecheSemidescremada() { lecheSemidescremada = (intercambioLecheSemidescremada * 12); } public void setLecheEntera() { lecheEntera = (intercambioLecheEntera * 12); } public void setLecheConAzucar() { lecheConAzucar = (intercambioLecheConAzucar * 30); } public void setFruta() { fruta = (intercambioFruta * 15); } public void setVerdura() { verdura = (intercambioVerdura * 4); } public void setAzucarSinGrasa() { azucarSinGrasa = (intercambioAzucarSinGrasa * 10); } public void setAzucarConGrasa() { azucarConGrasa = (intercambioAzucarConGrasa * 10); } public void setLeguminosa() { leguminosa = (intercambioLeguminosa * 20); } public void setCerealSinGrasa() { cerealSinGrasa = (intercambioCerealSinGrasa * 15); } public void setCerealConGrasa() { cerealConGrasa = (intercambioCerealConGrasa * 15); } public void setAlimentoMuyMagro() { alimentoMuyMagro = (intercambioAlimentoMuyMagro * 0); } public void setAlimentoMagro() { alimentoMagro = (intercambioAlimentoMagro * 0); } public void setAlimentoSemigraso() { alimentoSemigraso = (intercambioAlimentoSemigraso * 0); } public void setAlimentoGraso() { alimentoGraso = (intercambioAlimentoGraso * 0); } public void setAceiteSinProteina() { aceiteSinProteina = (intercambioAceiteSinProteina * 0); } public void setAceiteConProteina() { aceiteConProteina = (intercambioAceiteConProteina * 3); } public double setSubtotal1() { subtotal1 = ( lecheDescremada + lecheSemidescremada + lecheEntera + lecheConAzucar + fruta + verdura + azucarSinGrasa + azucarConGrasa + leguminosa ); return subtotal1; } public double setSubtotal2() { subtotal2 = ( cerealSinGrasa + cerealConGrasa); return subtotal2; } public double setSubtotal3() { subtotal3 = ( alimentoMuyMagro + alimentoMagro + alimentoSemigraso + alimentoGraso); return subtotal3; } public double setSubtotal4() { subtotal4 = ( aceiteSinProteina + aceiteConProteina); return subtotal4; } public void setClaseCarbohidrato() { setLecheDescremada(); setLecheSemidescremada(); setLecheEntera(); setLecheConAzucar(); setFruta(); setVerdura(); setAzucarSinGrasa(); setAzucarConGrasa(); setLeguminosa(); setCerealSinGrasa(); setCerealConGrasa(); setAlimentoMuyMagro(); setAlimentoMagro(); setAlimentoSemigraso(); setAlimentoGraso(); setAceiteSinProteina(); setAceiteConProteina(); double subtotal1 = setSubtotal1(); double subtotal2 = setSubtotal2(); double subtotal3 = setSubtotal3(); double subtotal4 = setSubtotal4(); } }
package proyecto; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner;
public class Prueba extends TestCase { protected Carbohidrato c; protected void setUp() { this.c = new Carbohidrato(); return; } public static Test suite() { return new TestSuite(Prueba.class); }
public void test1() { double expected = 159.0; c.setClaseCarbohidrato(); double actual = c.setSubtotal1(); assertEquals(expected,actual); return; } public void test2() { double expected = 40.0; c.setClaseCarbohidrato(); double actual = c.setSubtotal2(); assertEquals(expected, actual); return; } public void test3() { double expected = 0.0; c.setClaseCarbohidrato(); double actual = c.setSubtotal3(); assertEquals(expected,actual); return; }
public void test2() { double expected = 40.0; c.setClaseCarbohidrato(); double actual = c.setSubtotal2(); assertEquals(expected, actual); return; }
package nutrilife.gui; import nutrilife.dieta.*; import java.util.InputMismatchException; /** * * @author Carmen Suarez */ public class Main { public static void main(String[] args) { //Variable that stores that will stream the input from console Dieta D = new Dieta(); boolean control = true; do { try { InputDieta.input(D); control = false; } catch (InputMismatchException inputMismatchException) { inputMismatchException.printStackTrace(); System.out.println("Vuelve a meter los datos"); } }while( control ); } }
package nutrilife.gui; import nutrilife.dieta.*; import java.util.*; /** * * @author Carmen Suarez */ public class InputDieta { static void input(Dieta D) throws InputMismatchException { Scanner scan = new Scanner(System.in); System.out.println("Dame el numero de energia"); D.setEnergia(scan.nextDouble()); System.out.println("Energia: " + D.getEnergia()); boolean c = true; //Variable de control //Entrar porcentajes do { try { System.out.print("Dame el porcenaje de carbohidratos: "); D.setPorcentajeCarbohidrato(scan.nextDouble()); System.out.println("Porcentaje de carbohidratos: " + D.getPorcentajeCarbohidrato()); System.out.print("Dame el porcenaje de proteinas: "); D.setPorcentajeProteina(scan.nextDouble()); System.out.println("Porcentaje de proteinas: " + D.getPorcentajeProteina()); System.out.print("Dame el porcenaje de lipidos: "); D.setPorcentajeLipido(scan.nextDouble()); System.out.println("Porcentaje de lipidos: " + D.getPorcentajeLipido()); D.setPorcentajeTotal(); // Calcula el valor para la variable porcentajeTotal if( D.getPorcentajeTotal() != 100) { throw new MyException(); } c = false; D.setCarbohidratos(); // Calcula el valor para la variable carbohidrato D.setProteina(); // Calcula el valor para la variable proteina D.setLipido(); // Calcula el valor para la variable lipido } catch(MyException myException) { myException.getMessage(); myException.printStackTrace(); } }while(c); // Termina entrada de porcentajes //Entrada de Intercambios //intercambios de grupo: Leche System.out.print("Dame el intercambio de leche descremada: "); D.setIntercambioLecheDescremada(scan.nextInt()); System.out.println("Intercambios para leche descremada: " + D.getIntercambioLecheDescremada()); System.out.print("Dame el intercambio de leche semidescremada: "); D.setIntercambioLecheDescremada(scan.nextInt()); System.out.println("Intercambios para leche semidescremada: " + D.getIntercambioLecheSemidescremada()); System.out.print("Dame el intercambio de leche entera: "); D.setIntercambioLecheEntera(scan.nextInt()); System.out.println("Intercambios para leche entera: " + D.getIntercambioLecheEntera()); System.out.print("Dame el intercambio de leche con azucar: "); D.setIntercambioLecheConAzucar(scan.nextInt()); System.out.println("Intercambios para leche con azucar: " + D.getIntercambioLecheConAzucar()); //Intercambios de grupo: Fruta System.out.print("Dame el intercambio de la fruta: "); D.setIntercambioFruta(scan.nextInt()); System.out.println("Intercambios para la frutar: " + D.getIntercambioFruta()); //Intercambios de grupo: Verdura System.out.print("Dame el intercambio de la verdura: "); D.setIntercambioVerdura(scan.nextInt()); System.out.println("Intercambios para la verdura: " + D.getIntercambioVerdura()); //Intercambios de grupo: Azucar System.out.print("Dame el intercambio de la azucar sin grasa: "); D.setIntercambioAzucarSinGrasa(scan.nextInt()); System.out.println("Intercambios para la azucar sin grasa: " + D.getIntercambioAzucarSinGrasa()); System.out.print("Dame el intercambio la azucar con grasa: "); D.setIntercambioAzucarConGrasa(scan.nextInt()); System.out.println("Intercambios para la azucar con grasa: " + D.getIntercambioAzucarConGrasa()); //Intercambios de grupo: Leguminosas y Cereales System.out.print("Dame el intercambio la leguminosa: "); D.setIntercambioLecheConAzucar(scan.nextInt()); System.out.println("Intercambios para la leguminosa: " + D.getIntercambioLecheConAzucar()); System.out.print("Dame el intercambio del cereal sin grasa: "); D.setIntercambioCerealSinGrasa(scan.nextInt()); System.out.println("Intercambios para del cereal sin grasa: " + D.getIntercambioCerealSinGrasa()); System.out.print("Dame el intercambio del cereal con grasa: "); D.setIntercambioCerealConGrasa(scan.nextInt()); System.out.println("Intercambios para del cereal con grasa: " + D.getIntercambioCerealConGrasa()); //Intercambios de grupo: Alimentos de origen animal System.out.print("Dame el intercambio la carne muy magra: "); D.setIntercambioAlimentoMuyMagro(scan.nextInt()); System.out.println("Intercambios para la carne muy magra: " + D.getIntercambioAlimentoMuyMagro()); System.out.print("Dame el intercambio la carne magra: "); D.setIntercambioAlimentoMagro(scan.nextInt()); System.out.println("Intercambios para la carne magra: " + D.getIntercambioAlimentoMagro()); System.out.print("Dame el intercambio la carne semigrasa: "); D.setIntercambioAlimentoSemigraso(scan.nextInt()); System.out.println("Intercambios para la carne semigrasa: " + D.getIntercambioAlimentoSemigraso()); System.out.print("Dame el intercambio la carne grasa: "); D.setIntercambioAlimentoGraso(scan.nextInt()); System.out.println("Intercambios para la carne grasa: " + D.getIntercambioAlimentoGraso()); //Intercambios de grupo: Aceites System.out.print("Dame el intercambio de aceite sin proteina: "); D.setIntercambioAceiteSinProteina(scan.nextInt()); System.out.println("Intercambios para de aceite sin proteina: " + D.getIntercambioAceiteSinProteina()); System.out.print("Dame el intercambio de aceite con proteina: "); D.setIntercambioAceiteConProteina(scan.nextInt()); System.out.println("Intercambios para de aceite con proteina: " + D.getIntercambioAceiteConProteina()); } }
package nutrilife.gui; import java.sql.*; /** * * @author Carmen Suarez */ public class CreateSQLConnection { static final String DRIVER = "com.mysql.jdbc.Driver"; static final String DATABASE_URL = "jdbc:mysql://localhost/3306/Dieta"; static final String USUARIO = "root"; static final String PASSWORD = ""; public static void connection() { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { Class.forName(DRIVER); connection = DriverManager.getConnection(DATABASE_URL, USUARIO, PASSWORD); statement = connection.createStatement(); resultSet = statement.executeQuery("Select * from FichaIdentificacion"); ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); for ( int i = 1; i <= numberOfColumns; i++ ) { System.out.printf( "%-8s\t", resultSet.getObject(i)); } System.out.println(); while(resultSet.next()) { for ( int i = 1; i <= numberOfColumns; i++ ) System.out.printf( "%-8s\t", resultSet.getObject(i)); System.out.println(); } } catch (SQLException sqlException) { sqlException.getMessage(); System.out.println(); sqlException.printStackTrace(); throw new Error(); } catch(ClassNotFoundException classNotFoundException) { classNotFoundException.getMessage(); System.out.println(); classNotFoundException.printStackTrace(); } finally { try { resultSet.close(); statement.close(); connection.close(); } catch (Exception exception) { exception.getMessage(); exception.printStackTrace(); } } } }
connection = DriverManager.getConnection(DATABASE_URL, USUARIO, PASSWORD);