I am writing a code that allows a user to enter a discount code and that code will be applied to the price which will take away 10% subtotal.
What I am trying to do is to not have the discount code and the amount appear on the GUI unless a discount code has been applied (there is a window that will pop up and once they finish the can close and the information is there).
What has worked so far is the discount amount has worked. So it is not apparent when opening the program and when the discount code matches and is applied then the reduction amount is there.
However here is where I am getting trouble. I am trying to do the same thing with the discount code. So for example if you enter "SALE10" as the code then the GUI will display the code that has been entered and valid as well as the amount that has been taken off the subtotal.
I am trying to do that by using the setVisible booleans and visiblility tags on the FXML.
discountCode.setVisible(true);
discountAmount.setVisible(true);
I have those within the function but discountCode.setVisible(true); has a red line underneath and the error thats given is Cannot resolve method 'setVisible' in 'String' which I am unsure on what that means. I am more confused on the fact that the discountAmount works but discountCode doesn't
Here is the full function of it
public void applyDiscount(String discountCode){
if("SALE10".equals(discountCode)){
BigDecimal preDiscount = new BigDecimal(subtotalSum.getText().substring(1));
BigDecimal discountAmountValue = preDiscount.multiply(new BigDecimal("0.10"));
BigDecimal newSubtotal = preDiscount.subtract(discountAmountValue);
subtotalSum.setText("$" + newSubtotal.setScale(2, RoundingMode.HALF_UP));
BigDecimal taxRate = new BigDecimal("0.12");
BigDecimal newTotalPrice = newSubtotal.multiply(BigDecimal.ONE.add(taxRate));
totalPrice.setText("$" + newTotalPrice.setScale(2, RoundingMode.HALF_UP));
discountAmount.setText("-$" + discountAmountValue.setScale(2, RoundingMode.HALF_UP));
discountCode.setVisible(true);
discountAmount.setVisible(true);
} else {
discountCode.setVisible(false);
discountAmount.setVisible(false);
//will keep the text hidden if code is invalid
}
}
And here is the section of the FXML that has those Text
<Text fx:id="discountCode" layoutX="110.0" layoutY="223.0" strokeType="OUTSIDE" strokeWidth="0.0" text="-$9.99" wrappingWidth="58.5" visible="false"/>
<Text fx:id="discountAmount" layoutX="110.0" layoutY="223.0" strokeType="OUTSIDE" strokeWidth="0.0" text="-$9.99" wrappingWidth="58.5" visible="false"/>

Your class has probably fields
discountAmountanddiscountCode.In your method you have a parameter that is also named
discountCodeand that parameter shadows the field (i.e. you can no longer access the field only be the namediscountCode).You could either rename the parameter or access the field by prepending its name with
this.: