My program works fine as is, I just need to add an input validation "invalid input, number has to be positive" dialog, after an input of 0
or a negative number
for length: tLength
and width: tWidth
.
Here is my code which runs fine, just needs the validation added:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class FloorsRUs extends JFrame implements ActionListener, ItemListener
{
public JTabbedPane jTP;
public JRadioButton flrWood = null;
public JRadioButton flrCarpet = null;
public JTextField tCName;
public JTextField tAddress;
public JTextField tLength;
public JTextField tWidth;
public JButton calcArea;
public JButton calcCost;
public JButton subOrder;
public JButton orderSum;
public JButton ordList;
public JPanel flrPan;
public JPanel lwPan;
public JPanel cPan;
public JLabel lab1;
public JLabel lab2;
public JLabel lab3;
public JLabel lab4;
public String flrType;
public double area = 0;
public double cost = 0;
FloorsRUs(String title)
{
super(title);
jTP=new JTabbedPane();
flrPan=new JPanel();
lwPan=new JPanel();
cPan=new JPanel();
flrPan.add(flrWood=new JRadioButton("Choose Wood"));
flrPan.add(flrCarpet=new JRadioButton("Choose Carpet"));
ButtonGroup grp = new ButtonGroup();
grp.add(flrWood);
grp.add(flrCarpet);
lwPan.add(lab1=new JLabel("Enter the Length : "));
lwPan.add(tLength=new JTextField(10));
lwPan.add(lab2=new JLabel("Enter the Width"));
lwPan.add(tWidth=new JTextField(10));
lwPan.add(calcArea=new JButton("Calculate the Area"));
lwPan.add(calcCost=new JButton("Calculate the Cost"));
lwPan.add(subOrder=new JButton("Submit the Order"));
cPan.add(lab3=new JLabel("Enter the Customers Name : "));
cPan.add(tCName=new JTextField(10));
cPan.add(lab4=new JLabel("Enter the Customers Address : "));
cPan.add(tAddress=new JTextField(10));
cPan.add(orderSum=new JButton("The Order Summary"));
cPan.add(ordList=new JButton("The Order List"));
jTP.addTab("Select Floor Type ",flrPan);
jTP.addTab("Enter Length and Width ",lwPan);
jTP.addTab("Customer Info",cPan);
add(jTP);
calcArea.addActionListener(this);
calcCost.addActionListener(this);
subOrder.addActionListener(this);
orderSum.addActionListener(this);
ordList.addActionListener(this);
flrWood.addItemListener(this);
flrCarpet.addItemListener(this);
}
@Override
public void itemStateChanged(ItemEvent ie)
{
if(flrWood.isSelected()){
flrType="Wood";
}
else if(flrCarpet.isSelected()){
flrType="Carpet";
}
}
@Override
public void actionPerformed(ActionEvent ae)
{
String source=ae.getActionCommand();
switch (source) {
case "Calculate the Area":
System.out.println("Calc 1 Complete");
area=Double.parseDouble(tLength.getText())
*Double.parseDouble(tWidth.getText());
break;
case "Calculate the Cost":
System.out.println("Calc 2 Complete");
if(flrType.equals("Wood"))
cost = area *20;
else if(flrType.equals("Carpet"))
cost = area *10;
break;
case "The Order Summary":
JOptionPane.showMessageDialog(null,"The Order Summary - Area is :" +
""+area+""+ " The Cost is : "+cost," Information",
JOptionPane.INFORMATION_MESSAGE);
break;
case "The Order List":
JOptionPane.showMessageDialog(null,"The Order List : \n Customer's name :
"+tCName.getText()
+"\nAddress : "+tAddress.getText()
+"\n The Area is :"+area
+"\n The Cost is : "+cost,"Information",
JOptionPane.INFORMATION_MESSAGE);
break;
default:
break;
}
}
public static void main(String args[])
{
FloorsRUs f1=new FloorsRUs("FloorsRUs Easy App");
f1.setSize(new Dimension(650,450));
f1.setVisible(true);
}
}
I've tried:
if(tLength.getText()<=0 && tWidth.getText()<=0)
{
System.out.println("invalid input, number has to be positive");
}
And it received errors for "Bad operand for binary operator '<='".
I believe because tLength
and tWidth
are both String
.
So then I tried:
case "Calculate the Area":
System.out.println("Calc 1 Complete");
area=Double.parseDouble(tLength.getText())
*Double.parseDouble(tWidth.getText());
if((Double.parseDouble(tLength.getText())<=0) &&
((Double.parseDouble(tWidth.getText())<=0)))
{
System.out.println("invalid input, number has to be positive");
}
break;
It accepted it but didn't change the output when I put a 0
or negative number
into the tLength or tWidth field.
So finally I tried:
case "Calculate the Area":
System.out.println("Calc 1 Complete");
try {
if ((Double.parseDouble(tLength.getText()) < 0)
&& ((Double.parseDouble(tWidth.getText()) < 0))) {
System.out.println("invalid input, number has to be
positive");
} else {
area = Double.parseDouble(tLength.getText()) *
Double.parseDouble(tWidth.getText());
}
} catch (NumberFormatException e) {
System.out.println("invalid input, number has to be positive");
}
break;
Also, no errors but it didn't work when negative numbers
or 0
were entered.
I have tried switching '&&'
to '||'
as well and no luck.