gpt4 book ai didi

java - 为什么我总是收到找不到符号错误

转载 作者:行者123 更新时间:2023-12-02 04:16:00 24 4
gpt4 key购买 nike

我正在尝试将书籍、CD 或 DVD 三个项目之一存储到数组中,但我的添加方法不起作用。

这是 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.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,例如数组列表。或者,您可能只需要一个 Catalog 实例,例如

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

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

catalog.add(media);

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

关于java - 为什么我总是收到找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com