I have this program I have to write that calculates the volume of a pool and then displays the results along with how much water is needed to fill the pool and the cost to fill the pool if the cost is .77 cents per cubic foot plus a one time fee of $100. I have the code that calculates the pool volume, but I cant figure out how to get it to calculate how much water is needed or the cost. I am stummped. If someone could direct me in the right way to do this I would be so greatful. Here is the code I have so far.
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class PoolVolume {
private JFrame mainFrame;
private JButton calcButton, exitButton;
private JTextField length, width, depth, volume;
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel;
public PoolVolume() {
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate volume");
exitButton = new JButton("Exit");
length = new JTextField(5);
width = new JTextField(5);
depth = new JTextField(5);
volume = new JTextField(5);
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
depthLabel = new JLabel("Enter the depth dept of the swimming pool: ");
volumeLabel = new JLabel("Swimming pool volume: ");
JPanel c=new JPanel(new GridLayout(5,2));
c.add(lengthLabel);
c.add(length);
c.add(widthLabel);
c.add(width);
c.add(depthLabel);
c.add(depth);
c.add(volumeLabel);
c.add(volume);
c.add(calcButton);
c.add(exitButton);
calcButton.setMnemonic('C');
exitButton.setMnemonic('x');
mainFrame.setSize(500,200);
mainFrame.add(c);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
depth.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cdepth, cVolume, Total;
String sLength, sWidth, sdepth, sVolume;
sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sdepth =depth.getText();
cdepth = Double.parseDouble(sdepth);
if(e.getSource() == calcButton) {
Total = cLength * cWidth * cdepth;
volume.setText(num.format(Total));
try{
String value=volume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}
class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new PoolVolume();
}
}