Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DALIA NAYELY CONTRERAS MALFAVÓN, TOPICOS AVANZADOS DE PROGRAMACION, "SUMADOR Y CALIFICACIONES"
  2. //MAIN
  3.  
  4. package sumador;
  5.  
  6. import java.awt.event.ActionEvent;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class Sumador {
  10.  
  11.     public static void main(String[] args) {
  12.        String opc="";
  13.        opc=JOptionPane.showInputDialog(null,"1.-Sumador\n2.-Calilficaciones");
  14.        switch(opc){
  15.            case "1": new OperadoresBasicas(); break;
  16.            case "2": new Calificaciones().menu(); break;
  17.          
  18.        }
  19.      
  20.            
  21.      
  22.     }
  23.      public void actionPerformed(ActionEvent e){
  24.      }
  25.    
  26. }
  27.  
  28. //ALUMNO
  29. package sumador;
  30.  
  31. public class Alumno {
  32.  private String nombre, matricula;
  33.  private int c1,c2,c3,c4,c5;
  34.  
  35.     public Alumno(String nombre, String matricula, int c1, int c2, int c3, int c4, int c5) {
  36.         this.nombre = nombre;
  37.         this.matricula = matricula;
  38.         this.c1 = c1;
  39.         this.c2 = c2;
  40.         this.c3 = c3;
  41.         this.c4 = c4;
  42.         this.c5 = c5;
  43.     }
  44.  
  45.  
  46.     public String getNombre() {
  47.         return nombre;
  48.     }
  49.  
  50.     public void setNombre(String nombre) {
  51.         this.nombre = nombre;
  52.     }
  53.  
  54.     public String getMatricula() {
  55.         return matricula;
  56.     }
  57.  
  58.     public void setMatricula(String matricula) {
  59.         this.matricula = matricula;
  60.     }
  61.  
  62.     public int getC1() {
  63.         return c1;
  64.     }
  65.  
  66.     public void setC1(int c1) {
  67.         this.c1 = c1;
  68.     }
  69.  
  70.     public int getC2() {
  71.         return c2;
  72.     }
  73.  
  74.     public void setC2(int c2) {
  75.         this.c2 = c2;
  76.     }
  77.  
  78.     public int getC3() {
  79.         return c3;
  80.     }
  81.  
  82.     public void setC3(int c3) {
  83.         this.c3 = c3;
  84.     }
  85.  
  86.     public int getC4() {
  87.         return c4;
  88.     }
  89.  
  90.     public void setC4(int c4) {
  91.         this.c4 = c4;
  92.     }
  93.  
  94.     public int getC5() {
  95.         return c5;
  96.     }
  97.  
  98.     public void setC5(int c5) {
  99.         this.c5 = c5;
  100.     }
  101. }
  102. //OPERADORESBASICAS
  103. package sumador;
  104.  
  105. import java.awt.event.ActionEvent;
  106. import java.awt.event.ActionListener;
  107. import javax.swing.JButton;
  108. import javax.swing.JFrame;
  109. import javax.swing.JLabel;
  110. import javax.swing.JOptionPane;
  111. import javax.swing.JTextField;
  112.  
  113. public class OperadoresBasicas extends JFrame implements ActionListener{
  114.  
  115.     private JLabel jlTitulo,jlOper1, jlOper2,jlResultado;
  116.     private JTextField tfOper1, tfOper2,tfResultado;
  117.     private JButton btnTerminar, btnSumar,btnRestar;
  118.    
  119.  
  120. public OperadoresBasicas(){
  121.     super("Operaciones basicas");
  122.     this.setBounds(100,100,600,400);
  123.     this.setVisible(true);
  124.     this.inicializar();
  125. }
  126.  
  127. private void inicializar(){
  128.     this.setLayout(null);
  129.     this.jlTitulo= new JLabel("Operaciones basicas");
  130.     this.jlTitulo.setBounds(200, 30, 200, 30);
  131.     this.add(this.jlTitulo);
  132.    
  133.    
  134.     this.jlOper1= new JLabel("Operador 1");
  135.     this.jlOper1.setBounds(30, 80, 150, 30);
  136.     this.add(this.jlOper1);
  137.    
  138.     this.jlOper2= new JLabel("Operador 2");
  139.     this.jlOper2.setBounds(30, 115, 150, 30);
  140.     this.add(this.jlOper2);
  141.    
  142.    
  143.     this.jlResultado= new JLabel("Resultado");
  144.     this.jlResultado.setBounds(30, 150, 150, 30);
  145.     this.add(this.jlResultado);
  146.     //tetfield
  147.     this.tfOper1= new JTextField("");
  148.     this.tfOper1.setBounds(185, 80, 150, 30);
  149.     this.add(this.tfOper1);
  150.    
  151.     this.tfOper2= new JTextField("");
  152.     this.tfOper2.setBounds(185, 115, 150, 30);
  153.     this.add(this.tfOper2);
  154.    
  155.     this.tfResultado= new JTextField("");
  156.     this.tfResultado.setBounds(185, 150, 150, 30);
  157.     this.tfResultado.setEnabled(false);
  158.     this.add(this.tfResultado);
  159.    
  160.     //botones
  161.     this.btnTerminar= new JButton("Terminar");
  162.     this.btnTerminar.setBounds(115,185, 100, 30);
  163.     this.add(this.btnTerminar);
  164.     this.btnTerminar.addActionListener(this);
  165.    
  166.     this.btnSumar= new JButton("Sumar");
  167.     this.btnSumar.setBounds(220,185, 100, 30);
  168.     this.add(this.btnSumar);
  169.     this.btnSumar.addActionListener(this);
  170.    
  171.    
  172.    
  173.    
  174.    
  175. }
  176.     @Override
  177.     public void actionPerformed(ActionEvent e){
  178.         if(e.getSource()==this.btnTerminar)
  179.            System.exit(1);
  180.         else if(e.getSource()==this.btnSumar){
  181.             int a=0,b=0;
  182.             try{
  183.             a=Integer.parseInt(this.tfOper1.getText());
  184.             b=Integer.parseInt(this.tfOper2.getText());
  185.             } catch(NumberFormatException err){
  186.                 JOptionPane.showMessageDialog(null,"ERROR!");  
  187.                 a=0;b=0;
  188.             }
  189.             if(a<0 || a>100){
  190.                 JOptionPane.showMessageDialog(null,"Valor de a no valido");
  191.                 a=0;
  192.             }
  193.              if(b<0 || b>100){
  194.                 JOptionPane.showMessageDialog(null,"Valor de b no valido");
  195.                 b=0;
  196.             }
  197.            
  198.             this.tfResultado.setText(""+(a+b));
  199.             this.tfOper1.setText("");
  200.             this.tfOper2.setText("");
  201.         }
  202.            
  203.    
  204. }
  205. }
  206. //CALIFICACIONES
  207. package sumador;
  208.  
  209. import java.awt.event.ActionEvent;
  210. import java.awt.event.ActionListener;
  211. import java.io.BufferedReader;
  212. import java.io.DataOutputStream;
  213. import java.io.FileNotFoundException;
  214. import java.io.FileOutputStream;
  215. import java.io.FileReader;
  216. import java.io.IOException;
  217. import java.util.ArrayList;
  218. import java.util.StringTokenizer;
  219. import java.util.logging.Level;
  220. import java.util.logging.Logger;
  221. import javax.swing.JButton;
  222. import javax.swing.JFrame;
  223. import javax.swing.JLabel;
  224. import javax.swing.JOptionPane;
  225. import javax.swing.JTextField;
  226.  
  227. public class Calificaciones extends JFrame implements ActionListener {
  228.    private JLabel jlAlum,jlNom,jlMatric,jlCal1,jlCal2,jlCal3,jlCal4,jlCal5,jlProm;
  229.    private JTextField tfNom,tfMatric,tfCal1,tfCal2,tfCal3,tfCal4,tfCal5;
  230.    private JButton btnguardar;
  231.  
  232.    ArrayList<Alumno> a= new ArrayList<>();
  233.  
  234.    public Calificaciones(){
  235.     super("Calificaciones");
  236.     this.setBounds(100,100,500,500);
  237.     this.inicializar();
  238.      try {
  239.             LeerArchivo();
  240.             System.out.println("Leyendo");
  241.         } catch (IOException ex) {
  242.             System.out.println("no archivo");
  243.         }
  244.    }
  245.  
  246.    public void menu(){
  247.        String opc=JOptionPane.showInputDialog("1.-Agregar alumno\n2.-Mostrar mayor promedio\n3.-Salir");
  248.        switch(opc){
  249.            case "1":
  250.                this.setVisible(true); break;
  251.            case "2":
  252.               muestra();
  253.  
  254.                break;
  255.            case "3":
  256.                System.exit(1); break;
  257.        }
  258.    }
  259.    private void inicializar(){
  260.        this.setLayout(null);
  261.        this.jlAlum= new JLabel("Alumno");
  262.        this.jlAlum.setBounds(200, 30, 200, 30);
  263.        this.add(this.jlAlum);
  264.        this.jlNom= new JLabel("Nombre");
  265.        this.jlNom.setBounds(30, 80, 150, 30);
  266.        this.add(this.jlNom);
  267.        this.jlMatric= new JLabel("Matricula");
  268.        this.jlMatric.setBounds(30, 115, 150, 30);
  269.        this.add(this.jlMatric);
  270.        this.jlCal1= new JLabel("Calificación 1");
  271.        this.jlCal1.setBounds(30, 150, 150, 30);
  272.        this.add(this.jlCal1);
  273.        this.jlCal2= new JLabel("Calificación 2");
  274.        this.jlCal2.setBounds(30, 185, 150, 30);
  275.        this.add(this.jlCal2);
  276.        this.jlCal3= new JLabel("Calificación 3");
  277.        this.jlCal3.setBounds(30, 220, 150, 30);
  278.        this.add(this.jlCal3);
  279.        this.jlCal4= new JLabel("Calificación 4");
  280.        this.jlCal4.setBounds(30, 255, 150, 30);
  281.        this.add(this.jlCal4);
  282.        this.jlCal5= new JLabel("Calificación 5");
  283.        this.jlCal5.setBounds(30, 290, 150, 30);
  284.        this.add(this.jlCal5);
  285.  
  286.         //TEXTFIELDS
  287.         this.tfNom= new JTextField("");
  288.         this.tfNom.setBounds(185, 80, 150, 30);
  289.         this.add(this.tfNom);
  290.         this.tfMatric= new JTextField("");
  291.         this.tfMatric.setBounds(185, 115, 150, 30);
  292.         this.add(this.tfMatric);
  293.         this.tfCal1= new JTextField("");
  294.         this.tfCal1.setBounds(185, 150, 150, 30);
  295.         this.add(this.tfCal1);
  296.         this.tfCal2= new JTextField("");
  297.         this.tfCal2.setBounds(185, 185, 150, 30);
  298.         this.add(this.tfCal2);
  299.         this.tfCal3= new JTextField("");
  300.         this.tfCal3.setBounds(185, 220, 150, 30);
  301.         this.add(this.tfCal3);
  302.         this.tfCal4= new JTextField("");
  303.         this.tfCal4.setBounds(185, 255, 150, 30);
  304.         this.add(this.tfCal4);
  305.         this.tfCal5= new JTextField("");
  306.         this.tfCal5.setBounds(185, 290, 150, 30);
  307.         this.add(this.tfCal5);
  308.         //boton
  309.         this.btnguardar= new JButton("Guardar");
  310.         this.btnguardar.setBounds(220,330, 100, 30);
  311.         this.add(this.btnguardar);
  312.         this.btnguardar.addActionListener(this);
  313.         }
  314.  
  315.    public void Escribir(String d1) throws IOException{
  316.         DataOutputStream archivo=null;
  317.  
  318.         try{
  319.             archivo=new DataOutputStream(
  320.             new FileOutputStream("archivo1.txt",true)); //Sin el true remplaza el contenido del archivo cada vez.
  321.             archivo.writeUTF(d1);
  322.             archivo.close();
  323.         }
  324.         catch(IOException e){
  325.  
  326.         }
  327.     }
  328.     public void LeerArchivo() throws FileNotFoundException, IOException{
  329.     String nom,mat;
  330.     int c1,c2,c3,c4,c5;
  331.     a.clear();
  332.     BufferedReader br = new BufferedReader(new FileReader("archivo1.txt"));
  333.  
  334.      String line;
  335.      while( (line=br.readLine())!=null){
  336.      StringTokenizer s= new StringTokenizer(line,"-");
  337.          nom=s.nextToken();
  338.          mat=s.nextToken();
  339.          c1=Integer.parseInt(s.nextToken());
  340.          c2=Integer.parseInt(s.nextToken());
  341.          c3=Integer.parseInt(s.nextToken());
  342.          c4=Integer.parseInt(s.nextToken());
  343.          c5=Integer.parseInt(s.nextToken());
  344.          a.add(new Alumno(nom,mat,c1,c2,c3,c4,c5)); //lo cambie a integer :'c
  345.     }
  346.     }
  347.  
  348.     public void muestra(){
  349.  
  350.         int index = 0;
  351.         double mp=0,p;
  352.         for (int i=0; i<a.size();i++) {
  353.         p=(a.get(i).getC1()+a.get(i).getC2()+a.get(i).getC3()+a.get(i).getC4()+a.get(i).getC5())/5;
  354.             System.out.println(p);
  355.             if(mp<p){
  356.             index=i;
  357.                 System.out.println("Cambio a: "+a.get(index).getNombre());
  358.             mp=p;
  359.             }
  360.         }
  361.         new Mostrar(a.get(index)).setVisible(true);
  362.         System.out.println("El promedio mas alto fue de: "+a.get(index).getNombre()+" y fue de: "+ mp);
  363.      }
  364.  
  365.    @Override
  366.     public void actionPerformed(ActionEvent e){
  367.         if(Integer.parseInt(tfCal1.getText())<=100 && Integer.parseInt(tfCal1.getText())>=0 &&
  368.            Integer.parseInt(tfCal2.getText())<=100 && Integer.parseInt(tfCal2.getText())>=0 &&
  369.            Integer.parseInt(tfCal3.getText())<=100 && Integer.parseInt(tfCal3.getText())>=0 &&
  370.            Integer.parseInt(tfCal4.getText())<=100 && Integer.parseInt(tfCal4.getText())>=0 &&
  371.            Integer.parseInt(tfCal5.getText())<=100 && Integer.parseInt(tfCal5.getText())>=0
  372.                  ){
  373.     if(e.getSource()==this.btnguardar){
  374.         try {
  375.             Escribir(tfNom.getText()+"-"+tfMatric.getText()+"-"+tfCal1.getText()+"-"+tfCal2.getText()+"-"+tfCal3.getText()+"-"+tfCal4.getText()+"-"+tfCal5.getText()+"-\n");
  376.         } catch (IOException ex) {
  377.  
  378.             Logger.getLogger(Calificaciones.class.getName()).log(Level.SEVERE, null, ex);
  379.         }
  380.         a.add(new Alumno(tfNom.getText(),tfMatric.getText(),Integer.parseInt(tfCal1.getText()),Integer.parseInt(tfCal2.getText()),Integer.parseInt(tfCal3.getText()),Integer.parseInt(tfCal4.getText()),Integer.parseInt(tfCal5.getText())));
  381.         this.setVisible(false);
  382.         menu();
  383.     }}else{
  384.         JOptionPane.showMessageDialog(null,"Ingrese bien las calificaciones");
  385.         this.dispose();
  386.         menu();
  387.  
  388.         }
  389.     }
  390.  
  391.  
  392. }
  393.  
  394. //MOSTRAR
  395. package sumador;
  396.  
  397. import java.awt.event.ActionEvent;
  398. import java.awt.event.ActionListener;
  399. import java.util.ArrayList;
  400. import javax.swing.JButton;
  401. import javax.swing.JFrame;
  402. import javax.swing.JLabel;
  403. import javax.swing.JOptionPane;
  404. import javax.swing.JTextField;
  405.  
  406. public class Mostrar extends JFrame implements ActionListener {
  407.    Alumno b;
  408.    public JLabel jlAlum,jlNom,jlMatric,jlCal1,jlCal2,jlCal3,jlCal4,jlCal5,jlProm;
  409.    public JTextField tfNom,tfMatric,tfCal1,tfCal2,tfCal3,tfCal4,tfCal5,tfProm;
  410.    public JButton btnSalir;
  411.     Calificaciones c= new Calificaciones();
  412.     public Mostrar(Alumno a){
  413.        
  414.     super("Calificaciones");
  415.        b=a;
  416.     this.setBounds(100,100,500,500);
  417.     this.inicializar();
  418.    
  419.    }
  420.    
  421.    
  422.     private void inicializar(){
  423.          this.setLayout(null);
  424.        this.jlAlum= new JLabel("Alumno con mejor promedio");
  425.        this.jlAlum.setBounds(200, 30, 200, 30);
  426.        this.add(this.jlAlum);
  427.        this.jlNom= new JLabel("Nombre");
  428.        this.jlNom.setBounds(30, 80, 150, 30);
  429.        this.add(this.jlNom);
  430.        this.jlMatric= new JLabel("Matricula");
  431.        this.jlMatric.setBounds(30, 115, 150, 30);
  432.        this.add(this.jlMatric);
  433.        this.jlCal1= new JLabel("Calificación 1");
  434.        this.jlCal1.setBounds(30, 150, 150, 30);
  435.        this.add(this.jlCal1);
  436.        this.jlCal2= new JLabel("Calificación 2");
  437.        this.jlCal2.setBounds(30, 185, 150, 30);
  438.        this.add(this.jlCal2);
  439.        this.jlCal3= new JLabel("Calificación 3");
  440.        this.jlCal3.setBounds(30, 220, 150, 30);
  441.        this.add(this.jlCal3);
  442.        this.jlCal4= new JLabel("Calificación 4");
  443.        this.jlCal4.setBounds(30, 255, 150, 30);
  444.        this.add(this.jlCal4);
  445.        this.jlCal5= new JLabel("Calificación 5");
  446.        this.jlCal5.setBounds(30, 290, 150, 30);
  447.        this.add(this.jlCal5);
  448.        this.jlProm= new JLabel("Promedio");
  449.        this.jlProm.setBounds(30, 325, 150, 30);
  450.        this.add(this.jlProm);
  451.        
  452.         //TEXTFIELDS
  453.      
  454.         this.tfNom= new JTextField(b.getNombre());
  455.         this.tfNom.setBounds(185, 80, 150, 30);
  456.         this.tfNom.setEnabled(false);
  457.         this.add(this.tfNom);
  458.         this.tfMatric= new JTextField(b.getMatricula());
  459.         this.tfMatric.setBounds(185, 115, 150, 30);
  460.         this.tfMatric.setEnabled(false);
  461.         this.add(this.tfMatric);
  462.         this.tfCal1= new JTextField(b.getC1()+"");
  463.         this.tfCal1.setBounds(185, 150, 150, 30);
  464.         this.tfCal1.setEnabled(false);
  465.         this.add(this.tfCal1);
  466.         this.tfCal2= new JTextField(b.getC2()+"");
  467.         this.tfCal2.setBounds(185, 185, 150, 30);
  468.         this.tfCal2.setEnabled(false);
  469.         this.add(this.tfCal2);
  470.         this.tfCal3= new JTextField(b.getC3()+"");
  471.         this.tfCal3.setBounds(185, 220, 150, 30);
  472.         this.tfCal3.setEnabled(false);
  473.         this.add(this.tfCal3);
  474.         this.tfCal4= new JTextField(b.getC4()+"");
  475.         this.tfCal4.setBounds(185, 255, 150, 30);
  476.         this.tfCal4.setEnabled(false);
  477.         this.add(this.tfCal4);
  478.         this.tfCal5= new JTextField(b.getC5()+"");
  479.         this.tfCal5.setBounds(185, 290, 150, 30);
  480.         this.tfCal5.setEnabled(false);
  481.         this.add(this.tfCal5);
  482.         this.tfProm= new JTextField(((Integer.parseInt(tfCal1.getText())+Integer.parseInt(tfCal2.getText())+Integer.parseInt(tfCal3.getText())+Integer.parseInt(tfCal4.getText())+Integer.parseInt(tfCal5.getText()))/5)+"");
  483.         this.tfProm.setBounds(185,325,150,30);
  484.         this.tfProm.setEnabled(false);
  485.         this.add(this.tfProm);
  486.         //boton
  487.         this.btnSalir= new JButton("Salir");
  488.         this.btnSalir.setBounds(220,365, 100, 30);
  489.         this.add(this.btnSalir);
  490.         this.btnSalir.addActionListener(this);
  491.     }
  492.      public void actionPerformed(ActionEvent e){
  493.      if(e.getSource()==this.btnSalir)
  494.          this.dispose();
  495.          c.menu();
  496.       }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement