Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DALIA NAYELY CONTRERAS MALFAVÓN, TOPICOS AVANZADOS DE PROGRAMACION, "SUMADOR Y CALIFICACIONES"
- //MAIN
- package sumador;
- import java.awt.event.ActionEvent;
- import javax.swing.JOptionPane;
- public class Sumador {
- public static void main(String[] args) {
- String opc="";
- opc=JOptionPane.showInputDialog(null,"1.-Sumador\n2.-Calilficaciones");
- switch(opc){
- case "1": new OperadoresBasicas(); break;
- case "2": new Calificaciones().menu(); break;
- }
- }
- public void actionPerformed(ActionEvent e){
- }
- }
- //ALUMNO
- package sumador;
- public class Alumno {
- private String nombre, matricula;
- private int c1,c2,c3,c4,c5;
- public Alumno(String nombre, String matricula, int c1, int c2, int c3, int c4, int c5) {
- this.nombre = nombre;
- this.matricula = matricula;
- this.c1 = c1;
- this.c2 = c2;
- this.c3 = c3;
- this.c4 = c4;
- this.c5 = c5;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public String getMatricula() {
- return matricula;
- }
- public void setMatricula(String matricula) {
- this.matricula = matricula;
- }
- public int getC1() {
- return c1;
- }
- public void setC1(int c1) {
- this.c1 = c1;
- }
- public int getC2() {
- return c2;
- }
- public void setC2(int c2) {
- this.c2 = c2;
- }
- public int getC3() {
- return c3;
- }
- public void setC3(int c3) {
- this.c3 = c3;
- }
- public int getC4() {
- return c4;
- }
- public void setC4(int c4) {
- this.c4 = c4;
- }
- public int getC5() {
- return c5;
- }
- public void setC5(int c5) {
- this.c5 = c5;
- }
- }
- //OPERADORESBASICAS
- package sumador;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class OperadoresBasicas extends JFrame implements ActionListener{
- private JLabel jlTitulo,jlOper1, jlOper2,jlResultado;
- private JTextField tfOper1, tfOper2,tfResultado;
- private JButton btnTerminar, btnSumar,btnRestar;
- public OperadoresBasicas(){
- super("Operaciones basicas");
- this.setBounds(100,100,600,400);
- this.setVisible(true);
- this.inicializar();
- }
- private void inicializar(){
- this.setLayout(null);
- this.jlTitulo= new JLabel("Operaciones basicas");
- this.jlTitulo.setBounds(200, 30, 200, 30);
- this.add(this.jlTitulo);
- this.jlOper1= new JLabel("Operador 1");
- this.jlOper1.setBounds(30, 80, 150, 30);
- this.add(this.jlOper1);
- this.jlOper2= new JLabel("Operador 2");
- this.jlOper2.setBounds(30, 115, 150, 30);
- this.add(this.jlOper2);
- this.jlResultado= new JLabel("Resultado");
- this.jlResultado.setBounds(30, 150, 150, 30);
- this.add(this.jlResultado);
- //tetfield
- this.tfOper1= new JTextField("");
- this.tfOper1.setBounds(185, 80, 150, 30);
- this.add(this.tfOper1);
- this.tfOper2= new JTextField("");
- this.tfOper2.setBounds(185, 115, 150, 30);
- this.add(this.tfOper2);
- this.tfResultado= new JTextField("");
- this.tfResultado.setBounds(185, 150, 150, 30);
- this.tfResultado.setEnabled(false);
- this.add(this.tfResultado);
- //botones
- this.btnTerminar= new JButton("Terminar");
- this.btnTerminar.setBounds(115,185, 100, 30);
- this.add(this.btnTerminar);
- this.btnTerminar.addActionListener(this);
- this.btnSumar= new JButton("Sumar");
- this.btnSumar.setBounds(220,185, 100, 30);
- this.add(this.btnSumar);
- this.btnSumar.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==this.btnTerminar)
- System.exit(1);
- else if(e.getSource()==this.btnSumar){
- int a=0,b=0;
- try{
- a=Integer.parseInt(this.tfOper1.getText());
- b=Integer.parseInt(this.tfOper2.getText());
- } catch(NumberFormatException err){
- JOptionPane.showMessageDialog(null,"ERROR!");
- a=0;b=0;
- }
- if(a<0 || a>100){
- JOptionPane.showMessageDialog(null,"Valor de a no valido");
- a=0;
- }
- if(b<0 || b>100){
- JOptionPane.showMessageDialog(null,"Valor de b no valido");
- b=0;
- }
- this.tfResultado.setText(""+(a+b));
- this.tfOper1.setText("");
- this.tfOper2.setText("");
- }
- }
- }
- //CALIFICACIONES
- package sumador;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class Calificaciones extends JFrame implements ActionListener {
- private JLabel jlAlum,jlNom,jlMatric,jlCal1,jlCal2,jlCal3,jlCal4,jlCal5,jlProm;
- private JTextField tfNom,tfMatric,tfCal1,tfCal2,tfCal3,tfCal4,tfCal5;
- private JButton btnguardar;
- ArrayList<Alumno> a= new ArrayList<>();
- public Calificaciones(){
- super("Calificaciones");
- this.setBounds(100,100,500,500);
- this.inicializar();
- try {
- LeerArchivo();
- System.out.println("Leyendo");
- } catch (IOException ex) {
- System.out.println("no archivo");
- }
- }
- public void menu(){
- String opc=JOptionPane.showInputDialog("1.-Agregar alumno\n2.-Mostrar mayor promedio\n3.-Salir");
- switch(opc){
- case "1":
- this.setVisible(true); break;
- case "2":
- muestra();
- break;
- case "3":
- System.exit(1); break;
- }
- }
- private void inicializar(){
- this.setLayout(null);
- this.jlAlum= new JLabel("Alumno");
- this.jlAlum.setBounds(200, 30, 200, 30);
- this.add(this.jlAlum);
- this.jlNom= new JLabel("Nombre");
- this.jlNom.setBounds(30, 80, 150, 30);
- this.add(this.jlNom);
- this.jlMatric= new JLabel("Matricula");
- this.jlMatric.setBounds(30, 115, 150, 30);
- this.add(this.jlMatric);
- this.jlCal1= new JLabel("Calificación 1");
- this.jlCal1.setBounds(30, 150, 150, 30);
- this.add(this.jlCal1);
- this.jlCal2= new JLabel("Calificación 2");
- this.jlCal2.setBounds(30, 185, 150, 30);
- this.add(this.jlCal2);
- this.jlCal3= new JLabel("Calificación 3");
- this.jlCal3.setBounds(30, 220, 150, 30);
- this.add(this.jlCal3);
- this.jlCal4= new JLabel("Calificación 4");
- this.jlCal4.setBounds(30, 255, 150, 30);
- this.add(this.jlCal4);
- this.jlCal5= new JLabel("Calificación 5");
- this.jlCal5.setBounds(30, 290, 150, 30);
- this.add(this.jlCal5);
- //TEXTFIELDS
- this.tfNom= new JTextField("");
- this.tfNom.setBounds(185, 80, 150, 30);
- this.add(this.tfNom);
- this.tfMatric= new JTextField("");
- this.tfMatric.setBounds(185, 115, 150, 30);
- this.add(this.tfMatric);
- this.tfCal1= new JTextField("");
- this.tfCal1.setBounds(185, 150, 150, 30);
- this.add(this.tfCal1);
- this.tfCal2= new JTextField("");
- this.tfCal2.setBounds(185, 185, 150, 30);
- this.add(this.tfCal2);
- this.tfCal3= new JTextField("");
- this.tfCal3.setBounds(185, 220, 150, 30);
- this.add(this.tfCal3);
- this.tfCal4= new JTextField("");
- this.tfCal4.setBounds(185, 255, 150, 30);
- this.add(this.tfCal4);
- this.tfCal5= new JTextField("");
- this.tfCal5.setBounds(185, 290, 150, 30);
- this.add(this.tfCal5);
- //boton
- this.btnguardar= new JButton("Guardar");
- this.btnguardar.setBounds(220,330, 100, 30);
- this.add(this.btnguardar);
- this.btnguardar.addActionListener(this);
- }
- public void Escribir(String d1) throws IOException{
- DataOutputStream archivo=null;
- try{
- archivo=new DataOutputStream(
- new FileOutputStream("archivo1.txt",true)); //Sin el true remplaza el contenido del archivo cada vez.
- archivo.writeUTF(d1);
- archivo.close();
- }
- catch(IOException e){
- }
- }
- public void LeerArchivo() throws FileNotFoundException, IOException{
- String nom,mat;
- int c1,c2,c3,c4,c5;
- a.clear();
- BufferedReader br = new BufferedReader(new FileReader("archivo1.txt"));
- String line;
- while( (line=br.readLine())!=null){
- StringTokenizer s= new StringTokenizer(line,"-");
- nom=s.nextToken();
- mat=s.nextToken();
- c1=Integer.parseInt(s.nextToken());
- c2=Integer.parseInt(s.nextToken());
- c3=Integer.parseInt(s.nextToken());
- c4=Integer.parseInt(s.nextToken());
- c5=Integer.parseInt(s.nextToken());
- a.add(new Alumno(nom,mat,c1,c2,c3,c4,c5)); //lo cambie a integer :'c
- }
- }
- public void muestra(){
- int index = 0;
- double mp=0,p;
- for (int i=0; i<a.size();i++) {
- p=(a.get(i).getC1()+a.get(i).getC2()+a.get(i).getC3()+a.get(i).getC4()+a.get(i).getC5())/5;
- System.out.println(p);
- if(mp<p){
- index=i;
- System.out.println("Cambio a: "+a.get(index).getNombre());
- mp=p;
- }
- }
- new Mostrar(a.get(index)).setVisible(true);
- System.out.println("El promedio mas alto fue de: "+a.get(index).getNombre()+" y fue de: "+ mp);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if(Integer.parseInt(tfCal1.getText())<=100 && Integer.parseInt(tfCal1.getText())>=0 &&
- Integer.parseInt(tfCal2.getText())<=100 && Integer.parseInt(tfCal2.getText())>=0 &&
- Integer.parseInt(tfCal3.getText())<=100 && Integer.parseInt(tfCal3.getText())>=0 &&
- Integer.parseInt(tfCal4.getText())<=100 && Integer.parseInt(tfCal4.getText())>=0 &&
- Integer.parseInt(tfCal5.getText())<=100 && Integer.parseInt(tfCal5.getText())>=0
- ){
- if(e.getSource()==this.btnguardar){
- try {
- Escribir(tfNom.getText()+"-"+tfMatric.getText()+"-"+tfCal1.getText()+"-"+tfCal2.getText()+"-"+tfCal3.getText()+"-"+tfCal4.getText()+"-"+tfCal5.getText()+"-\n");
- } catch (IOException ex) {
- Logger.getLogger(Calificaciones.class.getName()).log(Level.SEVERE, null, ex);
- }
- 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())));
- this.setVisible(false);
- menu();
- }}else{
- JOptionPane.showMessageDialog(null,"Ingrese bien las calificaciones");
- this.dispose();
- menu();
- }
- }
- }
- //MOSTRAR
- package sumador;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class Mostrar extends JFrame implements ActionListener {
- Alumno b;
- public JLabel jlAlum,jlNom,jlMatric,jlCal1,jlCal2,jlCal3,jlCal4,jlCal5,jlProm;
- public JTextField tfNom,tfMatric,tfCal1,tfCal2,tfCal3,tfCal4,tfCal5,tfProm;
- public JButton btnSalir;
- Calificaciones c= new Calificaciones();
- public Mostrar(Alumno a){
- super("Calificaciones");
- b=a;
- this.setBounds(100,100,500,500);
- this.inicializar();
- }
- private void inicializar(){
- this.setLayout(null);
- this.jlAlum= new JLabel("Alumno con mejor promedio");
- this.jlAlum.setBounds(200, 30, 200, 30);
- this.add(this.jlAlum);
- this.jlNom= new JLabel("Nombre");
- this.jlNom.setBounds(30, 80, 150, 30);
- this.add(this.jlNom);
- this.jlMatric= new JLabel("Matricula");
- this.jlMatric.setBounds(30, 115, 150, 30);
- this.add(this.jlMatric);
- this.jlCal1= new JLabel("Calificación 1");
- this.jlCal1.setBounds(30, 150, 150, 30);
- this.add(this.jlCal1);
- this.jlCal2= new JLabel("Calificación 2");
- this.jlCal2.setBounds(30, 185, 150, 30);
- this.add(this.jlCal2);
- this.jlCal3= new JLabel("Calificación 3");
- this.jlCal3.setBounds(30, 220, 150, 30);
- this.add(this.jlCal3);
- this.jlCal4= new JLabel("Calificación 4");
- this.jlCal4.setBounds(30, 255, 150, 30);
- this.add(this.jlCal4);
- this.jlCal5= new JLabel("Calificación 5");
- this.jlCal5.setBounds(30, 290, 150, 30);
- this.add(this.jlCal5);
- this.jlProm= new JLabel("Promedio");
- this.jlProm.setBounds(30, 325, 150, 30);
- this.add(this.jlProm);
- //TEXTFIELDS
- this.tfNom= new JTextField(b.getNombre());
- this.tfNom.setBounds(185, 80, 150, 30);
- this.tfNom.setEnabled(false);
- this.add(this.tfNom);
- this.tfMatric= new JTextField(b.getMatricula());
- this.tfMatric.setBounds(185, 115, 150, 30);
- this.tfMatric.setEnabled(false);
- this.add(this.tfMatric);
- this.tfCal1= new JTextField(b.getC1()+"");
- this.tfCal1.setBounds(185, 150, 150, 30);
- this.tfCal1.setEnabled(false);
- this.add(this.tfCal1);
- this.tfCal2= new JTextField(b.getC2()+"");
- this.tfCal2.setBounds(185, 185, 150, 30);
- this.tfCal2.setEnabled(false);
- this.add(this.tfCal2);
- this.tfCal3= new JTextField(b.getC3()+"");
- this.tfCal3.setBounds(185, 220, 150, 30);
- this.tfCal3.setEnabled(false);
- this.add(this.tfCal3);
- this.tfCal4= new JTextField(b.getC4()+"");
- this.tfCal4.setBounds(185, 255, 150, 30);
- this.tfCal4.setEnabled(false);
- this.add(this.tfCal4);
- this.tfCal5= new JTextField(b.getC5()+"");
- this.tfCal5.setBounds(185, 290, 150, 30);
- this.tfCal5.setEnabled(false);
- this.add(this.tfCal5);
- 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)+"");
- this.tfProm.setBounds(185,325,150,30);
- this.tfProm.setEnabled(false);
- this.add(this.tfProm);
- //boton
- this.btnSalir= new JButton("Salir");
- this.btnSalir.setBounds(220,365, 100, 30);
- this.add(this.btnSalir);
- this.btnSalir.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==this.btnSalir)
- this.dispose();
- c.menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement