Cannot resolve method 'setVisible' in 'String' (Java)

73 Views Asked by At

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"/>
2

There are 2 best solutions below

3
Thomas Kläger On

Your class has probably fields discountAmount and discountCode.

In your method you have a parameter that is also named discountCode and that parameter shadows the field (i.e. you can no longer access the field only be the name discountCode).

You could either rename the parameter or access the field by prepending its name with this.:

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));

        this.discountCode.setVisible(true); // change here
        discountAmount.setVisible(true);
    } else {
        this.discountCode.setVisible(false); // change here
        discountAmount.setVisible(false);
        //will keep the text hidden if code is invalid
    }
}
0
SedJ601 On

Here is an app that can hopefully help you. I would use a Map to keep up with valid discounts or a List if your valid discounts are POJOs. From there, I would use a Set to keep up with the valid discounts that were entered. You could use a ListView, VBox, or HBox to show which valid discounts were entered.

Valid Codes

  • BIGSAVINGS
  • LUCKYDAY
  • LILSOMETHING

Main

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        Map<String, Double> validDiscountCodes = new HashMap();
        validDiscountCodes.put("BIGSAVINGS", .50);
        validDiscountCodes.put("LUCKYDAY", .75);
        validDiscountCodes.put("LILSOMETHING", .10);
       
        Set<String> verifiedDiscountCodes = new HashSet();
        
        Label lblDiscountCode = new Label("Discount Code: ");
        TextField tfDiscountCode = new TextField();    
        Button btnDiscountCode = new Button("Enter");
        HBox hBox = new HBox(lblDiscountCode, tfDiscountCode, btnDiscountCode);
        HBox hbDisplayEnteredValidCodes = new HBox();
        hbDisplayEnteredValidCodes.setSpacing(5);
        btnDiscountCode.setOnAction((actionEvent) -> {
            if(validDiscountCodes.containsKey(tfDiscountCode.getText().toUpperCase()))
            {
                if(verifiedDiscountCodes.add(tfDiscountCode.getText().toUpperCase()))
                {
                    hbDisplayEnteredValidCodes.getChildren().add(new Label(tfDiscountCode.getText().toUpperCase()));

                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Discount Code");
                    alert.setHeaderText("Valid Code");
                    alert.setContentText(tfDiscountCode.getText() + " is a Valid Code");
                    alert.show();
                    PauseTransition delay = new PauseTransition(Duration.seconds(2));
                    delay.setOnFinished(e -> alert.close());
                    delay.play();
                    tfDiscountCode.setText("");
                }
                else
                {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Discount Code");
                    alert.setHeaderText("Code Already Applied");
                    alert.setContentText(tfDiscountCode.getText() + " has already been applied!");
                    alert.show();
                    PauseTransition delay = new PauseTransition(Duration.seconds(2));
                    delay.setOnFinished(e -> alert.close());
                    delay.play();
                    tfDiscountCode.setText("");
                }
            }
            else
            {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("Discount Code");
                alert.setHeaderText("Invalid Code");
                alert.setContentText(tfDiscountCode.getText() + " is not a Valid Code!");
                alert.show();
                PauseTransition delay = new PauseTransition(Duration.seconds(2));
                delay.setOnFinished(e -> alert.close());
                delay.play();
            }
        });
        
        TextArea taDiscountApplied = new TextArea();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Discount Codes Applied:").append(System.lineSeparator());
        Button btnCheckOut = new Button("Checkout");
        btnCheckOut.setOnAction((actionEvent) -> 
        {
            for(String verifiedDiscountCode : verifiedDiscountCodes)
            {
                stringBuilder.append("\t").append(verifiedDiscountCode).append(": ").append(validDiscountCodes.get(verifiedDiscountCode) * 100).append(" off!").append(System.lineSeparator());
            }   
            
            taDiscountApplied.setText(stringBuilder.toString());
            btnCheckOut.setDisable(true);
            btnDiscountCode.setDisable(true);
        });
        
  
        VBox root = new VBox(hBox, hbDisplayEnteredValidCodes, btnCheckOut, taDiscountApplied);
        var scene = new Scene(new StackPane(root), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

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

}

enter image description here