# GUI für einen Shop



## SpicyMagic (8. Jan 2020)

public class ShopPane extends BorderPane {

    private static final String CONSOLES = "Konsolen";
    private static final String VIDEO_GAMES = "Videospiele";

    private BorderPane leftPane;

    private ComboBox<String> articleTypeBox;

    // general for center
    private TextField nameTextField;

    // specific for video games   
    private TextField ageRestrictionTextField;
    private ComboBox<Genre> genreBox;

    // specific for consoles
    private ComboBox<Manufacturer> manufacturerBox;

    private HBox buttonBox;
    private Button buyButton;
    private Button sellButton;

    private Storage<VideoGame> videoGameStorage;
    private Storage<GamingConsole> gamingConsoleStorage;

    private TableView<VideoGame> videoGameTableView;
    private TableView<GamingConsole> gamingConsoleTableView;

    public ShopPane() {
        this.videoGameStorage = new VideoGameStorage();
        this.gamingConsoleStorage = new GamingConsoleStorage();

        this.videoGameTableView = this.createVideoGameTableView(this.videoGameStorage.getArticles());
        this.gamingConsoleTableView = this.createGamingConsoleTableView(this.gamingConsoleStorage.getArticles());

        this.nameTextField = new TextField();
        this.nameTextField.setPromptText("Name");

        this.ageRestrictionTextField = new TextField();
        this.ageRestrictionTextField.setPromptText("Age Restriction (int)");

        this.genreBox = new ComboBox<>();
        this.genreBox.getItems().addAll(Genre.values());
        this.genreBox.getSelectionModel().selectFirst();

        this.manufacturerBox = new ComboBox<>();
        this.manufacturerBox.getItems().addAll(Manufacturer.values());
        this.manufacturerBox.getSelectionModel().selectFirst();

        this.articleTypeBox = new ComboBox<String>(FXCollections.observableArrayList(CONSOLES, VIDEO_GAMES));
        this.articleTypeBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.equals(CONSOLES)) {
                this.leftPane.setCenter(this.createConsoleCenter());
                this.setCenter(this.gamingConsoleTableView);
            } else {
                this.leftPane.setCenter(this.createVideoGameCenter());
                this.setCenter(this.videoGameTableView);
            }
        });
        this.leftPane = new BorderPane();
        this.leftPane.setTop(this.articleTypeBox);
        this.setLeft(this.leftPane);
        this.leftPane.setBottom(this.createButtonBox());
        this.articleTypeBox.getSelectionModel().selectFirst();
        // this.articleTypeBox.setValue(CONSOLES);
    }

    private Node createButtonBox() {
        this.buttonBox = new HBox();
        this.buyButton = new Button("Kaufen");
        this.buyButton.setOnAction(event -> {
            if (this.articleTypeBox.getValue().equals(CONSOLES)) {
                GamingConsole console = new GamingConsole(this.nameTextField.getText(),
                        this.manufacturerBox.getValue());
                this.gamingConsoleStorage.addArticle(console);
            } else { // VIDEO_GAMES
                VideoGame game = new VideoGame(this.nameTextField.getText(), this.genreBox.getValue());
                String ageRestrictionString = this.ageRestrictionTextField.getText();
                if (ageRestrictionString == null || ageRestrictionString.isEmpty()) {
                    this.videoGameStorage.addArticle(game);
                } else if (ageRestrictionString.matches("[0-9]+")) {
                    game.setAgeRestriction(Integer.parseInt(ageRestrictionString));
                    this.videoGameStorage.addArticle(game);
                }
            }
        });
        this.sellButton = new Button("Verkaufen");
        this.sellButton.setOnAction(event -> {
            if (this.articleTypeBox.getValue().equals(CONSOLES)) {
                GamingConsole console = this.gamingConsoleTableView.getSelectionModel().getSelectedItem();
                if (console != null) {
                    this.gamingConsoleStorage.getArticles().remove(console);
                }
            } else { // VIDEO_GAMES
                VideoGame game = this.videoGameTableView.getSelectionModel().getSelectedItem();
                if (game != null) {
                    this.videoGameStorage.getArticles().remove(game);
                }
            }
        });
        this.buttonBox.getChildren().addAll(this.buyButton, this.sellButton);
        return this.buttonBox;
    }

    private Node createVideoGameCenter() {
        VBox vbox = new VBox();
        vbox.getChildren().add(this.nameTextField);
        vbox.getChildren().add(this.genreBox);
        vbox.getChildren().add(this.ageRestrictionTextField);
        return vbox;
    }

    private Node createConsoleCenter() {
        VBox vbox = new VBox();
        vbox.getChildren().add(this.nameTextField);
        vbox.getChildren().add(this.manufacturerBox);
        return vbox;
    }

    private TableView<VideoGame> createVideoGameTableView(ObservableList<VideoGame> videoGameList) {
        TableColumn<VideoGame, String> idColumn = new TableColumn<VideoGame, String>("ID");
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        TableColumn<VideoGame, String> nameColumn = new TableColumn<VideoGame, String>("Name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<VideoGame, String> genreColumn = new TableColumn<VideoGame, String>("Genre");
        genreColumn.setCellValueFactory(new PropertyValueFactory<>("genre"));
        TableColumn<VideoGame, String> ageRestrictionColumn = new TableColumn<VideoGame, String>("Age Restriction");
        ageRestrictionColumn.setCellValueFactory(new PropertyValueFactory<>("ageRestriction"));
        TableView<VideoGame> tableView = new TableView<VideoGame>(videoGameList);
        tableView.getColumns().add(idColumn);
        tableView.getColumns().add(nameColumn);
        tableView.getColumns().add(genreColumn);
        tableView.getColumns().add(ageRestrictionColumn);

        return tableView;
    }

    private TableView<GamingConsole> createGamingConsoleTableView(ObservableList<GamingConsole> gamingConsoleList) {
        TableColumn<GamingConsole, String> idColumn = new TableColumn<GamingConsole, String>("ID");
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        TableColumn<GamingConsole, String> nameColumn = new TableColumn<GamingConsole, String>("Name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<GamingConsole, String> manufacturerColumn = new TableColumn<GamingConsole, String>("Manufacturer");
        manufacturerColumn.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
        TableView<GamingConsole> tableView = new TableView<>(gamingConsoleList);
        tableView.getColumns().add(idColumn);
        tableView.getColumns().add(nameColumn);
        tableView.getColumns().add(manufacturerColumn);

        return tableView;
    }

}


----------



## SpicyMagic (8. Jan 2020)

public class ShopApplication extends Application {

    public static void main(String... args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Videogame Shop");
        ShopPane pane = new ShopPane();
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

}


----------



## kneitzel (8. Jan 2020)

Ein paar Punkte:
a) Code bitte immer in Code Tags - sonst ist es nicht leserlich
b) Bitte deutlich sagen, was Du bezweckst.

Mir ist im Augenblick unklar, was Du von uns willst. Willst Du nur Deinen Code teilen? Willst Du Feedback? Gibt es ein Problem (Dann bitte die genauen Fehlerdetails bringen!)


----------



## MoxxiManagarm (8. Jan 2020)

https://www.java-forum.org/thema/java-code-drueberlesen-sieht-das-richtig-aus.186907/  enthält auch einen Videogame-Shop. Vielleicht solltet ihr euch zusammentun.


----------

