0

I'm making a basic book store in Javafx. My issue is when I select a book title, and then go into the menu to click add to cart, whatever item I already had in cart gets replaced. I'm a little stuck on how to fix it.

Another issue I'm having is to add my Book class - basically it's supposed add Book objects instead of String objects and anytime I try to do that I get an error because my book class requires String and Double. I thought maybe after I split up the string then I would say the first half is a string and the second is the double but it doesn't really work. I attempted it down below and it does work but it looks weird so if you could just check it out and let me know if its right that would be helpful! Any help would be appreciated. Here is the layout for the book text file Book Title, price

//all imports here

public class GUI extends Application{

     public static void main(String[] args)
       {
          launch(args);
       }
       
       public void start(Stage primaryStage)
       {
           final double WIDTH = 820.0, HEIGHT = 400.0;
           ArrayList<String> list = new ArrayList<>();
           
           BorderPane borderPane = new BorderPane();

           MenuBar menuBar = new MenuBar();
           Menu fileMenu = new Menu("File");
           Menu shoppingMenu = new Menu("Shopping");

           MenuItem loadBooks = new MenuItem("Load Books");
           MenuItem exitItem = new MenuItem("Exit");
           fileMenu.getItems().addAll(loadBooks,exitItem);

           MenuItem addSelected = new MenuItem("Add Selected Book");
           MenuItem removeSelected = new MenuItem("Remove Selected Book");
           MenuItem clearCart = new MenuItem("Clear Cart");
           MenuItem checkOut = new MenuItem("Check Out");
           shoppingMenu.getItems().addAll(addSelected, removeSelected, clearCart, checkOut);
           
           menuBar.getMenus().addAll(fileMenu, shoppingMenu);

           Label welcomeText = new Label("Welcome to the PFW Online Book Store!");
           Label availableBooks = new Label("Avaliable Books");
           Label shoopingCart = new Label("Shopping Cart");

           VBox vbox = new VBox(10, welcomeText);
           vbox.setAlignment(Pos.TOP_CENTER);

           ListView<String> listView = new ListView<>();
           listView.setPrefSize(400, 300);
           ListView<String> listView2 = new ListView<>();
           listView2.setPrefSize(400, 300);

           GridPane grid = new GridPane();
           grid.add(availableBooks, 0, 0);
           grid.add(listView, 0, 1);
           grid.add(shoopingCart, 1, 0);
           grid.add(listView2, 1, 1);
           grid.setHgap(20);

           borderPane.setTop(menuBar);
           borderPane.setCenter(vbox);
           borderPane.setBottom(grid);

           //event handlers
           exitItem.setOnAction(event ->
           {
               primaryStage.close();
           });
           
           loadBooks.setOnAction(event2 ->{
               FileChooser fileC = new FileChooser();
               fileC.setTitle("Open");
               File projectD = new File(System.getProperty("user.dir"));
               if(projectD.isDirectory()) {
                   fileC.setInitialDirectory(projectD);
               }
               
             File selectedFile = fileC.showOpenDialog(primaryStage);
             
             
              try {
                Scanner scan = new Scanner(selectedFile);
                
                while(scan.hasNext()) {
                    //puts the line into a string, then splits the string into an array
                    //then puts the first half of the array into an array list and then puts 
                    //into book object and then adds it to list
                    String bookT = scan.nextLine();
                    String book[] = bookT.split(",");
                    
                    String title = book[0];
                    String priceS = book[1];
                    Double priceD = Double.parseDouble(priceS);
                    
                    Book b = new Book(title,priceD);
                
                    list.add(b.toString());
                }
                
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
             
             listView.getItems().addAll(list);
               
           });
           
           //add selected book **This is where I'm having issues**
           addSelected.setOnAction(event2 ->{
               
               ObservableList<String> selections = listView.getSelectionModel().getSelectedItems();

               //I think I need to use loop to allow more than one item to be added but not sure

               listView2.getItems().setAll(selections);


           });
           
          
           Scene scene = new Scene(borderPane, WIDTH, HEIGHT);
           primaryStage.setTitle("Book Store Shopping Cart");
           primaryStage.setScene(scene);
           primaryStage.show();
       }
    
}

public class Book {

    /*
     * public String title;
• public double price;
• a constructor that accepts and assigns the title and price
• a toString() method that returns the title
     */
    public String title;
    public double price;
    
    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }
    
    public String toString() {
        return title;
    }
    
}

Amber
  • 27
  • 7

1 Answers1

2

On your first question:

As far as I can tell, you are calling setAll() on ObervableList:

listView2.getItems().setAll(selections);

This just replaces all items on the list with the ones in the argument. You need to use addAll() to append them to the list instead:

listView2.getItems().addAll(selections);

On your second question:

You have to write double without a capital letter to use the primitive type instead of the Wrapper Class Double (more on this here). So the line initializing the double value has to be

double priceD = Double.parseDouble(priceS);
mp1404
  • 21
  • 2