/**
* Driver program - Creates silo objects, asks for user input on dimensions of silo - returns results of size dimensions
*
* @author (Luke Schmid)
* @version (1)
*/
import javax.swing.*;
public class siloTest
{
final static int MAX = 0;
final static double pi = 22.0/7;
public static final int CANCEL_OPTION =0 ;
public static void main (String[]args)
{
//creates two seperate 'silo' objects.
silo c1 = new silo();
//Create MAX int for input comparison.
//sets iRadius to 0 to initialize while loop
double iRadius = 0;
while (iRadius <= MAX)
{
String pRadius1 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 1");
try
{
iRadius = Double.parseDouble(pRadius1);
//set radius of c1 'silo' object
c1.setRadius(iRadius);
}
catch (NumberFormatException nfe)
{
iRadius = -1; // just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will execute again
if (iRadius <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
//Sets iHeight to 0 to intialize loop
double iHeight = 0;
while (iHeight <= MAX)
{
String pHeight1 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 1");
try
{
iHeight = Double.parseDouble(pHeight1);
c1.setHeight(iHeight);
}
catch (NumberFormatException nfe)
{
iHeight = -1; //just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will continue
if (iHeight <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
//iRadius has been succesfully set in the above code, so it needs to be reset to 0 here to again start the loop
iRadius = 0;
while (iRadius <= MAX)
{
String pRadius2 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 2");
try
{
iRadius = Double.parseDouble(pRadius2);
}
catch (NumberFormatException nfe)
{
iRadius = -1; //just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//Determines whether loops starts again
if (iRadius <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
//Same story as iRadius
iHeight = 0;
while (iHeight <= MAX)
{
String pHeight2 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 2");
try
{
iHeight = Double.parseDouble(pHeight2);
}
catch (NumberFormatException nfe)
{
iHeight = -1; //to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will start again or not
if (iHeight <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
silo c2 = new silo(iRadius, iHeight);
displayDimensions(c1, c2);
String changeC1, changeC2;
int value = JOptionPane.showInputDialog (null, "Change the dimensions of silo 1? (y/n)");
if (value == JOptionPane.CANCEL_OPTION)
{
changeC1 = "n";
}
int value2 = JOptionPane.showInputDialog (null, "Change the dimensions of silo 2? (y/n)");
if (value == JOptionPane.CANCEL_OPTION)
{
changeC2 = "n";
}
if (changeC1.equalsIgnoreCase("y") && changeC2.equalsIgnoreCase("y"))
{
changeDimensionsOne(iRadius, iHeight, c1, MAX);
changeDimensionsTwo(iRadius, iHeight, c2, MAX);
displayDimensions(c1, c2);
}
else if (changeC1.equalsIgnoreCase("y"))
{
changeDimensionsOne(iRadius, iHeight, c1, MAX);
displayDimensions(c1, c2);
}
else if (changeC2.equalsIgnoreCase("y"))
{
changeDimensionsTwo(iRadius, iHeight, c2, MAX);
displayDimensions(c1,c2);
}
else
System.exit(0);
}
public static void displayDimensions(silo c1, silo c2)
{
JOptionPane.showMessageDialog(null, "Silo 1\nRadius = " + c1.getRadius() +
"\nHeight = " + c1.getHeight() +
"\nSurface Area = " + c1.getSurfaceArea(pi) +
"\nVolume = " + c1.calcVolume(pi));
JOptionPane.showMessageDialog(null, "Silo 2\nRadius = " + c2.getRadius() +
"\nHeight = " + c2.getHeight() +
"\nSurface Area = " + c2.getSurfaceArea(pi) +
"\nVolume = " + c2.calcVolume(pi));
}
public static void changeDimensionsOne(double iRadius, double iHeight, silo c1, int MAX)
{
iRadius = 0;
while (iRadius <= MAX)
{
String pRadius1 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 1");
try
{
iRadius = Double.parseDouble(pRadius1);
//set radius of c1 'silo' object
c1.setRadius(iRadius);
}
catch (NumberFormatException nfe)
{
iRadius = -1; // just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will execute again
if (iRadius <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
//Sets iHeight to 0 to intialize loop
iHeight = 0;
while (iHeight <= MAX)
{
String pHeight1 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 1");
try
{
iHeight = Double.parseDouble(pHeight1);
c1.setHeight(iHeight);
}
catch (NumberFormatException nfe)
{
iHeight = -1; //just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will continue
if (iHeight <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
public static void changeDimensionsTwo(double iRadius, double iHeight, silo c2, int MAX)
{
iRadius = 0;
while (iRadius <= MAX)
{
String pRadius2 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 2");
try
{
iRadius = Double.parseDouble(pRadius2);
}
catch (NumberFormatException nfe)
{
iRadius = -1; //just to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//Determines whether loops starts again
if (iRadius <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
//Same story as iRadius
iHeight = 0;
while (iHeight <= MAX)
{
String pHeight2 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 2");
try
{
iHeight = Double.parseDouble(pHeight2);
}
catch (NumberFormatException nfe)
{
iHeight = -1; //to be safe
}
//Stops programming from crashing if user presses Cancel button
catch (NullPointerException e)
{
System.exit(0);
}
//determines whether loop will start again or not
if (iHeight <= MAX)
JOptionPane.showMessageDialog (null, "Please enter a number higher than 0.\nAlso ensure you are entering an integer", "Error!", JOptionPane.ERROR_MESSAGE);
}
c2.setRadius(iRadius);
c2.setHeight(iHeight);
}
}