将String变量添加到链接列表?

时间:2021-02-11 07:21:16

I am making a program that returns the output in a long string variable. The data in the string is constantly changing based on what the user enters in the GUI. My question is, how do I take this and store it inside of my linked list? I have looked at a few examples, but the class I was provided with for my class is a little different, and I haven't been able to find something to specifically fix my problem.

我正在创建一个程序,在长字符串变量中返回输出。字符串中的数据会根据用户在GUI中输入的内容而不断变化。我的问题是,如何将其存储在链表中?我已经看了几个例子,但是我为我班级提供的课程有点不同,我找不到能够专门解决问题的方法。

Controller Code:

public class RentGameDialogController extends RentalStoreGUIController implements Initializable{

/** TextField Objects **/
@FXML private TextField nameField, rentedOnField, dueBackField;

/** String for NameField **/
String name, rentedOn, dueBack;

/** Game ComboBox ID's **/
@FXML private ObservableList<GameType> cbGameOptions;
@FXML private ComboBox<GameType> cbGame;

/** Console ComboBox ID's **/
@FXML private ObservableList<PlayerType> cbConsoleOptions;
@FXML private ComboBox<PlayerType> cbConsole;

/** GameType object **/
private GameType game;

/** PlayerType Object **/
private PlayerType console;

/** Button ID's **/
@FXML Button cancel, addToCart;

/** Counter for calculating total **/
int gameCounter;

/** Stage for closing GUI **/
private Stage currentStage;

private MyLinkedList list = new MyLinkedList();




@Override
public void initialize(URL location, ResourceBundle resources) {

    /** Select Console **/
    cbConsoleOptions = FXCollections.observableArrayList();
    for (PlayerType p : PlayerType.values()) { cbConsoleOptions.addAll(p); }
    cbConsole.getItems().addAll(cbConsoleOptions);

    /** Select Game **/
    cbGameOptions = FXCollections.observableArrayList();
    for (GameType g : GameType.values()){ cbGameOptions.addAll(g); }
    cbGame.getItems().addAll(cbGameOptions);

}

public String getName(){
    name = nameField.getText();

    try {

        String[] firstLast = name.split(" ");
        String firstName = firstLast[0];
        String lastName = firstLast[1];

    } catch (Exception e){
        e.printStackTrace();
    }

    return name;
}

public String getGame() {
    return cbGame.getSelectionModel().getSelectedItem().toString();
}

public String getConsole() {
    return cbConsole.getSelectionModel().getSelectedItem().toString();
}

public String getRentedOn() throws ParseException {

    rentedOn = rentedOnField.getText();


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Date rentedOnDate = format.parse(rentedOn);

    Calendar cal = Calendar.getInstance();
    cal.setLenient(false);
    cal.setTime(rentedOnDate);

    try {

        cal.getTime();

    } catch (Exception e) {
        rentedOnField.setText("ERROR");
    }

    return rentedOn;

}

public String getDueBack() throws ParseException {

    dueBack = dueBackField.getText();


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Date dueBackDate = format.parse(dueBack);

    Calendar cal = Calendar.getInstance();
    cal.setLenient(false);
    cal.setTime(dueBackDate);

    try {

        cal.getTime();

    } catch (Exception e) {
        dueBackField.setText("ERROR");
    }

    return dueBack;

}

/*************************************
 * This is the method to call the other
 * String methods so their output can be
 * put into my main GUI
 *
 *
 * @return
 * @throws ParseException
 *************************************/

public String storePurchaseData() throws ParseException {
    gameCounter++;
    String toList = getName() + " | " + getGame() + " | " + getConsole() + " | " +
            getRentedOn() + " | " + getDueBack();

    //Add 'toList' to the linked list here if possible

    return toList;
}


@FXML
public void handleCancelButtonAction () {
    currentStage = (Stage) cancel.getScene().getWindow();
    currentStage.close();
}

@FXML
public void addToCartButton () throws ParseException {
    appendTextArea(storePurchaseData());
    currentStage = (Stage) cancel.getScene().getWindow();
    currentStage.close();
}}

This code is for my controller. It launches a basic GUI, then I can pull the data from all of the fields I made, convert them to Strings and can then print them in one long chain of text. I would like to store the string into my linked list class.

此代码适用于我的控制器。它启动一个基本的GUI,然后我可以从我制作的所有字段中提取数据,将它们转换为字符串,然后可以在一个长文本链中打印它们。我想将字符串存储到我的链表类中。

Linked List code:

链接列表代码:

public class MyLinkedList<E> implements Serializable {

private DNode<E> top;
public int size;

public MyLinkedList() {
    top = null;
    size = 0;
}

}

I am very new to linked lists and I am trying to understand them, does this code make sense? Do I need to add anything to, say, save the String that I am storing into a text file?

我对链接列表很新,我试图理解它们,这段代码有意义吗?我是否需要添加任何内容,比如将我存储的字符串保存到文本文件中?

Thank you in advance

先感谢您

1 个解决方案

#1


1  

Without getting into your game code at all, it looks like your MyLinkedList class takes a type parameter E - You haven't shown the code for DNode but it also takes the E type. If you can specify this to be a String then the nodes of MyLinkedList can be populated with Strings as you desire.

根本没有进入你的游戏代码,看起来你的MyLinkedList类采用了类型参数E - 你没有显示DNode的代码,但它也采用了E类型。如果您可以将其指定为String,则可以根据需要使用字符串填充MyLinkedList的节点。

DNode<String> myFirstNode = new DNode<>(null, null, "nodeData");
MyLinkedList<String> list = new MyLinkedList<>(myFirstNode);

This assumes that the MyLinkedList class also has a constructor that takes a DNode to initialize its head, and that DNode looks something like this.

这假设MyLinkedList类还有一个构造函数,它使用一个DNode来初始化它的头部,并且该DNode看起来像这样。

#1


1  

Without getting into your game code at all, it looks like your MyLinkedList class takes a type parameter E - You haven't shown the code for DNode but it also takes the E type. If you can specify this to be a String then the nodes of MyLinkedList can be populated with Strings as you desire.

根本没有进入你的游戏代码,看起来你的MyLinkedList类采用了类型参数E - 你没有显示DNode的代码,但它也采用了E类型。如果您可以将其指定为String,则可以根据需要使用字符串填充MyLinkedList的节点。

DNode<String> myFirstNode = new DNode<>(null, null, "nodeData");
MyLinkedList<String> list = new MyLinkedList<>(myFirstNode);

This assumes that the MyLinkedList class also has a constructor that takes a DNode to initialize its head, and that DNode looks something like this.

这假设MyLinkedList类还有一个构造函数,它使用一个DNode来初始化它的头部,并且该DNode看起来像这样。