2016-11-15 71 views
0

我试图制作一个GUI,当您单击两个单选按钮中的一个时,根据您是新客户还是返回客户,它将应用不同的折扣。折扣适用于您可以选择的每个不同选项。我的问题是,每次点击复选框添加选项时,它都会复制折扣。第一次是正确的,但每次点击后,折扣越来越高。这里是我的复选框代码:当我切换JCheckBox时,GUI保持复合折扣

@FXML 
    void tires(ActionEvent event) { 
     if(isNewCustomer==true){ 
      tireRotation=tireRotation*(1.0-newCustomerDiscount); 
      //costLabel.setText("$ " +DF.format(cost)); 
     }else{ 
      tireRotation=tireRotation*(1.0-regularCustomerDiscount); 
      // costLabel.setText("$ " +DF.format(cost)); 
     } 

     if(tireBox.isSelected()){ 
      cost+=tireRotation; 
      costLabel.setText("$ " +DF.format(cost)); 

     } else { 
      cost =cost-tireRotation; 
      costLabel.setText("$ " +DF.format(cost)); 
     } 

    } 

这里是我的单选按钮代码:

@FXML 
void newCustomer(ActionEvent event) { 
    if (newCustomer.isSelected()){ 
     isNewCustomer=true; 
    } 
} 

任何帮助将不胜感激!

+1

首先,你可以删除'isNewCustomer ==真',只是用'isNewCustomer',因为它是一个'boolean'。其次,如果没有选择新客户,则应设置“isNewCustomer = false”,但我没有看到。第三,你正在压倒'轮胎转动',这是我假设的一个基数。我认为你应该使用一个新变量,'tireRotationCost = tireRotation *(1.0-new/regularCustomerDiscount);'。这样,它不会复合,而是每次都会覆盖成本。 –

+0

可能可以在一个处理两个切换的方法中完成。 – Sedrick

回答

0

我刚刚重读你的问题。创建全局变量。在您的切换框方法中设置finalCost全局变量。 设置变量,如果示例内:

全局变量:

double finalCost = 0; 
double initialCharge = 200; 
double discount= 50; 
double newAccountDiscount= 25; 

第一种方法:

if(tireBox.isSelected() && newCustomer.isSelected()) 
{ 
    finalCost = initialCharge - discount - newAccountDiscount; 
} 
else if(tireBox.isSelected()) 
{ 
    finalCost = initialCharge - discount; 
} 
else if(newCustomer.isSelected()) 
{ 
    finalCost = initialCharge - newAccountDiscount; 
} 
else 
{ 
    finalCost = initalCharge; 
} 

小号的Econd方法:

if(tireBox.isSelected() && newCustomer.isSelected()) 
{ 
    finalCost = initialCharge - discount - newAccountDiscount; 
} 
else if(tireBox.isSelected()) 
{ 
    finalCost = initialCharge - discount; 
} 
else if(newCustomer.isSelected()) 
{ 
    finalCost = initialCharge - newAccountDiscount; 
} 
else 
{ 
    finalCost = initalCharge; 
}