Precio de la caja de salida incorrecto - Salida = a codificación lo facilita

PROJ3CT2501:

Cuando la prueba en el botón Agregar para ver si se suman los valores de la salida pasa como Precio (jerga) Label@7a6d7fd9[styleClass=label]"

y sé que tiene algo que ver con el

Label price = new Label("Price" + " " + answer);

posiblemente podría ser la setText para responder a abajo en la parte inferior

answer.setText(answer.getText() + myFormatter.format(tax + totalCost));

código completo

package shoppingcart;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javax.swing.JList;
import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class ShoppingCart extends Application
{
    private Label answer;
    private Label price;
    private JList listItems;

    private String[] listArray = new String[7];
    private List cartItems = new List();
    //int cartItems = 0;

    private final double salesTax = 0.06;

    public static void main(String[] args) throws FileNotFoundException
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException
    {
        answer = new Label("");
        price = new Label("");
        String line;
        int index = 0;

        File file = new File("BookPrices.txt");
        Scanner fileReader = new Scanner(file);

        while (fileReader.hasNext())
        {
            line = fileReader.nextLine();
            String[] titles = line.split(",");
            listArray[index] = titles[0];
            index++;
        }

        //list view items book
        ListView < String > listView = new ListView < > ();
        listView.setPrefSize(200, 170);
        listView.getItems().addAll(listArray);


        // create label to display the selection
        Label selectedNameLabel = new Label("Select a Book");
        Label price = new Label("Price" + " " + answer);

        //Button for selection
        Button addButton = new Button("Add to Cart");
        addButton.setOnAction(new AddButtonListener());

        //Delete button
        Button removeButton = new Button("Remove Item");
        removeButton.setOnAction(new RemoveButtonListener());


        //Controls to VBox
        VBox vbox = new VBox(10, listView, selectedNameLabel,
            price, addButton, removeButton);
        vbox.setPadding(new Insets(10));
        vbox.setAlignment(Pos.CENTER);

        //Show
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public class AddButtonListener implements EventHandler < ActionEvent >
    {
        @Override
        public void handle(ActionEvent e)
        {
            //String value;
            //value = (String) listItems.getSelectedValue();
            String value = listItems.getSelectedValue().toString();
            cartItems.add(value);
        }

    }
    public class RemoveButtonListener implements EventHandler < ActionEvent >
    {

        @Override
        public void handle(ActionEvent e)
        {
            // String value;
            //value = (String) listItems.getSelectedValue();
            String value = listItems.getSelectedValue().toString();
            cartItems.remove(value);
        }

    }


    public class ButtonClickHandler1 implements EventHandler < ActionEvent >
    {
        @Override
        public void handle(ActionEvent e)
        {

            String line;
            double totalCost = 0.0, costOfItem = 0.0;
            File file = new File("BookPrices.txt");
            Scanner fileReader = null;
            try
            {
                fileReader = new Scanner(file);
            }
            catch (FileNotFoundException el)
            {
                el.printStackTrace();
            }

            while (fileReader.hasNextLine())
            {
                line = fileReader.nextLine();
                String[] cost = line.split(",");

                String title = cost[0];
                costOfItem = Double.parseDouble(cost[1]);

                for (int i = 0; i < cartItems.getItemCount(); i++)
                {
                    if (title.equals(cartItems.getItem(i)))
                        totalCost += costOfItem;
                }
            }

            double tax = salesTax * totalCost;
            DecimalFormat myFormatter = new DecimalFormat("###.##");

            answer.setText(answer.getText() + myFormatter.format(tax + totalCost));

        }
    }

}
maio290:

El galimatías Label@7a6d7fd9[styleClass=label]es en realidad sólo el común toString()llamada de un objeto.

Es necesario añadir una .getText()aquí:

Label price = new Label("Price" + " " + **answer.getText()**);

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=282163&siteId=1
Recomendado
Clasificación