Home > front end >  I'm not sure how to get these values given in different classes to connect to each other in Jav
I'm not sure how to get these values given in different classes to connect to each other in Jav

Time:02-15

This is Javafx and I am making a GUI with multiple classes.

Code:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class AutoLoanCalculator extends Application{

    public static void main(String[] args) {
          launch(args);
       }
    
    
    public void start(Stage primaryStage) {
        
        
        PaymentSection section1 = new PaymentSection();
        LoanTermSection section2 = new LoanTermSection();
        FinancingSection section3 = new FinancingSection();
        OptionsSection section4 = new OptionsSection();
        
        Button calc = new Button("Calculate");
        Button reset = new Button("Reset");
        Button exit = new Button("Exit");
        
        GridPane comboGrid = new GridPane();
        comboGrid.add(section1, 0, 0);
        comboGrid.add(section2, 1, 0);
        comboGrid.add(section3, 0, 1);
        comboGrid.add(section4, 1, 1);
        comboGrid.add(calc, 0,2);
        comboGrid.add(reset, 1,2);
        comboGrid.add(exit, 2,2);
        
        HBox combo = new HBox();
        combo.getChildren().add(comboGrid);
        
        calc.setOnAction(event ->{
            
            Double basePrice = Double.parseDouble(section3.getPriceNum());
            Double downPayment = Double.parseDouble(section3.getDownPayNum());
            Double taxRate = Double.parseDouble(section3.getTaxNum()) / 100;
            Double optionPrice = section4.getCost();
            Double interestRate = section2.getRate() / 100;
            int loanTermMonths = section2.getMonth();
            
            //calculates the values of rate and total sales tax
            double rate = interestRate/12;
            double salesTaxAmt = (basePrice - downPayment   optionPrice) * taxRate;
            
            Double totalLoanAmt = basePrice - downPayment   optionPrice   salesTaxAmt;
            Double monthPayment = totalLoanAmt * (rate * Math.pow(1   rate, loanTermMonths)) / (Math.pow(1   rate, loanTermMonths) - 1);
            Double totalPayment = monthPayment * loanTermMonths   downPayment;
            
            //section1.setLoanNum(String.format("%.2f", totalLoanAmt));
            
            System.out.println(basePrice);
            System.out.println(downPayment);
            System.out.println(taxRate);

        });
        
        reset.setOnAction(event ->{
            section1.reset();
            section2.reset();
            section3.reset();
            section4.reset();
        });
        
        exit.setOnAction(event ->{
            primaryStage.close();
        });
        
        Scene scene = new Scene(combo);
        primaryStage.setTitle("Auto Loan Calculator");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

//I call this section 1

import javafx.scene.control.Label;

import javafx.scene.layout.GridPane;

public class PaymentSection extends GridPane{

    private Label payInfo;
    private Label loanAmount;
    private Label monthlyPayment;
    private Label totalPayment;
    private Label loanNum;
    private Label monthNum;
    private Label totalNum;
    private String loanInt;
    private String monthInt;
    private String totalInt;
    
    public PaymentSection () {
        payInfo = new Label("Payment Information");
        loanAmount = new Label("Total Loan Amount: $ ");
        monthlyPayment = new Label("Monthly Payment: $ ");
        totalPayment = new Label("Total Payment: $ ");
        
        loanNum = new Label();
        monthNum = new Label();
        totalNum = new Label();
        
        
        
        loanNum.setText("0.0");
        monthNum.setText("0.0");
        totalNum.setText("0.0");
    
        
        add(payInfo,0,0);
        add(loanAmount,0,1);
        add(monthlyPayment,0,2);
        add(totalPayment,0,3);
        add(loanNum,1,1);
        add(monthNum,1,2);
        add(totalNum,1,3);
        setStyle("-fx-padding: 10;"  
                "-fx-border-style: solid inside;"   
                "-fx-border-width: 2;"  
                "-fx-border-color: black;");
        }
    

    public String setLoanNum(String string) {
        return loanInt;
    }
    
    public void reset() {
        loanNum.setText("0.0");
        monthNum.setText("0.0");
        totalNum.setText("0.0");
    }
    
}

//I call this section 3

import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class FinancingSection extends GridPane{

    private Label finance;
    private Label price;
    private Label tax;
    private Label downPay;
    private TextField priceNum;
    private TextField taxNum;
    private TextField downPayNum;
    private String priceInt;
    private String downPayInt;
    private String taxInt;
    

    public FinancingSection() {
        
        LoanTermSection term = new LoanTermSection();
        term.getRate();
        
        finance = new Label("Fianancing Information");
        price = new Label("Base Price: $ ");
        downPay = new Label("Down Payment: $ ");
        tax = new Label("Sales Tax: % ");
        
        priceNum = new TextField("0.0");
        downPayNum = new TextField("0.0");
        taxNum = new TextField("7.0");
        
        priceInt = priceNum.getText();
        downPayInt = downPayNum.getText();
        taxInt = taxNum.getText();
        
        add(finance,0,0);
        add(price,0,1);
        add(downPay,0,2);
        add(tax,0,3);
        add(priceNum,1,1);
        add(downPayNum,1,2);
        add(taxNum,1,3);
        setStyle("-fx-padding: 10;"  
                "-fx-border-style: solid inside;"   
                "-fx-border-width: 2;"  
                "-fx-border-color: black;");
        
        
    }
    
    public String getPriceNum() {
        return priceInt;
    }
    public void setPriceNum(String newPriceNum) {
        priceInt = newPriceNum;
    }
    
    public String getDownPayNum() {
        return downPayInt;
    }
    public void setDownPayNum(String newDownPay) {
        downPayInt = newDownPay;
    }
    
    public String getTaxNum() {
        return taxInt;
    }
    public void setTaxNum(String newTax) {
        taxInt = newTax;
    }
    
    public void reset() {
        priceNum.setText("0.0");
        downPayNum.setText("0.0");
        taxNum.setText("7.0");
    }
    
}

The issue is that I can't get basePrice, downPayment and taxRate to be set - I can't get them from their values set in section 3 code: I thought to use setters and getters but it seems to not work and I'm not sure why or how to fix it.- maybe it has something to do with my values I'm setting them as - not sure really. As for section 1 I can't get it to print - which I'm assuming is the same issue as section 3. So mainly I just need help with section 3's values being fetched. Thanks in advance!

CodePudding user response:

You need to call getText() on the text fields at the time you retrieve the values. So, for example,

public class FinancingSection extends GridPane{

    // ...

    public String getPriceNum() {
        return priceNum.getText();
    }

    // ...
}

You can probably get rid of the priceInt variables, etc., and you probably should get rid of the set methods in FinancingSection which you're not using.

The set methods that you are using, in PaymentSection, should update their corresponding labels, e.g.

public class PaymentSection extends GridPane{

    // ...

    public void setLoanNum(String string) {
        loanNum.setText(string) ;
    }

    // ...

}
  • Related