import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; class Registry extends JFrame{ JTextArea mainText = new JTextArea(); ArrayList allReg = new ArrayList(); JRadioButton startNrRB = new JRadioButton("Startnr",true); JRadioButton timeRB = new JRadioButton("Time"); JRadioButton ageRB = new JRadioButton("Age"); JRadioButton nameRB = new JRadioButton("Name"); Registry(){ super("DSV Marathon Registry"); setLayout(new BorderLayout()); JPanel north = new JPanel();//north add(north, BorderLayout.NORTH); JLabel northLabel = new JLabel("DSV Kista Marathon"); north.add(northLabel);//north ends TMP JScrollPane main = new JScrollPane(mainText);//center mainText.setLineWrap(true); add(main, BorderLayout.CENTER);//center ends TMP JPanel east = new JPanel(new GridLayout(2,1));//east JPanel sEast = new JPanel(new GridLayout(5,1)); JLabel sort = new JLabel("Sort by:"); JLabel blank = new JLabel(""); east.add(blank); add(east, BorderLayout.EAST); east.add(sEast); sEast.add(sort); sEast.add(startNrRB); sEast.add(nameRB); sEast.add(ageRB); sEast.add(timeRB); ButtonGroup radioGroup = new ButtonGroup(); radioGroup.add(startNrRB); radioGroup.add(nameRB); radioGroup.add(ageRB); radioGroup.add(timeRB);//east ends TMP JPanel south = new JPanel(new GridLayout(1,2));//south add(south, BorderLayout.SOUTH); JPanel southPan = new JPanel(); JPanel nullPan = new JPanel(null); south.add(southPan); south.add(nullPan); JButton newBut = new JButton("New"); southPan.add(newBut); newBut.addActionListener(new NewListener()); JButton showBut = new JButton("Show"); southPan.add(showBut); showBut.addActionListener(new ShowListener()); JButton timeBut = new JButton("Time"); southPan.add(timeBut); timeBut.addActionListener(new TimeListener());//south ends TMP setVisible(true); setSize(500,450); setDefaultCloseOperation(EXIT_ON_CLOSE); } public class Form extends JPanel{ JTextField nameField = new JTextField(15); JTextField countryField = new JTextField(15); JTextField ageField = new JTextField(5); Form(){ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); JPanel top = new JPanel(); add(top); JLabel nameLabel = new JLabel("Name: "); top.add(nameLabel); top.add(nameField); JPanel center = new JPanel(); add(center); JLabel countryLabel = new JLabel("Country: "); center.add(countryLabel); center.add(countryField); JPanel bottom = new JPanel(); add(bottom); JLabel ageLabel = new JLabel("Age: "); bottom.add(ageLabel); bottom.add(ageField); }//form String getFName(){ return nameField.getText(); } String getFCountry(){ return countryField.getText(); } String getFAge(){ return(ageField.getText()); } }//form class public class TimeForm extends JPanel{ JTextField nrField = new JTextField(10); JTextField timeField = new JTextField(10); TimeForm(){ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); JPanel top = new JPanel(); add(top); JLabel nrLabel = new JLabel("Startnr: "); top.add(nrLabel); top.add(nrField); JPanel bottom = new JPanel(); add(bottom); JLabel timeLabel = new JLabel("Time: "); bottom.add(timeLabel); bottom.add(timeField); } String getTNr(){ return nrField.getText(); } String getTTime(){ return timeField.getText(); } } class ShowListener implements ActionListener{ public void actionPerformed(ActionEvent ave){ mainText.setText(""); if (startNrRB.isSelected()){ Collections.sort(allReg, new StartCmp()); for(Person pers : allReg) mainText.append(pers.toString()+"\n"); }else if (nameRB.isSelected()){ Collections.sort(allReg, new NameCmp()); for(Person pers : allReg) mainText.append(pers.toString()+"\n"); }else if (ageRB.isSelected()){ Collections.sort(allReg, new AgeCmp()); for(Person pers : allReg) mainText.append(pers.toString()+"\n"); }else if (timeRB.isSelected()){ Collections.sort(allReg, new TimeCmp()); for(Person pers : allReg){ if(pers.getTime()>0) mainText.append(pers.toString()+"\n"); } } } } class NewListener implements ActionListener{ public void actionPerformed(ActionEvent ave){ Form f = new Form(); for(;;){ boolean ok=true; try{ int input = JOptionPane.showConfirmDialog(Registry.this,f,"Add new person",JOptionPane.OK_CANCEL_OPTION); if (input !=JOptionPane.YES_OPTION) return; String name = f.getFName(); if(name.length()<1){ JOptionPane.showMessageDialog(Registry.this, "You can not leave the name field empty"); ok=false; } String country = f.getFCountry(); if(country.length()<1){ JOptionPane.showMessageDialog(Registry.this, "You can not leave the country field empty"); ok=false; } int age = Integer.parseInt(f.getFAge()); if (age>140||age<16||age==0){ JOptionPane.showMessageDialog(Registry.this, "You have to be older than 15 and younger than 140"); ok=false; } if(ok){ Person tmp = new Person(name,country,age); allReg.add(tmp); return; } }catch(NumberFormatException e){ JOptionPane.showMessageDialog(Registry.this, "You have to add a number in the age field"); ok=false; } }//for }//ave }//NewListener class TimeListener implements ActionListener{ public void actionPerformed(ActionEvent ave){ TimeForm f = new TimeForm(); for(;;){ boolean ok=true; try{ int input = JOptionPane.showConfirmDialog(Registry.this,f,"Add a persons time",JOptionPane.OK_CANCEL_OPTION); if (input !=JOptionPane.YES_OPTION) return; int nr = Integer.parseInt(f.getTNr()); double time = Double.parseDouble(f.getTTime()); if(allReg.size()<1){ JOptionPane.showMessageDialog(Registry.this, "No persons in the registry!"); return; } for (Person tmp : allReg){//FEL HÄR if(tmp.getStart()==nr){//FEL HÄR tmp.setTime(time);//FEL HÄR ok=true;//FEL HÄR break;//FEL HÄR }else{ ok=false; } } if(ok) return; if(!ok){ JOptionPane.showMessageDialog(Registry.this, "No person with that Start number"); return; } }catch(NumberFormatException e){ JOptionPane.showMessageDialog(Registry.this, "Bad input. Use numbers"); ok=false; } }//for }//ave }//TimeListener class NameCmp implements Comparator{ public int compare(Person p1, Person p2){ return p1.getName().compareTo(p2.getName()); } } class AgeCmp implements Comparator{ public int compare(Person p1, Person p2){ return p1.getAge() - p2.getAge(); } } class TimeCmp implements Comparator{ public int compare(Person p1, Person p2){ return (int)(p1.getTime() - p2.getTime()); /*double tmp = p1.getTime() - p2.getTime(); if (tmp<0) return -1; else if(tmp>0) return +1; else return 0;*/ } } class StartCmp implements Comparator{ public int compare(Person p1, Person p2){ return p1.getStart() - p2.getStart(); } } public static void main(String[]args){ new Registry(); } }