Reputation: 15
I am working on a program that calculates the cost of flooring based on the dimensions of a room and the type of flooring selected. I have 5 classes (Cost
, CustomerInfo
, OrderSummary
, MainForm
, and MainApp
), and the program itself has 3 tabs (Cost, Customer Information, Order Summary).
Each tab is instantiated as its respective object in the MainForm
class. The problem that I am having is my OrderSummary
class needs to invoke the getFloorArea()
and getTotalCost()
methods from the Cost
class, and also needs to call the getCustName()
and getCustAddress()
methods from the CustomerInfo
class. Each tab in itself works properly (calculating the area, cost of proposed room / getting the customer's name and address), but I can't figure out how to pull this information into the OrderSummary
class. The Order Summary tab just shows all of the information as null.
I'm sure that it's because I need to instantiate Cost
and CustomerInfo
classes in the OrderSummary
class, but I can't figure out how to do it. I feel like the problem is 3 different objects are created when I create my tabs, but I don't know how to access the information that is being entered into each tab from the OrderSummary
class. Any help is really appreciated I'm pulling my hair out trying to figure out what to do.
I can provide code as needed but it's a pretty long program.
EDIT: Here are some bits that I think will help:
This is in my MainForm, where I create my tabs:
jtp.addTab("Cost", new Cost());
jtp.addTab("Customer Info", new CustomerInfo());
jtp.addTab("Order Summary", new OrderSummary());
This is method getFloorArea() in class Cost
public double getFloorArea() {
FloorLength = Double.parseDouble(enterLength.getText());
FloorWidth = Double.parseDouble(enterWidth.getText());
FloorArea = FloorLength * FloorWidth;
return FloorArea;
}
And this is my class OrderSummary, in which I can't figure out how to call these functions in order to display the information:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import helpers.*;
@SuppressWarnings("serial")
public class OrderSummary extends JPanel {
JTextArea orderSummary;
public OrderSummary() {
createPanel();
}
private void createPanel() {
super.setLayout(new GridBagLayout());
GridBagConstraints bag = new GridBagConstraints();
bag.fill = GridBagConstraints.BOTH;
bag.anchor = GridBagConstraints.FIRST_LINE_START;
bag.insets = new Insets(5,5,5,5);
bag.gridx = 0;
bag.gridy = 0;
orderSummary = new JTextArea(5, 20);
orderSummary.setFont(new Font("Arial", Font.BOLD, 12));
orderSummary.setBackground(Color.WHITE);
this.add(orderSummary, bag);
//This is my trouble area, I can't figure out how to access the classes to display the information in this JTextArea
orderSummary.setText("Customer Name: " + CustomerInfo.getFirstName() + " " + CustomerInfo.getLastName() +
"\nAddress: " + CustomerInfo.getStreet() + "\n" + CustomerInfo.getCity() + "\n" + CustomerInfo.getCustState() + "\n" + CustomerInfo.getZip() +
"\n\nTotal Area: " + Cost.getFloorArea() + " square feet" +
"\nCost: " + OutputHelpers.formattedCurrency(Cost.getTotalCost()));
}
}
I've tried making the variables and methods static in the Cost class, but then the calculations and cost do not work within that tab itself. For example, my clear button will clear all variables except the static ones.
Upvotes: 0
Views: 1489
Reputation: 1164
You could pass the Cost
and CustomerInformation
objects to the OrderSummary
when you create them in the MainForm
class. Although you may want to think about remodelling your project.
public class MainForm {
public void myMethod() {
Cost cost = new Cost();
CustomerInfo custInfo = new CustomerInfo();
OrderSummary orderSummary = new OrderSummary(cost, custInfo);
jtp.addTab("Cost", cost);
jtp.addTab("Customer Info", custInfo);
jtp.addTab("Order Summary", orderSummary);
...
}
}
And something like...
public class OrderSummary {
private Cost cost;
private CustomerInformation custInfo;
public OrderSummary(Cost cost, CustomerInformation custInfo) {
this.cost = cost;
this.custInfo = custInfo;
}
...
}
Upvotes: 2