Anonymous Anonymous - 5 months ago
238 0

No description

Java

JavaFX Aufgabe

import java.time.LocalDate;
import javafx.application.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.*;
import javafx.scene.control.*;

public class GewichtsController extends Application {
	
  @FXML
  private TableView<Eintrag> tvEintraege;
  @FXML
  private TableColumn<Eintrag, LocalDate> tcDatum;
  @FXML
  private TableColumn<Eintrag, Number> tcTagesgewicht;
  @FXML
  private LineChart<LocalDate, Boolean> lcVerlauf;
  @FXML
  private DatePicker dpDatum;
  @FXML
  private TextField tfGewicht;
  @FXML
  private RadioButton rdBtnAllData, rdBtnWeek, rdBtnMonth;
  @FXML
  private RadioButton rdBtnIdealgewicht, rdBtnFrau, rdBtnMann;
  
  private Model model = new Model();
  private Rechner rechner = new Rechner();
  
	@SuppressWarnings("rawtypes")
	private ObservableList lineChartData;
	@SuppressWarnings("rawtypes")
	private Series lineChartSeries;
	@SuppressWarnings("rawtypes")
	private ObservableList lineChartEntries;
	
	@SuppressWarnings("rawtypes")
	private Series lineChartIdealSeries;
	@SuppressWarnings("rawtypes")
	private ObservableList lineChartIdealEntries;

	// TODO FEHLERBEHEBUNG!
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
  public void start(Stage stage) {
    try {
    	Pane root = FXMLLoader.load(getClass().getResource("gewichtstagebuch.fxml"));
    	Scene scene = new Scene(root);
    	stage.setScene(scene);
    	stage.setTitle("Gewichtstagebuch");
    	stage.show();
    	
    } catch (Exception ex) {
    	ex.printStackTrace();
    }
  }
  
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initialize() {
		// TODO
  	/*tcDatum.setCellValueFactory(cellData -> cellData.getValue().datumProperty());
  	tcTagesgewicht.setCellValueFactory(cellData -> cellData.getValue().gewichtProperty());*/
    
  	// Gewicht
    lineChartEntries = FXCollections.observableArrayList();
    lineChartSeries = new LineChart.Series("Verlauf", lineChartEntries);
    // Idealgewicht
    lineChartIdealEntries = FXCollections.observableArrayList();
    lineChartIdealSeries = new LineChart.Series("Ideal", lineChartIdealEntries);
    // LineChart-Daten hinzufügen
    lineChartData = FXCollections.observableArrayList();
    lineChartData.addAll(lineChartSeries, lineChartIdealSeries);
    lcVerlauf.dataProperty().setValue(lineChartData);
    
    // Table-View füllen
    tvEintraege.setItems(model.eintraegeProperty());
    
    ToggleGroup filterTime = new ToggleGroup();
    rdBtnAllData.setToggleGroup(filterTime);
    rdBtnWeek.setToggleGroup(filterTime);
    rdBtnMonth.setToggleGroup(filterTime);
    rdBtnAllData.selectedProperty().set(true);
    
    ToggleGroup idealgewicht = new ToggleGroup();
    rdBtnIdealgewicht.setToggleGroup(idealgewicht);
    rdBtnFrau.setToggleGroup(idealgewicht);
    rdBtnMann.setToggleGroup(idealgewicht);
    rdBtnIdealgewicht.selectedProperty().set(true);
  }
 
	@FXML
  void speichernClickAction(ActionEvent event) {
  	Eintrag eintrag = new Eintrag(dpDatum.getValue(), Double.parseDouble(tfGewicht.getText()));
  	
  	model.addEintrag(eintrag);
  	updateLineChart();
  }
	
	@FXML
  void loeschenClickAction(ActionEvent event) {
		LocalDate datum = dpDatum.getValue();
		
		model.removeEintrag(datum);
		updateLineChart();
	}
	
  @FXML
  void allDataClickAction(ActionEvent event) {
  	model.filterAllData();
  	updateLineChart();
  }

  @FXML
  void weekClickAction(ActionEvent event) {
  	model.filterWeek();
  	updateLineChart();
  }
  
  @FXML
  void monthClickAction(ActionEvent event) {
  	model.filterMonth();
  	updateLineChart();
  }

  @FXML
  void keinIdealgewichtClickAction(ActionEvent event) {
  	lineChartIdealEntries.clear();
  }
  
  @FXML
  void frauClickAction(ActionEvent event) {
  	// TODO
  	TextInputDialog dialog = new TextInputDialog("walter");
  	dialog.setTitle("Text Input Dialog");
  	dialog.setHeaderText("Look, a Text Input Dialog");
  	dialog.setContentText("Please enter your name:");

  	double groesse = 165;
  	berechneIdealgewicht((RadioButton) event.getSource(), groesse);
  }
  
	@FXML
	void mannClickAction(ActionEvent event) {
		// TODO
		double groesse = 175;
		berechneIdealgewicht((RadioButton) event.getSource(), groesse);
	}

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void updateLineChart() {
  	lineChartEntries.clear();
  	for (Eintrag eintr: model.eintraegeProperty()) {
  		XYChart.Data xYChartData = new XYChart.Data(
    	  	eintr.datumProperty().getValue().toString(),
    	  	eintr.gewichtProperty().getValue()
    	  );
  		lineChartEntries.add(xYChartData);
  	}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void berechneIdealgewicht(RadioButton radioButton, double groesse) {
		if (radioButton.getId().equals("rdBtnFrau")) {
			rechner.istFrauProperty().setValue(true);
		} else {
			rechner.istFrauProperty().setValue(false);;
		}
		
		rechner.groesseProperty().setValue(groesse);
		
		lineChartIdealEntries.clear();
		for (Eintrag eintr : model.eintraegeProperty()) {
  		XYChart.Data xYChartData = new XYChart.Data(
    	  	eintr.datumProperty().getValue().toString(),
    	  	rechner.idealGewichtProperty().getValue()
    	  );
  		lineChartIdealEntries.add(xYChartData);
		}
	}
}
Comments