PROG2 Inlupp1

Från Data- och Systemvetenskap
Hoppa till: navigering, sök

Inlupp1 - Chewtoy's code:

/*
 * By: Chewtoy / brbe0492 / Bror Bellander
 * For java7
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
 
public class PROG2inlupp1 extends JFrame{
	private ArrayList<Deltagare> deltagare = new ArrayList<Deltagare>();
 
	private JButton newB, showB, timeB, quit;
	private JRadioButton nrR, nameR, ageR, timeR;
	private JTextArea ta = new JTextArea();
	private int startnr = 1;
 
	// Layout
	public void init(){
		setTitle("PROG2 inlupp1 - Kista maraton");
		Button lis = new Button();
 
		// Label and Quit
		JPanel top = new JPanel();
		top.add(new JLabel("DSV Kista Maraton"));
		quit = new JButton("Avsluta");
		quit.addActionListener(lis);
		top.add(quit);
		add(top, BorderLayout.NORTH);
 
		// TextArea
		ta.setEditable(false);
		add(new JScrollPane(ta), BorderLayout.CENTER);
 
		// RadioButtons
		JPanel rig = new JPanel();
		rig.setLayout(new BoxLayout(rig, BoxLayout.Y_AXIS));
 
		rig.add(new JLabel("Sortering:"));
 
		nrR = new JRadioButton("Startnr", true);
		rig.add(nrR);
 
		nameR = new JRadioButton("Namn");
		rig.add(nameR);
 
		ageR = new JRadioButton("Ålder");
		rig.add(ageR);
 
		timeR = new JRadioButton("Tid");
		rig.add(timeR);
 
		ButtonGroup rbut = new ButtonGroup();
			rbut.add(nrR);
			rbut.add(nameR);
			rbut.add(ageR);
			rbut.add(timeR);
		add(rig, BorderLayout.EAST);
 
		// Buttons
		JPanel bot = new JPanel();
		bot.setLayout(new BoxLayout(bot, BoxLayout.X_AXIS));
 
		newB = new JButton("Ny");
		newB.addActionListener(lis);
		bot.add(newB);
 
		showB = new JButton("Visa");
		showB.addActionListener(lis);
		bot.add(showB);
 
		timeB = new JButton("Tid");
		timeB.addActionListener(lis);
		bot.add(timeB);
 
		add(bot, BorderLayout.SOUTH);
 
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(300,300);
		setVisible(true);
	}
 
	public void nPerson(){		// ta in ny deltagare
		JPanel pPanel = new JPanel(),
		       pName = new JPanel(),
		       pNation = new JPanel(),
		       pAge = new JPanel();
 
		JTextField nameTF = new JTextField(15);
		JTextField nationTF = new JTextField(15);
		SpinnerNumberModel snm = new SpinnerNumberModel(20, 13, 90, 1);
		JSpinner ageS = new JSpinner(snm);
		JLabel titleL = new JLabel("Deltagare nr. " + startnr);
		JLabel nameL = new JLabel("Namn:");
		JLabel nationL = new JLabel("Land:");
		JLabel ageL = new JLabel("Ålder:");
 
 
		pPanel.setLayout(new BoxLayout(pPanel, BoxLayout.Y_AXIS));
		pPanel.add(titleL);
		pName.add(nameL);
		pName.add(nameTF);
		pPanel.add(pName);
 
		pNation.add(nationL);
		pNation.add(nationTF);
		pPanel.add(pNation);
 
		pAge.add(ageL);
		pAge.add(ageS);
		pPanel.add(pAge);
 
		boolean on = true;		
		while(on){
			int result = JOptionPane.showConfirmDialog(null, pPanel, "Skriv in deltagaren", JOptionPane.OK_CANCEL_OPTION);
			if (result == JOptionPane.OK_OPTION){
 
				if(!nameTF.getText().matches("\\s*") && !nameTF.getText().equals(null) && !nameTF.getText().equals("") && !nationTF.getText().matches("\\s*") && !nationTF.getText().equals(null) && !nationTF.getText().equals("")) {
 
					deltagare.add(new Deltagare(startnr, nameTF.getText(), nationTF.getText(), ((Integer) ageS.getValue()).intValue()));
					startnr++;
					on = false;
				}
				else {
					if (nameTF.getText().equals(null) || nameTF.getText().equals("")) {
						JOptionPane.showMessageDialog(null, "Du måste skriva in vad deltagaren heter!");
					}
 
					else if (nameTF.getText().matches("\\s*")){
						JOptionPane.showMessageDialog(null, "Man kan väl inte heta något som inte har synliga tecken i sig?");
					}
 
					else if (nationTF.getText().equals(null) || nationTF.getText().equals("")){
						JOptionPane.showMessageDialog(null, "Du måste skriva in vilket land deltagaren kommer från!!");
					}
 
					else if (nationTF.getText().matches("\\s*")){
						JOptionPane.showMessageDialog(null, "Alla länder heter något med synliga tecken, så testa att skriva in krumelurer.");
					}
				}
			}
 
			else if (result == JOptionPane.CANCEL_OPTION){
				on = false;
			}
		}
	}
 
 
		public void showNr(){			// Visa sorterat efter startNr
			String str = "";
			ta.setText("");
 
 
			if (deltagare.size() > 0){
				Collections.sort(deltagare, new Comparator<Deltagare>(){
					public int compare(Deltagare d1, Deltagare d2){
						return d1.getStartnr() - d2.getStartnr();
					}
				});
 
				for (Deltagare x : deltagare) {
					ta.append(x.getStartnr() + ". " + x.getName() + " - " + x.getNation() + " - " + x.getAge() + " - ");
					if (x.getTime() == 0){
					       ta.append("--" + "\n");
					}
			 		else {
						ta.append(x.getTime() +"\n");
					}
				}
			}
		}
 
		public void showName(){			// Visa sorterat efter namn
			ta.setText("");
 
 
			if (deltagare.size() > 0){
				Collections.sort(deltagare, new Comparator<Deltagare>(){
					public int compare(Deltagare d1, Deltagare d2){
						return d1.getName().compareTo(d2.getName());
					}
				});
				for (Deltagare x : deltagare){
					ta.append(x.getStartnr() + ". " + x.getName() + " - " + x.getNation() + " - " + x.getAge() + " - ");
					if (x.getTime() == 0){
					       ta.append("--" + "\n");
					}
			 		else {
						ta.append(x.getTime() +"\n");
					}
				}
			}
 
		}
 
		public void showAge(){			// Visa sorterat efter Ålder
			ta.setText("");
			if (deltagare.size() > 0){
				Collections.sort(deltagare, new Comparator<Deltagare>(){
					public int compare(Deltagare d1, Deltagare d2){
						return d1.getAge() - d2.getAge();
					}
				});
 
				for (Deltagare x : deltagare) {
					ta.append(x.getStartnr() + ". " + x.getName() + " - " + x.getNation() + " - " + x.getAge() + " - ");
					if (x.getTime() == 0){
					       ta.append("--" + "\n");
					}
			 		else {
						ta.append(x.getTime() +"\n");
					}
				}
			}
		}
 
		public void showTime(){			// Visa sorterat efter tid
			ta.setText("");
			if (deltagare.size() > 0){
				Collections.sort(deltagare, new Comparator<Deltagare>(){
					public int compare(Deltagare d1, Deltagare d2){
						return new Double(d1.getTime()).compareTo(new Double(d2.getTime()));
					}
				});
 
				for (Deltagare x : deltagare) { // Visa deltagare med tid först
					if (x.getTime() > 0) {
						ta.append(x.getStartnr() + ". " + x.getName() + " - " + x.getNation() + " - " + x.getAge() + " - " + x.getTime() + "\n");
					}
				}
				for (Deltagare x : deltagare) {
					if (x.getTime() <= 0) {
						ta.append(x.getStartnr() + ". " + x.getName() + " - " + x.getNation() + " - " + x.getAge() + " - " + "--\n");
					}
				}
			}
		}
 
 
		public void Time(){		// Skriv in tid på deltagare
 
 
			if (deltagare.size() > 0){
				JPanel tP = new JPanel(),
					pNr = new JPanel(),
					pTime = new JPanel(),
					pH = new JPanel();
 
				SpinnerNumberModel Tsnm = new SpinnerNumberModel(1,1,deltagare.size(),1),
						   nmTH = new SpinnerNumberModel(0,0,999,1),
						   nmTM = new SpinnerNumberModel(0,0,59,1);
				JSpinner sNr = new JSpinner(Tsnm),
					 sH = new JSpinner(nmTH),
					 sM = new JSpinner(nmTM);
 
				JLabel lNr = new JLabel("Startnr:"),
				       lTime = new JLabel("Tid:"),
				       lTh = new JLabel("timmar"),
				       lTm = new JLabel("minuter");
 
				JTextField tfTime = new JTextField(5);
 
				tP.setLayout(new BoxLayout(tP, BoxLayout.Y_AXIS));
				pNr.add(lNr);
				pNr.add(sNr);
				tP.add(pNr);
 
				pTime.add(lTime);
				pTime.add(sH);
				pTime.add(lTh);
				pTime.add(sM);
				pTime.add(lTm);
				tP.add(pTime);	
 
 
				boolean on = true;
				while(on){
					int result = JOptionPane.showConfirmDialog(null, tP, "Registrera tid", JOptionPane.OK_CANCEL_OPTION);
 
					if (result == JOptionPane.OK_OPTION){
						String StmpTime = "0.0";
						if (((Integer) sM.getValue()).intValue() < 10){
							StmpTime = ((Integer) sH.getValue()).intValue() + "." + "0" + ((Integer) sM.getValue()).intValue();
						}
						else{
							StmpTime = ((Integer) sH.getValue()).intValue() + "." + ((Integer) sM.getValue()).intValue();
						}
 
						double dtmpTime = Double.parseDouble(StmpTime);
 
						for (Deltagare x : deltagare) {
							if (x.getTime() == 0.0) {
								if (x.getStartnr() == ((Integer) sNr.getValue()).intValue()){
									x.setTime(dtmpTime);
									on = false;
								}
							}
							else if (x.getTime() > 0 && x.getStartnr() == ((Integer) sNr.getValue()).intValue()){
								int check = JOptionPane.showConfirmDialog(null, "Deltagaren har redan en tid.\nVill du ersätta denna?", "Har redan tid", JOptionPane.OK_CANCEL_OPTION);
 
								if (check == JOptionPane.OK_OPTION){
									if (x.getStartnr() == ((Integer) sNr.getValue()).intValue()){
										x.setTime(dtmpTime);
										on = false;
									}
								}
							}
						}
					}
					else if (result == JOptionPane.CANCEL_OPTION){
						on = false;
					}
				}	
			}
			else{
				JOptionPane.showMessageDialog(null, "Du måste lägga till deltagare innan någon kan få en tid!");
			}
		}
 
 
	class Button implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			if (ae.getSource() == newB) {
				nPerson();
			}
 
			else if (ae.getSource() == showB) {
 
				if (nrR.isSelected()){
					showNr();
				}
 
				else if (nameR.isSelected()){
					showName();
				}
 
				else if (ageR.isSelected()){
					showAge();
				}
 
				else if (timeR.isSelected()){
					showTime();
				}
 
			}
			else if (ae.getSource() == timeB) {
				Time();
			}
			else if (ae.getSource() == quit) {
				System.out.println("Quit");
				dispose();
				System.exit(0);
			}
		}
	}
 
	public static void main(String[] args){
		new PROG2inlupp1().init();
	}
}
 
class Deltagare {
	private int startnr;
	private String name;
	private String nation;
	private int age;
	private double time=0.00;
 
	Deltagare(){}
 
	Deltagare(int inStartnr, String inName, String inNation, int inAge){
		startnr=inStartnr;
		name=inName;
		nation=inNation;
		age = inAge;
	}
 
	void setTime(double inTime){
		time = inTime;
	}
 
	int getStartnr(){
		return startnr;
	}
 
	String getName(){
		return name;
	}
 
	String getNation(){
		return nation;
	}
 
	int getAge(){
		return age;
	}
 
	double getTime(){
		return time;
	}
}
Personliga verktyg
Namnrymder
Varianter
Åtgärder
Navigering
Verktygslåda