diff --git a/src/Tietorakenteet/Hiihdot.java b/src/Tietorakenteet/Hiihdot.java new file mode 100644 index 0000000000000000000000000000000000000000..51a854dda8bcc8f517f2e682a1427d18de3d77af --- /dev/null +++ b/src/Tietorakenteet/Hiihdot.java @@ -0,0 +1,110 @@ +package Tietorakenteet; + +import java.util.ArrayList; + +/** + * @author MHo + * @version 20.2.2020 + * Hiihtorekisterin jasenet, joka lisää ja poistaa jäseniä. + */ +public class Hiihdot { + private String tiedostonNimi = ""; + private ArrayList<Hiihto> hiihdot = new ArrayList<Hiihto>(); + + /** + * Muodostaja + */ + public Hiihdot(){ + // + } + + + + /** + * @param hiihto suoritus, joka lisätään rekisteriin + */ + public void lisaa(Hiihto hiihto){ + hiihdot.add(hiihto); + } + + + /** + * Palauttaa viitteen i:teen jäseneen. + * @param i monennenko jäsenen viite halutaan + * @return viite jäseneen, jonka indeksi on i + * @throws IndexOutOfBoundsException jos i ei ole sallitulla alueella + */ + public Hiihto anna(int i) throws IndexOutOfBoundsException { + + if (i < 0 || hiihdot.size() <= i) + throw new IndexOutOfBoundsException("Laiton indeksi: " + i); + return hiihdot.get(i); + } + + + /** + * Lukee hiihdot tiedostosta. + * @param hakemisto tiedoston hakemisto + * @throws SailoException jos lukeminen epäonnistuu + */ + public void lueTiedostosta(String hakemisto) throws SailoException { + tiedostonNimi = hakemisto + "/nimet.dat"; + throw new SailoException("Ei osata vielä lukea tiedostoa " + tiedostonNimi); + } + + + /** + * Tallentaa hiihdon tiedostoon. + * @throws SailoException jos talletus epäonnistuu + */ + public void talleta() throws SailoException { + throw new SailoException("Ei osata vielä tallentaa tiedostoa " + tiedostonNimi); + } + + + /** + * @param id jasenen ID + * @return rekisterin kaikki hiihdot listana + */ + public ArrayList<Hiihto> getJasenenHiihdot(int id) { + ArrayList<Hiihto> jasenenHiihdot = new ArrayList<Hiihto>(); + for (int i = 0; i < hiihdot.size(); i++) { + if (hiihdot.get(i).getJasenID() == id) jasenenHiihdot.add(hiihdot.get(i)); + } + return jasenenHiihdot; + } + + + /** + * @return rekisterin hiihtosuoritusten maara + */ + public int getHiihdot() { + return hiihdot.size(); + } + + + /** + * @param args ei käytössä + */ + public static void main(String args[]) { + Hiihdot hiihdot = new Hiihdot(); + + Hiihto lenkki = new Hiihto(1), toinen = new Hiihto(2); + + try { + hiihdot.lisaa(lenkki); + hiihdot.lisaa(toinen); + + System.out.println("========== Hiihdot testi =============="); + + for (int i=0; i < hiihdot.getHiihdot(); i++) { + Hiihto hiihto = hiihdot.anna(i); + System.out.println("Hiihto nro: " + i); + hiihto.tulosta(System.out); + } + + } catch ( Exception ex ) { + System.out.println(ex.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/Tietorakenteet/Hiihto.java b/src/Tietorakenteet/Hiihto.java new file mode 100644 index 0000000000000000000000000000000000000000..f5532c04c62c0644c23a43919dcfebc6a9b6e4bd --- /dev/null +++ b/src/Tietorakenteet/Hiihto.java @@ -0,0 +1,90 @@ +package Tietorakenteet; + +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Random; + +/** + * @author MHo + * @version 20.2.2020 + * Jasen -luokka, joka lisää jäsenen rekisteriin. + */ +public class Hiihto { + private int id; + private int jasenID; + private String pvm = ""; + private double matka; + private String aika = ""; + private double lampotila; + private double keskisyke; + private double maksimisyke; + private double kalorit; + + private static int juoksevaNro = 1; + + + /** + * Muodostaja + * @param jasenID jolle hiihtosuoritus lisätään + */ + public Hiihto(int jasenID){ + id = juoksevaNro; + this.jasenID = jasenID; + pvm = "5.3.2020"; + matka = new Random().nextInt((50 - 1) + 1) + 1; //arpoo matkan yksilöimistä varten + aika = "1:30"; + lampotila = new Random().nextInt((5 - (-20)) + 1) + (-20); //arpoo lämpötilan yksilöimistä varten + keskisyke = 120; + maksimisyke = 180; + kalorit = 100; + juoksevaNro++; + } + + + /** + * @return hiihdon jasen-ID + */ + public int getJasenID() { + return jasenID; + } + + + + /** + * Haetaan hiihdon kaikki tiedot + * @return hiihtosuorituksen tiedot merkkijonotaulukkona + */ + public String[] getTiedot() { + String[] tiedot = new String[] {pvm, Double.toString(matka), aika, Double.toString(lampotila), + Double.toString(keskisyke), Double.toString(maksimisyke), Double.toString(kalorit)}; + return tiedot; + } + + + /** + * Tulostetaan hiihdon tiedot + * @param out tietovirta johon tulostetaan + */ + public void tulosta(PrintStream out) { + out.println(String.format("%03d", jasenID, 3) + " " + pvm + " " + matka + "km aika" + aika + + " " + lampotila + "Keskisyke: " + keskisyke + " Maksimisyke: " + maksimisyke + " Kalorit: " + kalorit); + } + + + /** + * Tulostetaan hiihdon tiedot + * @param os tietovirta johon tulostetaan + */ + public void tulosta(OutputStream os) { + tulosta(new PrintStream(os)); + } + + + /** + * @param args ei käytössä + */ + public static void main(String args[]) { + Hiihto lenkki = new Hiihto(1); + lenkki.tulosta(System.out); + } +} \ No newline at end of file diff --git a/src/Tietorakenteet/Jasen.java b/src/Tietorakenteet/Jasen.java index 6c818f4d19c9325ee82d391c946a3885f624cd0b..72e83ba2c6ffe1159ead6f24787275325dcdaa26 100644 --- a/src/Tietorakenteet/Jasen.java +++ b/src/Tietorakenteet/Jasen.java @@ -2,6 +2,8 @@ package Tietorakenteet; import java.io.OutputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Random; /** * @author MHo @@ -30,25 +32,37 @@ public class Jasen { pKunta = "Lahti"; puh = 0401234567; sPosti = "nr@niilot.fi"; - liittymisV = 1980; + liittymisV = new Random().nextInt((2020 - 1980) + 1) + 1980; //arpoo liittymisvuoden yksilöimistä varten juoksevaNro++; } + /** + * @return jäsenen ID-numero + */ + public int getID() { + return id; + } + + /** * @return jäsenen nimi */ - public String getNimi(){ + public String getNimi() { return nimi; } + /** - * @return jäsenen ID-numero + * Haetaan henkilön tiedot + * @return jasenen tiedot merkkijono -taulukkona */ - public int getID() { - return id; + public String[] getTiedot() { + String[] tiedot = new String[] {nimi, syntAika, pKunta, Integer.toString(puh), sPosti, Integer.toString(liittymisV)}; + return tiedot; } + /** * Tulostetaan henkilön tiedot * @param out tietovirta johon tulostetaan diff --git a/src/Tietorakenteet/Rekisteri.java b/src/Tietorakenteet/Rekisteri.java index c343e6da3129cbf0c97a0c600fed4f469579f52e..505cd68fd8cd85b6c1cc704a9d1e08eb8989d27a 100644 --- a/src/Tietorakenteet/Rekisteri.java +++ b/src/Tietorakenteet/Rekisteri.java @@ -1,6 +1,6 @@ package Tietorakenteet; -import java.util.*; +import java.util.ArrayList; /** * @author MHo @@ -9,6 +9,7 @@ import java.util.*; */ public class Rekisteri { private final Jasenet jasenet = new Jasenet(); + private final Hiihdot hiihdot = new Hiihdot(); /** @@ -19,7 +20,7 @@ public class Rekisteri { /** - * Lisää jäsenen listaan. + * Lisää jäsenen taulukkoon. * @param jasen lisättävän jäsenen viite * @throws SailoException jos lisäystä ei voida tehdä */ @@ -37,6 +38,34 @@ public class Rekisteri { } + /** + * Lisää hiihto listaan. + * @param hiihto lisättävän hiihdon viite + * @throws SailoException jos lisäystä ei voida tehdä + */ + public void lisaaHiihto(Hiihto hiihto) throws SailoException { + hiihdot.lisaa(hiihto); + } + + + /** + * @param i monennenko jäsenen viite haetaan + * @return viite jäseneen, jonka indeksi on i + */ + public Hiihto annaHiihto(int i) { + return hiihdot.anna(i); + } + + + /** + * @param id hiihtajan ID + * @return halutun jäsenen kaikki hiihdot + */ + public ArrayList<Hiihto> getJasenenHiihdot(int id) { + return hiihdot.getJasenenHiihdot(id); + } + + /** * Lukee jäsenistön tiedostosta. TODO lukeminen tiedostosta * @param nimi jota käytetään lukemisessa diff --git a/src/fxHiihtorekisteri/HiihtorekisteriGUIController.java b/src/fxHiihtorekisteri/HiihtorekisteriGUIController.java index 8ee006a62b744ea8d3b218c0fc816af77edc44c2..624d0a9919318ecc5532dc54016e78819b2a8080 100644 --- a/src/fxHiihtorekisteri/HiihtorekisteriGUIController.java +++ b/src/fxHiihtorekisteri/HiihtorekisteriGUIController.java @@ -1,18 +1,22 @@ package fxHiihtorekisteri; import java.io.PrintStream; +import java.util.ArrayList; +import Tietorakenteet.Hiihto; import Tietorakenteet.Jasen; import Tietorakenteet.Rekisteri; import Tietorakenteet.SailoException; import fi.jyu.mit.fxgui.Dialogs; import fi.jyu.mit.fxgui.ListChooser; import fi.jyu.mit.fxgui.ModalController; +import fi.jyu.mit.fxgui.StringGrid; import fi.jyu.mit.fxgui.TextAreaOutputStream; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.MenuItem; +import javafx.scene.control.TableColumn; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; @@ -60,9 +64,33 @@ public class HiihtorekisteriGUIController { @FXML private ListChooser<Jasen> chooserJasenet; + //@FXML + //private TextArea areaTiedot; + + @FXML + private TextField kenttaNimi; + + @FXML + private TextField kenttaSAika; + + @FXML + private TextField kenttaPKunta; + @FXML - private TextArea textAreaJasen; + private TextField kenttaPuh; + + @FXML + private TextField kenttaEmail; + + @FXML + private TextField kenttaLVuosi; + + @FXML + private TextField kenttaKm; + @FXML + private StringGrid<?> gridHiihdot; + private Rekisteri rekisteri; private Jasen jasenKohdalla; @@ -84,10 +112,25 @@ public class HiihtorekisteriGUIController { jasenKohdalla = chooserJasenet.getSelectedObject(); if (jasenKohdalla == null) return; - jasenKohdalla.tulosta(System.out); - textAreaJasen.setText(""); - try (PrintStream os = TextAreaOutputStream.getTextPrintStream(textAreaJasen)) { - jasenKohdalla.tulosta(os); + + TextField[] kentat = new TextField[] {kenttaNimi, kenttaSAika, kenttaPKunta, kenttaPuh, kenttaEmail, kenttaLVuosi}; + String[] tiedot = jasenKohdalla.getTiedot(); + for (int i = 0; i < tiedot.length; i++) { + kentat[i].setText(tiedot[i]); + } + } + + + /** + * Näyttää listasta valitun jäsenen hiihdot + */ + protected void naytaHiihdot() { + jasenKohdalla = chooserJasenet.getSelectedObject(); + ArrayList<Hiihto> jasenenHiihdot = rekisteri.getJasenenHiihdot(jasenKohdalla.getID()); + gridHiihdot.clear(); + for (int i = 0; i < jasenenHiihdot.size(); i++) { + String[] rivi = jasenenHiihdot.get(i).getTiedot(); + gridHiihdot.add(rivi); } } @@ -99,7 +142,16 @@ public class HiihtorekisteriGUIController { @FXML void handleLisaaHiihto() { - ModalController.showModal(HiihtorekisteriGUIController.class.getResource("LisaaHiihto.fxml"), "Hiihto", null, ""); + //ModalController.showModal(HiihtorekisteriGUIController.class.getResource("LisaaHiihto.fxml"), "Hiihto", null, ""); + jasenKohdalla = chooserJasenet.getSelectedObject(); + int id = jasenKohdalla.getID(); + try { + rekisteri.lisaaHiihto(new Hiihto(id)); + } catch (SailoException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + hae(jasenKohdalla.getID()); } @FXML @@ -114,14 +166,15 @@ public class HiihtorekisteriGUIController { @FXML void handleLisaaHiihtaja() { + Jasen uusi = new Jasen(); try { - rekisteri.lisaa(new Jasen()); + rekisteri.lisaa(uusi); } catch (SailoException e) { // TODO Auto-generated catch block e.printStackTrace(); } //ModalController.showModal(HiihtorekisteriGUIController.class.getResource("LisaaHiihtaja.fxml"), "Hiihtaja", null, ""); - hae(0); + hae(uusi.getID()); } @FXML @@ -156,12 +209,12 @@ public class HiihtorekisteriGUIController { */ protected void hae(int jnro) { chooserJasenet.clear(); - + int index = 0; for (int i = 0; i < rekisteri.getLkm(); i++) { Jasen jasen = rekisteri.annaJasen(i); if (jasen.getID() == jnro) index = i; - chooserJasenet.add(jasen.getID() + jasen.getNimi(), jasen); + chooserJasenet.add(jasen.getID() + " " + jasen.getNimi(), jasen); } chooserJasenet.setSelectedIndex(index); // tästä tulee muutosviesti joka näyttää jäsenen } @@ -173,5 +226,6 @@ public class HiihtorekisteriGUIController { protected void alusta() { chooserJasenet.clear(); chooserJasenet.addSelectionListener(e -> naytaJasen()); + chooserJasenet.addSelectionListener(e -> naytaHiihdot()); } } \ No newline at end of file diff --git a/src/fxHiihtorekisteri/PaaikkunaGUIView.fxml b/src/fxHiihtorekisteri/PaaikkunaGUIView.fxml index f240d20d7b4547e1239acf2f69c9a8043250001b..b882066ca01a345ecf1e5d5d03d31c8d880210e8 100644 --- a/src/fxHiihtorekisteri/PaaikkunaGUIView.fxml +++ b/src/fxHiihtorekisteri/PaaikkunaGUIView.fxml @@ -11,7 +11,6 @@ <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.ScrollPane?> <?import javafx.scene.control.TableColumn?> -<?import javafx.scene.control.TextArea?> <?import javafx.scene.control.TextField?> <?import javafx.scene.input.KeyCodeCombination?> <?import javafx.scene.layout.BorderPane?> @@ -21,7 +20,7 @@ <?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.VBox?> -<BorderPane prefHeight="587.0" prefWidth="673.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxHiihtorekisteri.HiihtorekisteriGUIController"> +<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxHiihtorekisteri.HiihtorekisteriGUIController"> <bottom> <HBox spacing="10.0" BorderPane.alignment="CENTER"> <BorderPane.margin> @@ -58,82 +57,66 @@ </menus> </MenuBar> </top> - <left> - <BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> - <top> - <VBox BorderPane.alignment="CENTER"> - <children> - <ComboBoxChooser rivit="Nimi Paikkakunta " /> - <TextField fx:id="Hakukentta" onAction="#handleHaku" onKeyReleased="#handleHaku" prefHeight="0.0" prefWidth="200.0" /> - <VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0"> - <children> - <Label contentDisplay="CENTER" text="Hiihtäjät" textAlignment="CENTER" wrapText="true" /> - <ListChooser fx:id="chooserJasenet" rivit="nimi" /> - </children> - </VBox> - <VBox alignment="TOP_CENTER"> - <children> - <TextArea fx:id="textAreaJasen" prefHeight="200.0" prefWidth="200.0" /> - <Label contentDisplay="CENTER" text="Hiihtäjän tiedot" textAlignment="CENTER" /> - </children> - </VBox> - <GridPane hgap="10.0" prefHeight="117.0" prefWidth="209.0"> - <columnConstraints> - <ColumnConstraints fillWidth="false" halignment="RIGHT" hgrow="SOMETIMES" /> - <ColumnConstraints hgrow="ALWAYS" maxWidth="140.00004069010416" minWidth="69.33333841959634" prefWidth="83.33335367838542" /> - </columnConstraints> - <rowConstraints> - <RowConstraints vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> - </rowConstraints> - <padding> - <Insets left="10.0" right="10.0" top="10.0" /> - </padding> - <children> - <Label text="Sukunimi" /> - <TextField text="Nokka" GridPane.columnIndex="1" /> - <Label text="Etunimi" GridPane.rowIndex="1" /> - <TextField text="Räkä" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" /> - <Label text="Syntymäaika" GridPane.rowIndex="2" /> - <TextField text="01.08.1955" GridPane.columnIndex="1" GridPane.rowIndex="2" /> - <Label text="Paikkakunta" GridPane.rowIndex="3" /> - <TextField text="Lahti" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" /> - <Label text="Puhelinnumero" GridPane.rowIndex="4" /> - <TextField text="0552012233" GridPane.columnIndex="1" GridPane.rowIndex="4" /> - <Label text="Sähköposti" GridPane.rowIndex="5" /> - <TextField text="nr@niilot.fi" GridPane.columnIndex="1" GridPane.rowIndex="5" /> - <Label text="Liittymisvuosi" GridPane.rowIndex="6" /> - <TextField text="1960" GridPane.columnIndex="1" GridPane.rowIndex="6" /> - <Label text="Km yhteensä" GridPane.rowIndex="7" /> - <TextField text="1200" GridPane.columnIndex="1" GridPane.rowIndex="7" /> - </children> - </GridPane> - </children> - </VBox> - </top> - </BorderPane> - </left> - <center> - <ScrollPane fitToWidth="true" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> + <right> + <ScrollPane fitToWidth="true" BorderPane.alignment="CENTER"> <content> - <StringGrid prefHeight="400.0" prefWidth="443.0"> + <StringGrid fx:id="gridHiihdot" editable="true" rivit="Pvm|Matka| Aika|Lämpötila|Keskisyke|Maksimisyke|Kalorit "> <columns> - <TableColumn minWidth="-1.0" prefWidth="75.0" text="Suorituspvm" /> - <TableColumn prefWidth="75.0" text="Matka" /> - <TableColumn prefWidth="75.0" text="Hiihtoaika" /> - <TableColumn prefWidth="75.0" text="Lämpötila" /> - <TableColumn prefWidth="75.0" text="Keskisyke" /> - <TableColumn prefWidth="75.0" text="Maksimisyke" /> - <TableColumn prefWidth="75.0" text="Kalorit" /> + <TableColumn fx:id="Pvm" minWidth="-1.0" prefWidth="75.0" text="Suorituspvm" /> + <TableColumn fx:id="Matka" prefWidth="75.0" text="Matka" /> + <TableColumn fx:id="Aika" prefWidth="75.0" text="Hiihtoaika" /> + <TableColumn fx:id="LTila" prefWidth="75.0" text="Lämpötila" /> + <TableColumn fx:id="KSyke" prefWidth="75.0" text="Keskisyke" /> + <TableColumn fx:id="MSyke" prefWidth="75.0" text="Maksimisyke" /> + <TableColumn fx:id="Kalorit" prefWidth="75.0" text="Kalorit" /> </columns> </StringGrid> </content> </ScrollPane> - </center> + </right> + <left> + <VBox BorderPane.alignment="CENTER"> + <children> + <ComboBoxChooser rivit="Nimi Paikkakunta " /> + <TextField fx:id="Hakukentta" onAction="#handleHaku" onKeyReleased="#handleHaku" prefHeight="0.0" prefWidth="200.0" /> + <Label contentDisplay="CENTER" text="Hiihtäjät" textAlignment="CENTER" wrapText="true" /> + <ListChooser fx:id="chooserJasenet" maxHeight="100.0" maxWidth="300.0" rivit="nimi" /> + <Label contentDisplay="CENTER" text="Hiihtäjän tiedot" textAlignment="CENTER" /> + <GridPane hgap="10.0" maxHeight="200.0"> + <columnConstraints> + <ColumnConstraints fillWidth="false" halignment="RIGHT" hgrow="SOMETIMES" /> + <ColumnConstraints hgrow="ALWAYS" maxWidth="140.00004069010416" minWidth="69.33333841959634" prefWidth="83.33335367838542" /> + </columnConstraints> + <rowConstraints> + <RowConstraints vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints prefHeight="30.0" vgrow="SOMETIMES" /> + </rowConstraints> + <padding> + <Insets left="10.0" right="10.0" top="10.0" /> + </padding> + <children> + <Label text="Nimi" /> + <TextField fx:id="kenttaNimi" GridPane.columnIndex="1" /> + <Label text="Syntymäaika" GridPane.rowIndex="1" /> + <TextField fx:id="kenttaSAika" GridPane.columnIndex="1" GridPane.rowIndex="1" /> + <Label text="Paikkakunta" GridPane.rowIndex="2" /> + <TextField fx:id="kenttaPKunta" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" /> + <Label text="Puhelinnumero" GridPane.rowIndex="3" /> + <TextField fx:id="kenttaPuh" GridPane.columnIndex="1" GridPane.rowIndex="3" /> + <Label text="Sähköposti" GridPane.rowIndex="4" /> + <TextField fx:id="kenttaEmail" GridPane.columnIndex="1" GridPane.rowIndex="4" /> + <Label text="Liittymisvuosi" GridPane.rowIndex="5" /> + <TextField fx:id="kenttaLVuosi" GridPane.columnIndex="1" GridPane.rowIndex="5" /> + <Label text="Km yhteensä" GridPane.rowIndex="6" /> + <TextField fx:id="kenttaKm" GridPane.columnIndex="1" GridPane.rowIndex="6" /> + </children> + </GridPane> + </children> + </VBox> + </left> </BorderPane>