I am having trouble getting my radio buttons to be acknowledged by my method for calculating the cost
I am having trouble getting my radio buttons to be acknowledged by my method for calculating the cost. My main
method just runs the GUI so I wont include that. I have here my Floor Class to get all the information about a customers order on the floor, and my GUI class. I how do I complete the radio buttons so they are factored into the cost? I was told initially the code is incomplete. I realize this. That is why I am asking. Here is my new code. First is the Floor Class, then my GUI class. How do I get my radio buttons to factor into my cost calculation (getCalculateCost method)?
Floor Class:
package flooringmain;
import java.text.NumberFormat;
import java.util.Locale;
/**
*
* @author matt
*/
public class Floor {
//set cost to look like currency
private static final NumberFormat CF = NumberFormat.getCurrencyInstance(Locale.US);
public enum CarpetOrWood{
//establish if flooring is carpet or wood
Carpet, Wood
}//end enum carpet or wood
//instance variables
private String customerName, floorOrder;
private double length, width, area;
public static final double CARPET_PRICE = 10;
public static final double WOOD_PRICE = 20;
//variables for methods
private CarpetOrWood carpetOrWood;
private double floorTypeCost;
private double cost = 0;
//class methods
//set and return customer name
public void setName(String cName){
//store cName into customerName instance variable
customerName = cName;
}//end setName
public String getName(){
return customerName;
}//end getName
//set flooring type (Carpet or Wood)
public void setCarpetOrWood(CarpetOrWood floorType){
carpetOrWood = floorType;
}//end getCarpetOrWood
//get and return flooring type string for order
public String getCarpetOrWood(){
String str;
switch(carpetOrWood){
//shows as string for order summary
case Carpet:
str = “Carpet”;
break;
case Wood:
str = “Wood”;
break;
default:
str = “Carpet”;
}//end switch
return str;
}//end getCarpetOrWood method
//set and return the length
public void setLength(double fLength){
//store fLength into length instance variable
length = fLength;
}//end setLength
public double getLength(){
return length;
}//end getLength
//set and return the width
public void setWidth(double fWidth){
//store fWidth into width instance variable
width = fWidth;
}//end setWidth
public double getWidth(){
return width;
}//end getWidth
//calculate area and return area
public double getArea(){
area = length*width;
return area;
}//end getArea method
//calculate and return cost
private void calculateCost(){
cost = 0; //set to zero at first
switch(carpetOrWood){
case Carpet:
floorTypeCost = CARPET_PRICE;
break;
case Wood:
floorTypeCost = WOOD_PRICE;
break;
default:
floorTypeCost = CARPET_PRICE;
}//end switch
cost = floorTypeCost*area;
}//end cost calculation
//return cost as string as currency
public String getCost(){
calculateCost();
return CF.format(cost);
}//end getCost
public String getOrderSummary(){
//add order details into a new string
StringBuilder str = new StringBuilder();
//for name
str.append(“nCustomer Name: “);
str.append(getName());
//for carpet or wood
str.append(“nCarpet or Wood: “);
str.append(getCarpetOrWood());
//for dimensions
str.append(“nLength: “);
str.append(getLength());
str.append(“nWidth: “);
str.append(getWidth());
str.append(“nTotal Area: “);
str.append(getArea());
//totals
str.append(“nFloor Type Cost: “);
str.append(CF.format(floorTypeCost));
str.append(“nOrder Total: “);
str.append(getCost());
return str.toString();
}//end getOrderSummary
@Override
public String toString(){
return toString();
}//end toString
}// end class Floor
FloorGUI class:
public class FlooringGUI extends JFrame {
//GUI components
private JTextField txtName, txtLength, txtWidth, txtArea, txtCost;
private JTextArea txtOrderArea;
private JButton btnSubmit, btnClear;
private JRadioButton radCarpet, radWood;
//class
private Floor floor;
//constructor builds GUI
public FlooringGUI(){
//call superclass constructor to add the title
super(“Enter Customer Information”);
//instantiate components
//add fields
txtName = new JTextField(50);
txtLength = new JTextField(10);
txtWidth = new JTextField(10);
txtArea = new JTextField(10);
txtCost = new JTextField(10);
//add buttons
btnSubmit = new JButton(“Submit Order”);
btnClear = new JButton(“Clear Fields”);
//add text area
txtOrderArea = new JTextArea();
//add radio buttons
radCarpet = new JRadioButton(“Carpeting”);
radWood = new JRadioButton(“Wood Flooring”);
//create button group
ButtonGroup b = new ButtonGroup();
b.add(radCarpet);
b.add(radWood);
//panel for radio buttons
JPanel radioPanel = new JPanel();
//layout for panel and buttons
radioPanel.setLayout(new FlowLayout());
radioPanel.add(radCarpet);
radioPanel.add(radWood);
//instantiate class
floor = new Floor();
//set layout
setLayout(new GridLayout(0,2));
//add components to frame
add(new JLabel(“Customer Name: “));
add(txtName);
add(new JLabel(“Floor Length: “));
add(txtLength);
add(new JLabel(“Floor Width: “));
add(txtWidth);
add(new JLabel(“”));
add(radioPanel);
add(new JLabel(“”));
add(btnSubmit);
add(new JLabel(“”));
add(btnClear);
add(new JLabel(“Total Area: “));
add(txtArea);
add(new JLabel(“Total Cost: “));
add(txtCost);
add(new JLabel(“Order Summary: “));
add(txtOrderArea);
//set up event Handlers
ButtonHandler handler = new ButtonHandler();
//connect buttons to actionlistener
btnSubmit.addActionListener(handler);
btnClear.addActionListener(handler);
}//end FlooringGUI constructor
private void setCarpetOrWood(Floor.CarpetOrWood carpetOrWood) {
floor.setCarpetOrWood(carpetOrWood);
setCalculateCost();
}
private void setCalculateCost() {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
//implement actionlistener
private class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
//code to process button events
if(e.getSource()==btnSubmit){
//verfies if all input is entered and valid
if(txtName.getText().isEmpty()||
txtLength.getText().isEmpty()||
txtWidth.getText().isEmpty()){
//show prompt to enter info in appropriate fields
JOptionPane.showMessageDialog(null, “Please Make Sure Name, Length, and Width Fields Are Filled”);
return;
}//end if verify fields full
//make sure length and width is numeric
floor.setName(txtName.getText());
try{
floor.setLength(Double.parseDouble(txtLength.getText()));
floor.setWidth(Double.parseDouble(txtWidth.getText()));
}//end try
catch(NumberFormatException ex){
//prompt user to enter numeric information
JOptionPane.showMessageDialog(null, “Make Sure Length and Width Are Numeric Values”);
}//end catch
//display order info
txtArea.setText(String.valueOf(floor.getArea()));
txtCost.setText(String.valueOf(floor.getCost()));
txtOrderArea.setText(String.valueOf(floor.getOrderSummary()));
}//end if button processing
else if(e.getSource()==btnClear){
//clear all fields by setting to “”
txtName.setText(“”);
txtLength.setText(“”);
txtWidth.setText(“”);
txtArea.setText(“”);
txtCost.setText(“”);
txtOrderArea.setText(“”);
}//end else if
}//end actionPerformed
}//end class ButtonHandler
private void radCarpetItemStateChange(java.awt.event.ItemEvent evt) {
if(radCarpet.isSelected()){
setCarpetOrWood(Floor.CarpetOrWood.Carpet);
}//end if
}
private void radDoubleItemStateChanged(java.awt.event.ItemEvent evt) {
if(radWood.isSelected()){
setCarpetOrWood(Floor.CarpetOrWood.Wood);
}//end if
}
}//end class FlooringGUI