I am trying to store one of three items a book, cd, or dvd into an array but my add method is not working.
here is the GUI Class.
/**
Class BookStoreApplication GUI represents a book store.
It gives the user the option to
- add a book to the store's inventory
- list all books in the store's inventory
Author: YOUR FULL NAME HERE
E-mail address: YOUR E-MAIL ADDRESS
Last changed: TODAY'S DATE
Lab 01
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class BookStoreGUI extends Application {
private String title;
private String author;
private double price;
private static int pages;
private static int playingTime;
private Label titleLabel;
private Label authorLabel;
private Label priceLabel;
private TextField titleTextField;
private TextField authorTextField;
private TextField priceTextField;
private GridPane inputGrid;
private RadioButton BookRadioButton;
private RadioButton CDRadioButton;
private RadioButton DVDRadioButton;
private VBox RadioVBox;
private ToggleGroup mediaGroup;
private Button addItemButton;
private Button displayInventoryButton;
private HBox buttonHBox;
private TextArea outputText;
private BookStoreHandler handler;
private Stage maessageWindow;
private GridPane topGrid;
private BorderPane mainPane;
private static int count = 0;
private static BookStoreItem[] Catalog;
public void start(Stage primaryStage) {
createInputComponents();
createOutputComponents();
createButtons();
createWindow();
Scene scene = new Scene(mainPane, 400, 500);
primaryStage.setTitle("Book Sotre");
primaryStage.setScene(scene);
primaryStage.show();
}
private void createInputComponents(){
titleLabel = new Label("Title");
authorLabel = new Label("Author's name");
priceLabel = new Label ("Price");
titleTextField = new TextField();
authorTextField = new TextField();
priceTextField = new TextField();
inputGrid = new GridPane();
inputGrid.setHgap(15);
inputGrid.setVgap(15);
inputGrid.add(titleLabel, 0 , 0);
inputGrid.add(titleTextField, 1, 0);
inputGrid.add(authorLabel, 0, 1);
inputGrid.add(authorTextField, 1, 1);
inputGrid.add(priceLabel, 0 , 2);
inputGrid.add(priceTextField, 1, 2);
CDRadioButton = new RadioButton("CD");
BookRadioButton = new RadioButton("Book");
DVDRadioButton = new RadioButton("DVD");
mediaGroup = new ToggleGroup();
CDRadioButton.setToggleGroup(mediaGroup);
BookRadioButton.setToggleGroup(mediaGroup);
DVDRadioButton.setToggleGroup(mediaGroup);
BookRadioButton.setSelected(true);
RadioVBox = new VBox(15);
RadioVBox.getChildren().add(CDRadioButton);
RadioVBox.getChildren().add(BookRadioButton);
RadioVBox.getChildren().add(DVDRadioButton);
topGrid = new GridPane();
topGrid.setHgap(20);
topGrid.setAlignment(Pos.CENTER);
topGrid.add(inputGrid, 0, 0);
topGrid.add(RadioVBox, 1, 0);
}
private void createOutputComponents () {
outputText = new TextArea();
outputText.setEditable(false);
}
private void createButtons() {
addItemButton = new Button("Add Item");
displayInventoryButton = new Button("Display Inventory");
addItemButton.setPrefSize(100, 50);
displayInventoryButton.setPrefSize(100, 50);
buttonHBox = new HBox(100);
buttonHBox.setAlignment(Pos.CENTER);
buttonHBox.getChildren().add(addItemButton);
buttonHBox.getChildren().add(displayInventoryButton);
handler = new BookStoreHandler();
addItemButton.setOnAction(handler);
displayInventoryButton.setOnAction(handler);
}
private void createWindow() {
mainPane = new BorderPane();
mainPane.setTop(topGrid);
mainPane.setCenter(outputText);
mainPane.setBottom(buttonHBox);
}
private class BookStoreHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent ae) {
BookStoreItem media;
if(ae.getSource () == addItemButton){
outputText.setText("");
title = titleTextField.getText().trim();
author = authorTextField.getText().trim();
price = Double.parseDouble(priceTextField.getText().trim());
if (CDRadioButton.isSelected()){
media = new CD(title, author, price, 0);
}
else if(DVDRadioButton.isSelected()) {
media = new DVD(title, author, price, 0);
}
else {
media = new Book(title, author, price, 0);
}
Catalog = Catalog.add(media);
}
}
}
}
here is my error message. BookStoreGUI.java:210: error: cannot find symbol Catalog = Catalog.add(media); ^ symbol: method add(BookStoreItem) location: variable Catalog of type BookStoreItem 1 error
and her is the catalog class from wich the add method comes from.
public class Catalog {
private BookStoreItem[] inventory;
private final int CAPACITY = 100;
private int count;
public Catalog() {
inventory = new BookStoreItem[CAPACITY];
count = 0;
}
public void add(BookStoreItem newItem) {
inventory[count] = newItem;
count++;
}
public boolean isAvailable(String title) {
boolean found = false;
for (int i = 0;i < count && !found;i++) {
if (title.equals(inventory[i].getTitle())) {
found = true;
}
}
return found;
}
public BookStoreItem getItem(String title) {
BookStoreItem desiredItem = null;
boolean found = false;
for (int i = 0;i < count && !found;i++) {
if (title.equals(inventory[i].getTitle())) {
desiredItem = inventory[i];
found = true;
}
}
return desiredItem;
}
public BookStoreItem[] getList() {
return inventory;
}
}
Firstly, you would have fewer problems if you followed Java naming conventions - currently Catalog
is both an array variable name and a type name.
Secondly, there is no add
method in arrays. You may want a List
, e.g. ArrayList
. Alternatively, you may well just need an instance of Catalog
, e.g.
// This replaces your private static BookStoreItem[] Catalog; declaration
Catalog catalog = new Catalog();
Then you can use this in your handle
method:
catalog.add(media);
Note that I've made it an instance variable rather than a static variable - I doubt that you want any static variables, to be honest.
See more on this question at Stackoverflow