繁体   English   中英

为什么我不断收到找不到符号的错误

[英]Why do i keep getting a cant find symbol error

我正在尝试将书籍,cd或dvd的三个项目之一存储到数组中,但是我的add方法不起作用。

这是GUI类。

/**
    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);




           }

       }
   }
}

这是我的错误信息。 BookStoreGUI.java:210:错误:找不到符号Catalog = Catalog.add(media); ^符号:方法add(BookStoreItem)位置:变量BookStoreItem类型的目录1错误

她是add方法来自的目录类。

  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;
    }

}

首先,如果遵循Java命名约定,则问题会更少-当前Catalog既是数组变量名又是类型名。

其次, 没有 add的阵列方法。 您可能需要一个List ,例如ArrayList 另外,您可能只需要Catalog的实例,例如

// This replaces your private static BookStoreItem[] Catalog; declaration
Catalog catalog = new Catalog();

然后可以在handle方法中使用它:

catalog.add(media);

请注意,我将其设置为实例变量而不是静态变量-老实说,我怀疑您是否需要任何静态变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM