I'm implementation polymorphism and have one confusing that if I use instanceof then I'm trying to perform casting which voids polymorphism so will that be any way to avoid instanceof with the code and still have the program working? Block of code where I have used instanceof starts from line , also if anyone can point out if there is any redundant code here. Any help is much appreciated.
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//implements class Orders from Constant
public class Orders implements Constants
{
//Declaring array
private static Rocks[] rocksArray;
private static int numOrders;
/**
* @param args
*/
public static void main(String[] args)
{
numOrders = 0;
//setting the max value for array
rocksArray = new Rocks[MAX_ORDERS];
String choice;
while (true)
{
//Choice option begins --->
choice = "";
//While statement
while (!choice.equalsIgnoreCase("m") && !choice.equalsIgnoreCase("b")
&& !choice.equalsIgnoreCase("e") && !choice.equalsIgnoreCase("d"))
{
choice = JOptionPane.showInputDialog
(" Add monument-only order >> (m)\n " +
"Add a monument & base order >> (b) \n " +
"Stop entering orders >> (e)\n Display orders >> (d) ");
if (!choice.equalsIgnoreCase("m") && !choice.equalsIgnoreCase("b")
&& !choice.equalsIgnoreCase("e") && !choice.equalsIgnoreCase("d"))
{
JOptionPane.showMessageDialog(null, "Invalid option!");
}
}
if (choice.equalsIgnoreCase("e"))
{
break;
}
if (choice.equalsIgnoreCase("d"))
{
displayOutput();
}
else
{
//asking & getting user input -->
int quatity = Integer.parseInt(JOptionPane.showInputDialog
("Please enter the quatity: "));
double width = Double.parseDouble(JOptionPane.showInputDialog
("Please enter the width: "));
double thickness = Double.parseDouble(JOptionPane
.showInputDialog("Please enter the thickness: "));
double height = Double
.parseDouble(JOptionPane.showInputDialog
("Please enter the height: "));
if (choice.equalsIgnoreCase("m"))
{
Monument newMonument = new Monument
(quatity, width, thickness, height);
rocksArray[numOrders] = newMonument;
}
else
{
double baseThickness = Double.parseDouble(JOptionPane
.showInputDialog("Please enter the base thickness: "));
double baseDiameter = Double.parseDouble(JOptionPane
.showInputDialog("Please enter the base diameter: "));
MonumentBase newMonumentBase = new MonumentBase
(quatity, width, thickness, height,
baseThickness, baseDiameter);
rocksArray[numOrders] = newMonumentBase;
}
numOrders++;
} // user input ends
}
// Displays Output --->
displayOutput();
}
private static void displayOutput()
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numOrders; i++)
{
//appending the appropriate values for monument & monument base.
msg.append("Width: " + rocksArray[i].getWidth());
msg.append(" Height: " + rocksArray[i].getHeight());
msg.append(" Thickness: " + rocksArray[i].getThickness());
msg.append(" Cubic feet: " + rocksArray[i].getCubicFeet());
msg.append(" Cubic inches: " + rocksArray[i].getCubicInches());
msg.append(" Quantity: " + rocksArray[i].getQuantity());
msg.append(" Total price: " + rocksArray[i].getTotalPrice());
**if (rocksArray[i] instanceof MonumentBase)
{
msg.append(" Base diameter: " +
((MonumentBase) rocksArray[i]).getBaseDiameter());
msg.append(" Base radius: " +
((MonumentBase) rocksArray[i]).getBaseRadius());
msg.append(" Base thickness: " +
((MonumentBase) rocksArray[i]).getBaseThickness());
}
msg.append("\n");**
} // display output ends
// create a JTextArea
JTextArea textArea = new JTextArea(10, 80);
textArea.setText(msg.toString());
textArea.setEditable(false);
// wraping scrollpane around it
JScrollPane scrollPane = new JScrollPane(textArea);
// display them in a message dialog
JOptionPane.showMessageDialog(null, scrollPane);
}
} // end class