根据ChoiceBox选择添加价格

时间:2016-11-09 07:10:49

标签: java javafx fxml scenebuilder

我正在使用javaFX和Scenebuilder制作汽车护理中心GUI,直到遇到一个问题我才开始做好。

问题很简单。基本上,我做了这个一个选择框,用户可以选择更换普通轮胎(225美元)或运动轮胎(310美元)。 我必须将成本加到总和上(变量名= costSum)。  如果用户首先在选择框中选择常规轮胎,costSum仅添加225美元。

然后,如果用户选择运动轮胎,它应该只增加310美元(普通轮胎不是225美元),但是costSum增加了225和310,因为在选择运动轮胎之前已将{225}添加到costSum

我知道为什么会发生这种情况,但即使看起来很简单,我也找不到解决方法。

在用户从choicbox中选择之前,有没有办法将costSum的值重置为原始值?

为了简化我的问题,当用户选择每个不同的选项(普通轮胎,运动轮胎)时,如何只为costSum添加一个成本值?

以下是该选择框的代码:

@FXML
void onSelectTireReplacementChoice(ActionEvent event) {

    if(tireReplacementChkBox.isSelected()){
        tireReplacementChoiceBox.setDisable(false);     
        tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

            public void changed(ObservableValue ov, Number value, Number selection) {

                if(selection.intValue() == 0){
                    costSum += REGULAR_TIRE;                 
                }

                if(selection.intValue() == 1){
                    costSum += SPORTS_TIRE;                    
                 }
         ;
                String cost = "Service Cost: " + "$" + df.format(costSum);
                serviceCostLabel.setText(cost);   
            }
        });
    } else{
      //  costSum += initialCost;
        String cost = "Service Cost: " + "$" + df.format(costSum);
        serviceCostLabel.setText(cost);
        tireReplacementChoiceBox.setDisable(true);     
    }       

}

这是完整的代码(虽然我还没完成):

public class FXMLDocumentController implements Initializable {
    public double costSum = 0;
    public double costOnCustomerType;
    public boolean update;
    public int choice;
    public double sum = 0;

    String cost = "Service Cost: " + "$";

    final double BRAKES = 27.27;
    final double FLUID_CHK = 9.09;
    final double CAR_WASH = 4.54;
    final double EMMISION_INSPECTION = 36.37;
    final double TIRE_ROTATION = 18.18;
    final double REGULAR_TIRE = 225.22;
    final double SPORTS_TIRE = 315.32;
    final double REGULAR_OIL = 13.64;
    final double SYNTHETIC_OIL = 24.54;

    public double initialCost ;

    DecimalFormat df = new DecimalFormat("#.00");

    @FXML
    private Label label;

    @FXML
    private RadioButton newCustomerRadioButton;

    @FXML
    private ToggleGroup customerType;

    @FXML
    private RadioButton regularCustomerRadioButton;

    @FXML
    private TextField nameTextField;

    @FXML
    private TextField phoneTextField;

    @FXML
    private TextField addressTextField;

    @FXML
    private TextField emailTextField;

    @FXML
    private Button printInvoiceButtion;

    @FXML
    private Label serviceCostLabel;

    @FXML
    private Button resetButton;

    @FXML
    private CheckBox brakesChkBox;

    @FXML
    private CheckBox tireRotationChkBox;

    @FXML
    private CheckBox fluidChkBox;

    @FXML
    private CheckBox carWashChkBox;

    @FXML
    private CheckBox inspectionChkBox;

    @FXML
    private CheckBox tireReplacementChkBox;

    @FXML
    private CheckBox oilChangeChkBox;

    @FXML
    private ChoiceBox tireReplacementChoiceBox;

     @FXML
    void updateBrakes(ActionEvent event) {

        if(brakesChkBox.isSelected()){
            costSum += BRAKES;
        }else{
            costSum -= BRAKES;
        }

        String cost = "Service Cost: " + "$" + df.format(costSum);
        serviceCostLabel.setText(cost);
    }

     @FXML
    void updateCarWash(ActionEvent event) {

    }

    @FXML
    void updateEmmissonInspec(ActionEvent event) {

    }

    @FXML
    void updateFluidCheck(ActionEvent event) {

    }

    @FXML
    void updateTireRotation(ActionEvent event) {
        if(tireRotationChkBox.isSelected()){  
            costSum += TIRE_ROTATION; 
        } else{
            costSum -= TIRE_ROTATION;    
        }

        serviceCostLabel.setText(cost + df.format(costSum));


    }


    @FXML
    void onChangeServiceCostRequest(ActionEvent event) {

    }

    @FXML
    void onSelectOilChange(ActionEvent event) {

    }

    @FXML
    void onSelectTireReplacementChoice(ActionEvent event) {

        if(tireReplacementChkBox.isSelected()){
         tireReplacementChoiceBox.setDisable(false);     
         tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

                public void changed(ObservableValue ov, Number value, Number selection) {

                    if(selection.intValue() == 0){
                        costSum += REGULAR_TIRE;                 
                    }

                    if(selection.intValue() == 1){
                        costSum += SPORTS_TIRE;                    
                     }
             ;
                    String cost = "Service Cost: " + "$" + df.format(costSum);
                    serviceCostLabel.setText(cost);   
                }
            });
        } else{
          //  costSum += initialCost;
               String cost = "Service Cost: " + "$" + df.format(costSum);
               serviceCostLabel.setText(cost);
         tireReplacementChoiceBox.setDisable(true);     
        }       

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
       tireReplacementChoiceBox.setItems(FXCollections.observableArrayList("Regular Tire", "Sports TIre"));
       tireReplacementChoiceBox.setDisable(true);


        String cost = "Service Cost: " + "$" + df.format(costSum);
        serviceCostLabel.setText(cost);// TODO

    }    

}

1 个答案:

答案 0 :(得分:0)

试试这个:

@FXML
void onSelectTireReplacementChoice(ActionEvent event) {
    double newValue = 0;

    if(tireReplacementChkBox.isSelected()){
        tireReplacementChoiceBox.setDisable(false);              

        tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

            public void changed(ObservableValue ov, Number value, Number selection) {

                if(selection.intValue() == 0){
                    newValue = REGULAR_TIRE;                 
                }

                if(selection.intValue() == 1){
                    newValue = SPORTS_TIRE;                    
                 }
         ;
                String cost = "Service Cost: " + "$" + df.format(newValue);
                serviceCostLabel.setText(cost);   
            }
        });
    } else{
        costSum = initialCost + newValue;
        String cost = "Service Cost: " + "$" + df.format(costSum);
        serviceCostLabel.setText(cost);
        tireReplacementChoiceBox.setDisable(true);     
    }       

}