0

Java Kurs Proje Kodları ( Okuldan – ikinciSahibinden) Bölüm -1

Merhaba arkadaşlar okuldaki java kursumuzda örnek amaçlı yaptığımız proje yetişmeyince buradan paylaşmak gerekti. Henüz buraya kadar ki kısımda görsel arayüzü hazırlamış ve kontrollere ait verileri yüklemiş olduk. Bir sonraki adımda girilen bilgileri tabloya aktaracağız ve filtreleme yapacağız. Tabi ilerleyen süreçte farklı işlemlerde yaparak projeyi belkide büyüteceğiz. Projedeki kodların çokluğu sizi korkutmasın aslında hep temel işlemler uyguladık. Herkese kolay gelsin …

// Controller.java
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @FXML
    ComboBox comboMarkalar,comboKasaTipi,comboMotorHacim;
    @FXML
    ChoiceBox choiceVitesTuru,choiceYakit;
    @FXML
    ToggleButton toggleHasar;

    @FXML
    TextField textModelYili,textPlaka,textFiyat;

    @FXML
    ColorPicker colorPickerRenk;
    @FXML
    Button buttonKaydet;

    @FXML
    Label colorLabel;

    @FXML
    TableView<Araba>  tableKayitlar;

    ObservableList<Araba> kayitlar= FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {



        comboMarkalar.setItems(Markalar.getMarkalar());
        comboMarkalar.setPromptText("Lütfen Bir Marka Seçiniz");

        comboKasaTipi.setItems(Ozellikler.getKasaTipleri());
        comboKasaTipi.getSelectionModel().selectFirst();

        comboMotorHacim.setItems(Ozellikler.getMotorHacimleri());
        comboMotorHacim.getSelectionModel().select(3);

        choiceVitesTuru.setItems(Ozellikler.getVitesTurleri());
        choiceVitesTuru.getSelectionModel().selectFirst();

        choiceYakit.setItems(Ozellikler.getyakitTurleri());
        choiceYakit.getSelectionModel().selectFirst();

        toggleHasar.setText("Hasar Yok");

        toggleHasar.setOnAction(event -> {
            if(toggleHasar.isSelected()){
                toggleHasar.setText("Hasarlı");
            }
            else
            {
                toggleHasar.setText("Hasar Yok");
            }
        });

        TableColumn<Araba,String> markaCol=new TableColumn<>("Marka");
        markaCol.setCellValueFactory(new PropertyValueFactory<>("marka"));

        TableColumn<Araba,Integer> modelYiliCol=new TableColumn<>("Model Yılı");
        modelYiliCol.setCellValueFactory(new PropertyValueFactory<>("modelYili"));

        TableColumn<Araba,String> renkCol=new TableColumn<>("Renk");
        renkCol.setCellValueFactory(new PropertyValueFactory<>("renk"));



        TableColumn<Araba,String> kasaTipiCol=new TableColumn<>("Kasa Tipi");
        kasaTipiCol.setCellValueFactory(new PropertyValueFactory<>("kasaTipi"));

        TableColumn<Araba,Integer> motorHacmiCol=new TableColumn<>("Motor Hacmi");
        motorHacmiCol.setCellValueFactory(new PropertyValueFactory<>("motorHacmi"));

        TableColumn<Araba,String> vitesTuruCol=new TableColumn<>("Vites Türü");
        vitesTuruCol.setCellValueFactory(new PropertyValueFactory<>("vitesTuru"));
        vitesTuruCol.setVisible(false);

        TableColumn<Araba,String> yakitCol=new TableColumn<>("Yakıt");
        yakitCol.setCellValueFactory(new PropertyValueFactory<>("yakit"));

        TableColumn<Araba,String> hasarDurumCol=new TableColumn<>("Hasar Durumu");
        hasarDurumCol.setCellValueFactory(new PropertyValueFactory<>("hasarDurum"));

        TableColumn<Araba,String> plakaCol=new TableColumn<>("Plaka");
        plakaCol.setCellValueFactory(new PropertyValueFactory<>("plaka"));
        plakaCol.setCellFactory(new Callback<TableColumn<Araba, String>, TableCell<Araba, String>>() {
            @Override
            public TableCell<Araba, String> call(TableColumn<Araba, String> param) {



                TableCell<Araba,String> cell=new TableCell<>(){

                    Tooltip tooltip=new Tooltip();
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if(!empty){

                            setText(item);
                            tooltip.setText(getTableView().getItems().get(getIndex()).vitesTuru);
                            setTooltip(tooltip);
                        }
                    }
                };

                return cell;

            }
        });



        TableColumn<Araba,Long> fiyatCol=new TableColumn<>("Fiyat");
        fiyatCol.setCellValueFactory(new PropertyValueFactory<>("fiyat"));

        fiyatCol.setCellFactory(new Callback<TableColumn<Araba, Long>, TableCell<Araba, Long>>() {

            @Override
            public TableCell<Araba, Long> call(TableColumn<Araba, Long> param) {

                TableCell<Araba,Long> cell=new TableCell<>(){

                    @Override
                    protected void updateItem(Long item, boolean empty) {
                        super.updateItem(item, empty);

                        if(!empty){

                            if(item>100000){
                               setText(item.toString());
                                setStyle("-fx-background-color: #ff0000");
                            }
                            if(item<100000){

                                setText(item.toString());
                                setStyle("-fx-background-color: #00ff00");
                            }

                        }


                    }
                };

                return cell;
            }



        });


        tableKayitlar.getColumns().addAll(markaCol,modelYiliCol,renkCol,kasaTipiCol,motorHacmiCol,vitesTuruCol,yakitCol,hasarDurumCol,plakaCol,fiyatCol);

        tableKayitlar.setItems(kayitlar);

        buttonKaydet.setOnAction(event -> {
            // Kaydetme butonana basıldğınad yapılacak işler

            String marka=comboMarkalar.getSelectionModel().getSelectedItem().toString();
            int modelYili= Integer.parseInt(textModelYili.getText());
            String renk=  colorPickerRenk.getValue().toString().substring(1,colorPickerRenk.getValue().toString().length());
            System.out.println(renk);
            String kasaTipi=comboKasaTipi.getSelectionModel().getSelectedItem().toString();
            int motorHacmi=Integer.parseInt(comboMotorHacim.getSelectionModel().getSelectedItem().toString());
            String vitesTuru=choiceVitesTuru.getValue().toString();
            String yakit=choiceYakit.getValue().toString();
            String hasarDurum=toggleHasar.getText();
            String plaka=textPlaka.getText();
            long fiyat=Long.parseLong(textFiyat.getText());

            kayitlar.add(new Araba(marka,modelYili,renk,kasaTipi,motorHacmi,vitesTuru,yakit,hasarDurum,plaka,fiyat));


        });


        kayitlar.add(new Araba("BMW",2011,"Kırmızı","ST Wagon",1600,"manuel","LPG","yok","06 EE 1111",150000));

        kayitlar.add(new Araba("Audi",2008,"Yeşil","Sedan",1300,"otomatik","Benzin","yok","07 CE 135",210000));
        kayitlar.add(new Araba("Mercedes",2010,"Gri","HB",1600,"yarı otomatik","LPG","yok","06 KP 296",190000));
        kayitlar.add(new Araba("Renault",2016,"Sarı","Sedan",1800,"otomatik","Benzin","yok","06 SL 288",70000));
        kayitlar.add(new Araba("Honda",2013,"Beyaz","Sedan",2000,"yarı otomatik","Dizel","yok","06 FF 65",110000));
        kayitlar.add(new Araba("Toyota",2014,"Siyah","Sedan",1600,"manuel","Benzin","yok","06 ES 3654",60000));
        kayitlar.add(new Araba("Ford",2018,"Mor","HB",1800,"otomatik","Benzin","yok","07 EE 28",80000));


    }
}
//Ozellikler.java
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public  class Ozellikler {




        public static ObservableList<String> getKasaTipleri(){

            return FXCollections.observableArrayList("Sedan","HatchBack","Station Wagon");

        }

        public static ObservableList<Integer> getMotorHacimleri(){


            return FXCollections.observableArrayList(1000,1200,1400,1600,1800,2000);
        }


    public static ObservableList<String> getVitesTurleri(){


        return FXCollections.observableArrayList("Düz Vites","Yarı Otomatik","Tam Otomatik");

    }

    public static ObservableList<String> getyakitTurleri(){


        return FXCollections.observableArrayList("Benzin","Dizel","Lpg","Hybrit","Elektrikli");

    }





}
//Markalar.java
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Markalar {

   private static ObservableList<String> markalar= FXCollections.observableArrayList();
    private static String[] markaListesi={"Audi","BMW","Citroen","Honda","Opel","Toyota","Volkswagen"};

    public static ObservableList<String> getMarkalar(){
      markalar.addAll(Arrays.asList(markaListesi));
        return markalar;
    }


}
// arayuz.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.effect.Reflection?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane prefHeight="704.0" prefWidth="1059.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <VBox prefHeight="704.0" prefWidth="163.0" style="-fx-background-color: #212121;">
         <children>
            <Button mnemonicParsing="false" prefHeight="125.0" prefWidth="164.0" style="-fx-background-color: #263238; -fx-text-fill: #fff;" text="Yeni Araç Ekle">
               <font>
                  <Font size="15.0" />
               </font>
               <effect>
                  <DropShadow />
               </effect>
            </Button>
            <Button layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="125.0" prefWidth="164.0" style="-fx-background-color: #263238; -fx-text-fill: #fff;" text="Yeni Araç Ekle">
               <font>
                  <Font size="15.0" />
               </font>
            </Button>
            <Button layoutX="10.0" layoutY="135.0" mnemonicParsing="false" prefHeight="125.0" prefWidth="164.0" style="-fx-background-color: #263238; -fx-text-fill: #fff;" text="Çıkış Yap">
               <font>
                  <Font size="15.0" />
               </font>
               <effect>
                  <DropShadow />
               </effect>
            </Button>
         </children>
      </VBox>
      <VBox layoutX="208.0" layoutY="-2.0" prefHeight="282.0" prefWidth="833.0" style="-fx-background-color: #F5F5F5;">
         <children>
            <HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
               <children>
                  <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Araç Bilgi Kayıt Formu">
                     <font>
                        <Font size="19.0" />
                     </font>
                     <effect>
                        <Reflection />
                     </effect>
                  </Text>
               </children>
            </HBox>
            <Separator prefWidth="200.0" />
            <GridPane alignment="CENTER" prefHeight="219.0" prefWidth="804.0">
              <columnConstraints>
                <ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" maxWidth="395.0" minWidth="10.0" prefWidth="250.0" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="395.0" minWidth="10.0" prefWidth="450.0" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="395.0" minWidth="10.0" prefWidth="175.0" />
                  <ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" maxWidth="395.0" minWidth="10.0" prefWidth="300.0" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="395.0" minWidth="10.0" prefWidth="450.0" />
                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="629.0" minWidth="10.0" prefWidth="0.0" />
              </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label alignment="CENTER" text="Araç Marka" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label text="Model Yılı" GridPane.rowIndex="1">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label text="Renk" GridPane.rowIndex="2">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label text="Kasa Tipi" GridPane.rowIndex="3">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label text="Motor Hacmi" GridPane.rowIndex="4">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <TextField fx:id="textModelYili" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <Label alignment="CENTER" text="Vites Türü" GridPane.columnIndex="3" GridPane.halignment="RIGHT">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label alignment="CENTER" text="Yakıt" GridPane.columnIndex="3" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label alignment="CENTER" text="Hasar Durumu" GridPane.columnIndex="3" GridPane.halignment="RIGHT" GridPane.rowIndex="2">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label alignment="CENTER" text="Plakası " GridPane.columnIndex="3" GridPane.halignment="RIGHT" GridPane.rowIndex="3">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <Label alignment="CENTER" text="Fiyat" GridPane.columnIndex="3" GridPane.halignment="RIGHT" GridPane.rowIndex="4">
                     <padding>
                        <Insets right="10.0" />
                     </padding>
                  </Label>
                  <TextField fx:id="textPlaka" GridPane.columnIndex="4" GridPane.rowIndex="3" />
                  <TextField fx:id="textFiyat" GridPane.columnIndex="4" GridPane.rowIndex="4" />
                  <ComboBox fx:id="comboMarkalar" prefHeight="25.0" prefWidth="250.0" GridPane.columnIndex="1" />
                  <ComboBox fx:id="comboKasaTipi" prefHeight="25.0" prefWidth="148.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                  <ComboBox fx:id="comboMotorHacim" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                  <ChoiceBox fx:id="choiceVitesTuru" prefWidth="150.0" GridPane.columnIndex="4" />
                  <ChoiceBox fx:id="choiceYakit" prefWidth="150.0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
                  <Button fx:id="buttonKaydet" mnemonicParsing="false" prefHeight="37.0" prefWidth="144.0" text="Kaydet" GridPane.columnIndex="4" GridPane.halignment="RIGHT" GridPane.rowIndex="5" />
                  <ColorPicker fx:id="colorPickerRenk" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <ToggleButton fx:id="toggleHasar" mnemonicParsing="false" text="Hasar Durumu" GridPane.columnIndex="4" GridPane.rowIndex="2" />
               </children>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </GridPane>
         </children>
      </VBox>
      <Separator layoutX="224.0" layoutY="279.0" prefHeight="0.0" prefWidth="804.0" />
      <VBox layoutX="224.0" layoutY="295.0" prefHeight="407.0" prefWidth="804.0" spacing="10.0">
         <children>
            <TableView fx:id="tableKayitlar" prefHeight="333.0" prefWidth="804.0">
              <columns>
                <TableColumn prefWidth="75.0" text="C1" />
                <TableColumn prefWidth="75.0" text="C2" />
              </columns>
            </TableView>
            <HBox alignment="CENTER_LEFT" prefHeight="51.0" prefWidth="804.0">
               <children>
                  <Label text="Filterele  :" />
                  <TextField fx:id="textFiltrele" prefHeight="27.0" prefWidth="319.0">
                     <HBox.margin>
                        <Insets left="10.0" />
                     </HBox.margin>
                     <padding>
                        <Insets top="10.0" />
                     </padding>
                  </TextField>
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>
// main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("arayuz.fxml"));
        primaryStage.setTitle("İkinci Sahibinden Araç Kayıt Sistemi v 1.0");
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
package sample;

public class Araba {

    String marka;
    int modelYili;
    String renk;
    String kasaTipi;
    int motorHacmi;
    String vitesTuru;
    String yakit;
    String hasarDurum;
    String plaka;
    long fiyat;


    public Araba(String marka, int modelYili, String renk, String kasaTipi, int motorHacmi, String vitesTuru, String yakit, String hasarDurum, String plaka, long fiyat) {
        this.marka = marka;
        this.modelYili = modelYili;
        this.renk = renk;
        this.kasaTipi = kasaTipi;
        this.motorHacmi = motorHacmi;
        this.vitesTuru = vitesTuru;
        this.yakit = yakit;
        this.hasarDurum = hasarDurum;
        this.plaka = plaka;
        this.fiyat = fiyat;
}

    public String getMarka() {
        return marka;
    }

    public void setMarka(String marka) {
        this.marka = marka;
    }

    public int getModelYili() {
        return modelYili;
    }

    public void setModelYili(int modelYili) {
        this.modelYili = modelYili;
    }

    public String getRenk() {
        return renk;
    }

    public void setRenk(String renk) {
        this.renk = renk;
    }

    public String getKasaTipi() {
        return kasaTipi;
    }

    public void setKasaTipi(String kasaTipi) {
        this.kasaTipi = kasaTipi;
    }

    public int getMotorHacmi() {
        return motorHacmi;
    }

    public void setMotorHacmi(int motorHacmi) {
        this.motorHacmi = motorHacmi;
    }

    public String getVitesTuru() {
        return vitesTuru;
    }

    public void setVitesTuru(String vitesTuru) {
        this.vitesTuru = vitesTuru;
    }

    public String getYakit() {
        return yakit;
    }

    public void setYakit(String yakit) {
        this.yakit = yakit;
    }

    public String getHasarDurum() {
        return hasarDurum;
    }

    public void setHasarDurum(String hasarDurum) {
        this.hasarDurum = hasarDurum;
    }

    public String getPlaka() {
        return plaka;
    }

    public void setPlaka(String plaka) {
        this.plaka = plaka;
    }

    public long getFiyat() {
        return fiyat;
    }

    public void setFiyat(long fiyat) {
        this.fiyat = fiyat;
    }
}

 

admin

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir