Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java program error in displaying Output

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Java program error in displaying Output

    I've been working on this project all night and have been unsuccessful in getting the values to display in output. All I get once I push the calculate button are 0's for each of the values, but the total is summed up. Please point me in the right direction.

    Thank you in advance!!!


    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    import java.awt.*;
     
    public class Presentation extends JFrame implements ActionListener, ItemListener 
    {
        // All the GUI objects
    JPanel mainPanel = new JPanel();
    JPanel discountPanel = new JPanel();
    JLabel storeLabel = new JLabel("       OrderBook INC.         ");
    JTextField bookNameTextField = new JTextField(20);
    JTextField quantityTextField = new JTextField(20);
    JRadioButton discountARadioButton = new JRadioButton(" A ");
        JRadioButton discountBRadioButton = new JRadioButton(" B ");
        JRadioButton discountCRadioButton = new JRadioButton(" C ");
        JRadioButton discountInvisibleRadioButton = new JRadioButton("");
        ButtonGroup discountButtonGroup = new ButtonGroup();
    JTextField priceTextField = new JTextField(20);
        JButton calculateButton = new JButton(" Calculate   ");
    JTextArea outputTextArea = new JTextArea("Books Ordered", 10,25);
    JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
     
        //object of the font
        Font storeFont = new Font("Arial",Font.BOLD,14);
     
        public static void main(String[] args) 
        {
            //create an object of the class and code the close button
            Presentation myBooks = new Presentation();
            myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
        public Presentation() 
        {
            designFrame();
            add(mainPanel);
            setTitle("OrderBook INC.");
            setSize(304,600);
            setVisible(true);        
        }
     
        public void designFrame() 
        {
          //set the font and the color
    storeLabel.setFont(storeFont);
    storeLabel.setForeground(Color.GRAY);
          //add the components to the mainPanel
          mainPanel.add(storeLabel);
          mainPanel.add(new JLabel("       Book Name            "));
          mainPanel.add(bookNameTextField);
          mainPanel.add(new JLabel("    Quantity Ordered   "));
          mainPanel.add(quantityTextField);
          //adding discount radio buttons to car radio button group 
          mainPanel.add(new JLabel("   Discount Type   "));
    discountButtonGroup.add(discountARadioButton);
        discountButtonGroup.add(discountBRadioButton);
        discountButtonGroup.add(discountCRadioButton);
        discountButtonGroup.add(discountInvisibleRadioButton);
        //adding radio buttons to panel
        mainPanel.add(discountARadioButton);
        mainPanel.add(discountBRadioButton);
        mainPanel.add(discountCRadioButton);
          priceTextField.setEditable(false);
    calculateButton.setEnabled(false);
          mainPanel.add(new JLabel("    Price       "));
          mainPanel.add(priceTextField);
          mainPanel.add(calculateButton);
          mainPanel.add(outputScrollPane);
     
          //add the listeners
          priceTextField.addActionListener(this);
          calculateButton.addActionListener(this);
          discountARadioButton.addItemListener(this);
          discountBRadioButton.addItemListener(this);
        discountCRadioButton.addItemListener(this);
     
          //add the panel to the frame
          add(mainPanel);
          setSize(300,400);
          setVisible(true);
    }
     
    public void itemStateChanged(ItemEvent evt)
    {
    if(discountARadioButton.isSelected())    
        {
            enabledComponents();
        }
        else if (discountBRadioButton.isSelected())
        {
            enabledComponents();
        }
        else if (discountCRadioButton.isSelected())
        {
            enabledComponents();
        }
    }
     
    public void actionPerformed(ActionEvent evt)
    {
    getInput();
    clear();
    }
     
    public void getInput() 
    {      
    //declaring local variables 
    int quantityInteger;
    int discountTypeInteger;
    double priceDouble;
    double discountDouble;
    String nameString;
    char discountChar;
     
    // get input
    try
    {
            quantityInteger = Integer.parseInt(quantityTextField.getText());
            try
            {
            priceDouble = Double.parseDouble(priceTextField.getText());
            }
     
            catch(NumberFormatException err)
            {
                JOptionPane.showMessageDialog(null, "Price is invalid");
                priceTextField.selectAll();
                priceTextField.requestFocus();
            }
            }
        catch(NumberFormatException err)
        {
            JOptionPane.showMessageDialog(null, "Quantity is invalid");
            quantityTextField.selectAll();
            quantityTextField.requestFocus();
        }
     
     
        if(discountARadioButton.isSelected())
        {
    discountChar = 'a';
            discountTypeInteger = getTypeOfDiscount(discountChar);
            nameString = bookNameTextField.getText();
    quantityInteger = Integer.parseInt(quantityTextField.getText());
            priceDouble = Double.parseDouble(priceTextField.getText());
     
            if((discountTypeInteger != -1))
            {    
                if (!(nameString.equals("")))
                {
    Calculation myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble);
    discountDouble = myOrder.getDiscount();
    displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
    clear();
                    }
                    else
                    {
    JOptionPane.showMessageDialog(null, "Please Enter Book Name");
                    bookNameTextField.selectAll();
                    bookNameTextField.requestFocus();
                    }
                }
            }
        else if(discountBRadioButton.isSelected())
        {
            discountChar = 'b';
    discountTypeInteger = getTypeOfDiscount(discountChar);
            nameString = bookNameTextField.getText();
    quantityInteger = Integer.parseInt(quantityTextField.getText());
    priceDouble = Double.parseDouble(priceTextField.getText());
     
    if((discountTypeInteger != -1))
                {    
                    if (!(nameString.equals("")))
                    {
    Calculation myOrder = new Calculation(quantityInteger, 
                    discountTypeInteger, priceDouble);
    discountDouble = myOrder.getDiscount();
    displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
                    clear();
                    }
                    else
                    {
    JOptionPane.showMessageDialog(null, "Please Enter Book Name");
                    bookNameTextField.selectAll();
                    bookNameTextField.requestFocus();
                    }
     
                }
            }
    else if(discountCRadioButton.isSelected())
        {
            discountChar = 'c';
            discountTypeInteger = getTypeOfDiscount(discountChar);
            nameString = bookNameTextField.getText();
    quantityInteger = Integer.parseInt(quantityTextField.getText());
            priceDouble = Double.parseDouble(priceTextField.getText());
     
                if((discountTypeInteger != -1))
                {    
                    if (!(nameString.equals("")))
                    {
                    Calculation myOrder = new
    Calculation(quantityInteger, 
                    discountTypeInteger, priceDouble);
                    discountDouble = myOrder.getDiscount();
    displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
                    clear();
                    }
                    else
                    {
    JOptionPane.showMessageDialog(null, "Please Enter Book Name");
                    bookNameTextField.selectAll();
                    bookNameTextField.requestFocus();
                    }
    }
    }
    try
    {
    quantityInteger = Integer.parseInt(quantityTextField.getText());
     
        try
        {
        priceDouble = Double.parseDouble(priceTextField.getText());
     
        }
        catch(NumberFormatException err)
        {
        JOptionPane.showMessageDialog(null, "Price is Invalid");
        priceTextField.selectAll();
        priceTextField.requestFocus();
        }
        }
        catch(NumberFormatException err)
        {
        JOptionPane.showMessageDialog(null, "Quantity is Invalid");
        quantityTextField.selectAll();
        quantityTextField.requestFocus();
        }
        }
     
    //set text fields visible once a discount is selected    
    public void enabledComponents()
    {
        priceTextField.setEditable(true);
        calculateButton.setEnabled(true);
    }
     
    public int getTypeOfDiscount(char discountChar)
    {        
        int discountTypeInteger = 0;
        {
        if(discountARadioButton.isSelected())
        {
        discountTypeInteger = 1;
        }
        else if(discountBRadioButton.isSelected())
        {
        discountTypeInteger = 2;
        }
        else if(discountCRadioButton.isSelected())
        {
        discountTypeInteger = 3;
        }
        else if(discountInvisibleRadioButton.isSelected())
        {
        discountTypeInteger = -1;
        }
        }
    return discountTypeInteger;
        }
     
        public void displayOutput(int quantityInteger, double priceDouble, 
            double discountDouble, String nameString, char discountChar) 
        {
            //object to format to currency
          DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
          DecimalFormat formatNumberFormat = new DecimalFormat("0");
    Calculation myCalcs = new Calculation();
     
          String outputString;
     
          if(discountChar == 'a')
        {            
        outputString = "Name of the Book: " + nameString + '\n' + 
    "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
    + '\n' + "Price:     " + formatDecimalFormat.format(myCalcs.getPrice())
            + '\n' + "Discount Percentage: 20% " +
    formatDecimalFormat.format(myCalcs.getDiscount())
            + '\n' + "Subtotal:     " + 
    formatDecimalFormat.format(myCalcs.getSubTotal())
            + '\n' + "Shipping:     " +
    formatDecimalFormat.format(myCalcs.getShipping())
            + '\n' + "Tax:     " +
    formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
            "Total:    " +    formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
        "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
    + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
        outputTextArea.append(outputString);
        }
        else if(discountChar == 'b')
            {            
        outputString = "Name of the Book: " + nameString + '\n' + 
    "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
    + '\n' + "Price:     " + formatDecimalFormat.format(myCalcs.getPrice())
            + '\n' + "Discount Percentage: 20% " +
    formatDecimalFormat.format(myCalcs.getDiscount())
            + '\n' + "Subtotal:     " + 
    formatDecimalFormat.format(myCalcs.getSubTotal())
            + '\n' + "Shipping:     " +
    formatDecimalFormat.format(myCalcs.getShipping())
            + '\n' + "Tax:     " +
    formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
            "Total:    " +    formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
        "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
    + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
        outputTextArea.append(outputString);
        }
     
        else if(discountChar == 'c')
        {            
        outputString = "Name of the Book: " + nameString + '\n' + 
    "Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
    + '\n' + "Price:     " + formatDecimalFormat.format(myCalcs.getPrice())
            + '\n' + "Discount Percentage: 20% " +
    formatDecimalFormat.format(myCalcs.getDiscount())
            + '\n' + "Subtotal:     " + 
    formatDecimalFormat.format(myCalcs.getSubTotal())
            + '\n' + "Shipping:     " +
    formatDecimalFormat.format(myCalcs.getShipping())
            + '\n' + "Tax:     " +
    formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
            "Total:    " +    formatDecimalFormat.format(myCalcs.getTotal())+'\n' +'\n' +
        "Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n'
    + "Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())+ '\n';
        outputTextArea.append(outputString);
        }
    }
        }
     
        public void  clear()
        {
    //clear existing text from text fields and request cursor to top
    bookNameTextField.setText("");
    quantityTextField.setText("");
    discountInvisibleRadioButton.setSelected(true);
    priceTextField.setText("");
    bookNameTextField.requestFocus();
    priceTextField.setEditable(false);
    calculateButton.setEnabled(false);  
        }    
    }


  2. #2
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP!!! Need help with Output

    Here is the Calculation class just in case.

     
    public class Calculation 
    {
    	private double priceDouble, subTotalDouble, taxDouble, 
    	shippingDouble, totalDouble, discountDouble;
    	private int quantityInteger, discountTypeInteger;
    	private static double grandTotalDouble;
    	private static int totalNumberOfOrdersInteger;
    	private final double DISCOUNT_A = 0.20;
    	private final double DISCOUNT_B = 0.10;
    	private final double DISCOUNT_C = 0;
    	private final double TAX_RATE = 0.0825;
    	private final int SHIPPING_RATE_INTEGER = 1;
     
     
     
    	public Calculation()
    	{
     
    	}
     
    	public Calculation(int quantityNewInteger, int discountTypeNewInteger, double priceNewDouble)
    	{
    		setQuantity(quantityNewInteger);
    		setPrice(priceNewDouble);
    		setDiscount(discountTypeNewInteger);
    		calculate();
    	}
     
    	private void setQuantity(int quantityNewInteger)
    	{
    		//assign public variable to private
    		quantityInteger = quantityNewInteger;
     
    		}
    	private void setPrice(double priceNewDouble)
    	{
    		//assign public variable to private
    		priceDouble = priceNewDouble;
     
    		}
    	private void setDiscount(int discountTypeNewInteger)
    	{
    		//assign public variable to private
    		discountTypeInteger = discountTypeNewInteger;
    	}
    	private void calculate()
    	{		
    		//calculate the subTotal of the order
    		subTotalDouble = quantityInteger * priceDouble;
    		//calculate the shipping of the order
    		shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger;
    		//calculate the Tax of the order
    		taxDouble = subTotalDouble * TAX_RATE;
    		//calculate the Total including shipping and tax of the order
    		totalDouble = taxDouble + subTotalDouble + shippingDouble;
    		// calculate the total of all ordersrs
    		//and the number of orders processed
    		grandTotalDouble += totalDouble;
     
    		switch(discountTypeInteger)
    		{
    			case 1:
    				calculateADiscount();
    				totalNumberOfOrdersInteger++;
     
    				break;
    			case 2:
    				calculateBDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    			case 3:
    				calculateCDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    		}
     
    	}
     
    	private void calculateADiscount()
    	{
    		//calculate the subTotal of the order
    		subTotalDouble = priceDouble * quantityInteger;
    		//calculate the discount of the order
    		discountDouble = subTotalDouble * DISCOUNT_A;
    		//calculate the shipping of the order
    		shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger;
    		//calculate the Tax of the order
    		taxDouble = subTotalDouble * TAX_RATE;
    		//calculate the Total including shipping and tax of the order
    		totalDouble = taxDouble + subTotalDouble + shippingDouble;
     
    	}
    	//calculation of rental fees
    	private void calculateBDiscount()
    	{
    		//calculate the subTotal of the order
    		subTotalDouble = priceDouble * quantityInteger;
    		//calculate the discount of the order
    		discountDouble = subTotalDouble * DISCOUNT_B;
    		//calculate the shipping of the order
    		shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger;
    		//calculate the Tax of the order
    		taxDouble = subTotalDouble * TAX_RATE;
    		//calculate the Total including shipping and tax of the order
    		totalDouble = taxDouble + subTotalDouble + shippingDouble;
    	}
    	//calculation of rental fees
    	private void calculateCDiscount()
    	{
    		//calculate the subTotal of the order
    		subTotalDouble = priceDouble * quantityInteger;
    		//calculate the discount of the order
    		discountDouble = DISCOUNT_C;
    		//calculate the shipping of the order
    		shippingDouble = SHIPPING_RATE_INTEGER * quantityInteger;
    		//calculate the Tax of the order
    		taxDouble = subTotalDouble * TAX_RATE;
    		//calculate the Total including shipping and tax of the order
    		totalDouble = taxDouble + subTotalDouble + shippingDouble;
    	}
     
     
    	public double getQuantity()
    	{
    		//returning quantity
    		return quantityInteger;
     
    	}
     
    	public double getPrice()
    	{
    		//returning price
    		return priceDouble;
    	}
     
    	public double getDiscount()
    	{
    		//returning discount
    		return discountDouble;
     
    	}
    	public double getSubTotal()
    	{
    		//returning subTotal
    		return subTotalDouble;
     
    	}
     
    	public double getShipping()
    	{
    		//returning shipping
    		return shippingDouble;
     
    	}
     
    	public double getTax()
    	{
    		//returning Tax
    		return taxDouble;
     
    	}
     
    	public double getTotal()
    	{
    		//returning grand total of orders
    		return totalDouble;
    	}
     
    	public double getGrandTotal()
    	{
    		//returning grand total of orders
    		return grandTotalDouble;
    	}
     
    	public int getNumberOfOrders()
    	{
    		//return total number of orders processed
    		return totalNumberOfOrdersInteger;
    	}	
    }

  3. #3
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 10 Times in 4 Posts

    Default Re: Please HELP!!! Need help with Output

    I keep getting "Quantity is Invalid" no matter what I enter...

    I'm looking over the code for you now.

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP!!! Need help with Output

    Ok I fixed that error..... I don't know why but I placed my try and catch twice. Removed the bottom one and now I don't have that error.

    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    import java.awt.*;
     
    public class Presentation extends JFrame implements ActionListener, ItemListener 
    {
        // All the GUI objects
        JPanel mainPanel = new JPanel();
    	JPanel discountPanel = new JPanel();
        JLabel storeLabel = new JLabel("       OrderBook INC.         ");
        JTextField bookNameTextField = new JTextField(20);
        JTextField quantityTextField = new JTextField(20);
        JRadioButton discountARadioButton = new JRadioButton(" A ");
    	JRadioButton discountBRadioButton = new JRadioButton(" B ");
    	JRadioButton discountCRadioButton = new JRadioButton(" C ");
    	JRadioButton discountInvisibleRadioButton = new JRadioButton("");
    	ButtonGroup discountButtonGroup = new ButtonGroup();
        JTextField priceTextField = new JTextField(20);
    	JButton calculateButton = new JButton(" Calculate   ");
        JTextArea outputTextArea = new JTextArea("Books Ordered", 10, 25);
        JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
     
        //object of the font
        Font storeFont = new Font("Arial",Font.BOLD,14);
     
        public static void main(String[] args) 
        {
            //create an object of the class and code the close button
            Presentation myBooks = new Presentation();
            myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
        public Presentation() 
        {
    		designFrame();
    		add(mainPanel);
    		setTitle("OrderBook INC.");
    		setSize(304,600);
    		setVisible(true);        
        }
     
        public void designFrame() 
        {
            //set the font and the color
            storeLabel.setFont(storeFont);
            storeLabel.setForeground(Color.GRAY);
     
            //add the components to the mainPanel
            mainPanel.add(storeLabel);
            mainPanel.add(new JLabel("       Book Name            "));
            mainPanel.add(bookNameTextField);
            mainPanel.add(new JLabel("    Quantity Ordered   "));
            mainPanel.add(quantityTextField);
            //adding discount radio buttons to car radio button group 
            mainPanel.add(new JLabel("   Discount Type   "));
    		discountButtonGroup.add(discountARadioButton);
    		discountButtonGroup.add(discountBRadioButton);
    		discountButtonGroup.add(discountCRadioButton);
    		discountButtonGroup.add(discountInvisibleRadioButton);
    		//adding radio buttons to panel
    		mainPanel.add(discountARadioButton);
    		mainPanel.add(discountBRadioButton);
    		mainPanel.add(discountCRadioButton);
            priceTextField.setEditable(false);
    		calculateButton.setEnabled(false);
            mainPanel.add(new JLabel("    Price       "));
            mainPanel.add(priceTextField);
            mainPanel.add(calculateButton);
            mainPanel.add(outputScrollPane);
     
            //add the listeners
            priceTextField.addActionListener(this);
            calculateButton.addActionListener(this);
            discountARadioButton.addItemListener(this);
            discountBRadioButton.addItemListener(this);
    		discountCRadioButton.addItemListener(this);
     
            //add the panel to the frame
            add(mainPanel);
            setSize(300,400);
            setVisible(true);
        }
     
      //create a listener for the Radio Buttons
    	public void itemStateChanged(ItemEvent evt)
    	{
    		if(discountARadioButton.isSelected())	
    		{
    			enabledComponents();
    		}
    		else if (discountBRadioButton.isSelected())
    		{
    			enabledComponents();
    		}
    		else if (discountCRadioButton.isSelected())
    		{
    			enabledComponents();
    		}
    	}
        public void actionPerformed(ActionEvent evt)
     
        {
            getInput();
            clear();        
        }
     
        //declaring local variables    	
     
     
     
        public void getInput() 
        {  	
            //declaring local variables 
        	char discountChar;
        	int quantityInteger;
        	int discountTypeInteger;
        	double priceDouble;
        	double discountDouble;
        	String nameString;
     
            // get input
        	try
    		{
    		quantityInteger = Integer.parseInt(quantityTextField.getText());
     
    		try
    		{
    			priceDouble = Double.parseDouble(priceTextField.getText());
     
    		}
    		catch(NumberFormatException err)
    		{
    			JOptionPane.showMessageDialog(null, "Price is invalid");
    			priceTextField.selectAll();
    			priceTextField.requestFocus();
    		}
    		}
    		catch(NumberFormatException err)
    		{
    			JOptionPane.showMessageDialog(null, "Quantity is invalid");
    			quantityTextField.selectAll();
    			quantityTextField.requestFocus();
    		}
     
     
    		if(discountARadioButton.isSelected())
    		{
    			discountChar = 'a';
    			discountTypeInteger = getTypeOfDiscount(discountChar);
    			nameString = bookNameTextField.getText();
    			quantityInteger = Integer.parseInt(quantityTextField.getText());
    			priceDouble = Double.parseDouble(priceTextField.getText());
     
    			if((discountTypeInteger != -1))
    			{	
    				if (!(nameString.equals("")))
    				{
    					Calculation myOrder = new Calculation(quantityInteger, 
    							discountTypeInteger, priceDouble);
    					discountDouble = myOrder.getDiscount();
    					displayOutput(quantityInteger, priceDouble, discountDouble, 
    							nameString, discountChar);
    					clear();
    					}
    					else
    					{
    					JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    					bookNameTextField.selectAll();
    					bookNameTextField.requestFocus();
    					}
    				}
    			}
    		else if(discountBRadioButton.isSelected())
    		{
    			discountChar = 'b';
    			discountTypeInteger = getTypeOfDiscount(discountChar);
    			nameString = bookNameTextField.getText();
    			quantityInteger = Integer.parseInt(quantityTextField.getText());
    			priceDouble = Double.parseDouble(priceTextField.getText());
     
    			if((discountTypeInteger != -1))
    			{	
    				if (!(nameString.equals("")))
    				{
    					Calculation myOrder = new Calculation(quantityInteger, 
    							discountTypeInteger, priceDouble);
    					discountDouble = myOrder.getDiscount();
    					displayOutput(quantityInteger, priceDouble, discountDouble, 
    							nameString, discountChar);
    					clear();
    					}
    					else
    					{
    					JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    					bookNameTextField.selectAll();
    					bookNameTextField.requestFocus();
    					}
    				}
    			}
    			else if(discountCRadioButton.isSelected())
    			{
    				discountChar = 'c';
    				discountTypeInteger = getTypeOfDiscount(discountChar);
    				nameString = bookNameTextField.getText();
    				quantityInteger = Integer.parseInt(quantityTextField.getText());
    				priceDouble = Double.parseDouble(priceTextField.getText());
     
    				if((discountTypeInteger != -1))
    				{	
    					if (!(nameString.equals("")))
    					{
    						Calculation myOrder = new Calculation(quantityInteger, 
    								discountTypeInteger, priceDouble);
    						discountDouble = myOrder.getDiscount();
    						displayOutput(quantityInteger, priceDouble, discountDouble, 
    								nameString, discountChar);
    						clear();
    						}
    						else
    						{
    						JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    						bookNameTextField.selectAll();
    						bookNameTextField.requestFocus();
    						}
    			}
    		}
    	}			
        //set text fields visible once a discount is selected	
    	public void enabledComponents()
    	{
    		priceTextField.setEditable(true);
    		calculateButton.setEnabled(true);
     
     
    	}
     
        public int getTypeOfDiscount(char discountChar)
    	{		
    		int discountTypeInteger = 0;
    		{
    			if(discountARadioButton.isSelected())
    			{
     
    				discountTypeInteger = 1;
    			}
    			else if(discountBRadioButton.isSelected())
    			{
    				discountTypeInteger = 2;
    			}
    			else if(discountCRadioButton.isSelected())
    			{
    				discountTypeInteger = 3;
    			}
    			else if(discountInvisibleRadioButton.isSelected())
    			{
    				discountTypeInteger = -1;
    			}
    		}
    		return discountTypeInteger;
    	}
     
        public void displayOutput(int quantityInteger, double priceDouble, 
        		double discountDouble, String nameString, char discountChar) 
        {
        	 //object to format to currency
            DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
            DecimalFormat formatNumberFormat = new DecimalFormat("0");
     
     
         Calculation myCalcs = new Calculation();
     
            String outputString;
     
            if(discountChar == 'a')
    		{			
    		outputString =  '\n' + "Name of the Book: " + nameString + '\n' + 
        		"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
        		+ '\n' + "Price: 	" + formatDecimalFormat.format(myCalcs.getPrice())
        		+ '\n' + "Discount Percentage: 20% " + formatDecimalFormat.format(myCalcs.getDiscount())
        		+ '\n' + "Subtotal: 	" + formatDecimalFormat.format(myCalcs.getSubTotal())
        		+ '\n' + "Shipping: 	" + formatDecimalFormat.format(myCalcs.getShipping())
        		+ '\n' + "Tax: 	" + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
        		"Total:	" +	formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + 
        		"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + 
        		"Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())
        		+ '\n';
    		outputTextArea.append(outputString);
    		}
    		else if(discountChar == 'b')
    		{			
    			outputString =  '\n' + "Name of the Book: " + nameString + '\n' + 
        		"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
        		+ '\n' + "Price: 	" + formatDecimalFormat.format(myCalcs.getPrice())
        		+ '\n' + "Discount Percentage: 10% " + formatDecimalFormat.format(myCalcs.getDiscount())
        		+ '\n' + "Subtotal: 	" + formatDecimalFormat.format(myCalcs.getSubTotal())
        		+ '\n' + "Shipping: 	" + formatDecimalFormat.format(myCalcs.getShipping())
        		+ '\n' + "Tax: 	" + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
        		"Total:	" +	formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + 
        		"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + 
        		"Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())
        		+ '\n';
    		outputTextArea.append(outputString);
    		}
    		else if(discountChar == 'c')
    		{			
    			outputString =  '\n' + "Name of the Book: " + nameString + '\n' +
        		"Quantity Ordered: " + formatNumberFormat.format(myCalcs.getQuantity())
        		+ '\n' + "Price: 	" + formatDecimalFormat.format(myCalcs.getPrice())
        		+ '\n' + "No Discount: " + formatDecimalFormat.format(myCalcs.getDiscount())
        		+ '\n' + "Subtotal: 	" + formatDecimalFormat.format(myCalcs.getSubTotal())
        		+ '\n' + "Shipping: 	" + formatDecimalFormat.format(myCalcs.getShipping())
        		+ '\n' + "Tax: 	" + formatDecimalFormat.format(myCalcs.getTax()) + '\n'+ 
        		"Total:	" +	formatDecimalFormat.format(myCalcs.getTotal()) + '\n' +'\n' + 
        		"Total Quantity To-Date: " + (myCalcs.getNumberOfOrders()) + '\n' + 
        		"Grand Total To-Date: " + formatDecimalFormat.format(myCalcs.getGrandTotal())
        		+ '\n';
    		outputTextArea.append(outputString);
    	}
        }
     
        public void  clear()
        {
        	//clear existing text from text fields and request cursor to top
        	bookNameTextField.setText("");
            quantityTextField.setText("");
    		discountInvisibleRadioButton.setSelected(true);
            priceTextField.setText("");
            bookNameTextField.requestFocus();
            priceTextField.setEditable(false);
    		calculateButton.setEnabled(false);
     
        }    
    }

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP!!! Need help with Output

    Quote Originally Posted by Flash View Post
    I keep getting "Quantity is Invalid" no matter what I enter...

    I'm looking over the code for you now.
    Any luck Flash?

  6. #6
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Please HELP!!! Need help with Output

    First of all, there is no need to create two calculation objects as one will suffice, further more when using the try/catch block don't ignore the stacktraces as you did in your try catch blok. I've made some changes and comments in your code.


    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    import java.awt.*;
     
    public class Presentation extends JFrame implements ActionListener, ItemListener 
    {
    	// All the GUI objects
    	JPanel mainPanel = new JPanel();
    	JPanel discountPanel = new JPanel();
    	JLabel storeLabel = new JLabel("       OrderBook INC.         ");
    	JTextField bookNameTextField = new JTextField(20);
    	JTextField quantityTextField = new JTextField(20);
    	JRadioButton discountARadioButton = new JRadioButton(" A ");
    	JRadioButton discountBRadioButton = new JRadioButton(" B ");
    	JRadioButton discountCRadioButton = new JRadioButton(" C ");
    	JRadioButton discountInvisibleRadioButton = new JRadioButton("");
    	ButtonGroup discountButtonGroup = new ButtonGroup();
    	JTextField priceTextField = new JTextField(20);
    	JButton calculateButton = new JButton(" Calculate   ");
    	JTextArea outputTextArea = new JTextArea("Books Ordered", 10,25);
    	JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
    	//create only one instance, not two!
    	Calculation myOrder ;
    	//object of the font
    	Font storeFont = new Font("Arial",Font.BOLD,14);
     
    	public static void main(String[] args) 
    	{
    		//create an object of the class and code the close button
    		Presentation myBooks = new Presentation();
    		myBooks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	public Presentation() 
    	{
    		designFrame();
    		add(mainPanel);
    		setTitle("OrderBook INC.");
    		setSize(304,600);
    		setVisible(true);        
    	}
     
    	public void designFrame() 
    	{
    		//set the font and the color
    		storeLabel.setFont(storeFont);
    		storeLabel.setForeground(Color.GRAY);
    		//add the components to the mainPanel
    		mainPanel.add(storeLabel);
    		mainPanel.add(new JLabel("       Book Name            "));
    		mainPanel.add(bookNameTextField);
    		mainPanel.add(new JLabel("    Quantity Ordered   "));
    		mainPanel.add(quantityTextField);
    		//adding discount radio buttons to car radio button group 
    		mainPanel.add(new JLabel("   Discount Type   "));
    		discountButtonGroup.add(discountARadioButton);
    		discountButtonGroup.add(discountBRadioButton);
    		discountButtonGroup.add(discountCRadioButton);
    		discountButtonGroup.add(discountInvisibleRadioButton);
    		//adding radio buttons to panel
    		mainPanel.add(discountARadioButton);
    		mainPanel.add(discountBRadioButton);
    		mainPanel.add(discountCRadioButton);
    		priceTextField.setEditable(false);
    		calculateButton.setEnabled(false);
    		mainPanel.add(new JLabel("    Price       "));
    		mainPanel.add(priceTextField);
    		mainPanel.add(calculateButton);
    		mainPanel.add(outputScrollPane);
     
    		//add the listeners
    		priceTextField.addActionListener(this);
    		calculateButton.addActionListener(this);
    		discountARadioButton.addItemListener(this);
    		discountBRadioButton.addItemListener(this);
    		discountCRadioButton.addItemListener(this);
     
    		//add the panel to the frame
    		add(mainPanel);
    		setSize(300,400);
    		setVisible(true);
    	}
     
    	public void itemStateChanged(ItemEvent evt)
    	{
    		if(discountARadioButton.isSelected())	
    		{
    			enabledComponents();
    		}
    		else if (discountBRadioButton.isSelected())
    		{
    			enabledComponents();
    		}
    		else if (discountCRadioButton.isSelected())
    		{
    			enabledComponents();
    		}
    	}
     
    	public void actionPerformed(ActionEvent evt)
    	{
    		getInput();
    		clear();
    	}
     
    	public void getInput() 
    	{  	
    //		declaring local variables 
    		int quantityInteger;
    		int discountTypeInteger;
    		double priceDouble;
    		double discountDouble;
    		String nameString;
    		char discountChar;
     
    //		get input
     
    		try{
    			//	quantityInteger = Integer.parseInt(quantityTextField.getText());		
    			//	priceDouble = Double.parseDouble(priceTextField.getText());
     
    		}
    		catch(NumberFormatException e){
    			//its ALWAYS a good idea to print the stacktrace.
    		}
     
     
     
     
    		if(discountARadioButton.isSelected())
    		{
    			discountChar = 'a';
    			discountTypeInteger = getTypeOfDiscount(discountChar);
    			nameString = bookNameTextField.getText();
     
    			//You should try catch this.
     
    			try{
    				quantityInteger = Integer.parseInt(quantityTextField.getText());
    				priceDouble = Double.parseDouble(priceTextField.getText());
     
    			if((discountTypeInteger != -1))
    			{	
    				if (!(nameString.equals("")))
    				{
    					myOrder = new Calculation(quantityInteger, discountTypeInteger, priceDouble);
    					discountDouble = myOrder.getDiscount();
    					displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
    					clear();
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    					bookNameTextField.selectAll();
    					bookNameTextField.requestFocus();
    				}
    			}
    			}
    			catch(NumberFormatException e){
    				outputTextArea.setText("Please recorrect your input.");
    				//its ALWAYS a good idea to print the stacktrace.
    			}
    		}
    		else if(discountBRadioButton.isSelected())
    		{
    			discountChar = 'b';
    			discountTypeInteger = getTypeOfDiscount(discountChar);
    			nameString = bookNameTextField.getText();
    			quantityInteger = Integer.parseInt(quantityTextField.getText());
    			priceDouble = Double.parseDouble(priceTextField.getText());
     
    			if((discountTypeInteger != -1))
    			{	
    				if (!(nameString.equals("")))
    				{
    					myOrder = new Calculation(quantityInteger, 
    							discountTypeInteger, priceDouble);
    					discountDouble = myOrder.getDiscount();
    					displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
    					clear();
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    					bookNameTextField.selectAll();
    					bookNameTextField.requestFocus();
    				}
     
    			}
    		}
    		else if(discountCRadioButton.isSelected())
    		{
    			discountChar = 'c';
    			discountTypeInteger = getTypeOfDiscount(discountChar);
    			nameString = bookNameTextField.getText();
    			quantityInteger = Integer.parseInt(quantityTextField.getText());
    			priceDouble = Double.parseDouble(priceTextField.getText());
     
    			if((discountTypeInteger != -1))
    			{	
    				if (!(nameString.equals("")))
    				{
    					myOrder = new Calculation(quantityInteger, 
    							discountTypeInteger, priceDouble);
    					discountDouble = myOrder.getDiscount();
    					displayOutput(quantityInteger, priceDouble, discountDouble, nameString, discountChar);
    					clear();
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(null, "Please Enter Book Name");
    					bookNameTextField.selectAll();
    					bookNameTextField.requestFocus();
    				}
    			}
    		}	
    	}
     
    //	set text fields visible once a discount is selected	
    	public void enabledComponents()
    	{
    		priceTextField.setEditable(true);
    		calculateButton.setEnabled(true);
    	}
     
    	public int getTypeOfDiscount(char discountChar)
    	{		
    		int discountTypeInteger = 0;
    		{
    			if(discountARadioButton.isSelected())
    			{
    				discountTypeInteger = 1;
    			}
    			else if(discountBRadioButton.isSelected())
    			{
    				discountTypeInteger = 2;
    			}
    			else if(discountCRadioButton.isSelected())
    			{
    				discountTypeInteger = 3;
    			}
    			else if(discountInvisibleRadioButton.isSelected())
    			{
    				discountTypeInteger = -1;
    			}
    		}
    		return discountTypeInteger;
    	}
     
    	public void displayOutput(int quantityInteger, double priceDouble, 
    			double discountDouble, String nameString, char discountChar) 
    	{
    		//object to format to currency
    		DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
    		DecimalFormat formatNumberFormat = new DecimalFormat("0");
    		//Calculation myCalcs = new Calculation();
     
    		String outputString;
     
    		if(discountChar == 'a')
    		{			
    			outputString = "Name of the Book: " + nameString + '\n' + 
    			"Quantity Ordered: " + formatNumberFormat.format(myOrder.getQuantity())
    			+ '\n' + "Price: 	" + formatDecimalFormat.format(myOrder.getPrice())
    			+ '\n' + "Discount Percentage: 20% " +
    			formatDecimalFormat.format(myOrder.getDiscount())
    			+ '\n' + "Subtotal: 	" + 
    			formatDecimalFormat.format(myOrder.getSubTotal())
    			+ '\n' + "Shipping: 	" +
    			formatDecimalFormat.format(myOrder.getShipping())
    			+ '\n' + "Tax: 	" +
    			formatDecimalFormat.format(myOrder.getTax()) + '\n'+ 
    			"Total:	" +	formatDecimalFormat.format(myOrder.getTotal())+'\n' +'\n' +
    			"Total Quantity To-Date: " + (myOrder.getNumberOfOrders()) + '\n'
    			+ "Grand Total To-Date: " + formatDecimalFormat.format(myOrder.getGrandTotal())+ '\n';
    			outputTextArea.append(outputString);
    		}
    		else if(discountChar == 'b')
    		{			
    			outputString = "Name of the Book: " + nameString + '\n' + 
    			"Quantity Ordered: " + formatNumberFormat.format(myOrder.getQuantity())
    			+ '\n' + "Price: 	" + formatDecimalFormat.format(myOrder.getPrice())
    			+ '\n' + "Discount Percentage: 20% " +
    			formatDecimalFormat.format(myOrder.getDiscount())
    			+ '\n' + "Subtotal: 	" + 
    			formatDecimalFormat.format(myOrder.getSubTotal())
    			+ '\n' + "Shipping: 	" +
    			formatDecimalFormat.format(myOrder.getShipping())
    			+ '\n' + "Tax: 	" +
    			formatDecimalFormat.format(myOrder.getTax()) + '\n'+ 
    			"Total:	" +	formatDecimalFormat.format(myOrder.getTotal())+'\n' +'\n' +
    			"Total Quantity To-Date: " + (myOrder.getNumberOfOrders()) + '\n'
    			+ "Grand Total To-Date: " + formatDecimalFormat.format(myOrder.getGrandTotal())+ '\n';
    			outputTextArea.append(outputString);
    		}
     
    		else if(discountChar == 'c')
    		{			
    			outputString = "Name of the Book: " + nameString + '\n' + 
    			"Quantity Ordered: " + formatNumberFormat.format(myOrder.getQuantity())
    			+ '\n' + "Price: 	" + formatDecimalFormat.format(myOrder.getPrice())
    			+ '\n' + "Discount Percentage: 20% " +
    			formatDecimalFormat.format(myOrder.getDiscount())
    			+ '\n' + "Subtotal: 	" + 
    			formatDecimalFormat.format(myOrder.getSubTotal())
    			+ '\n' + "Shipping: 	" +
    			formatDecimalFormat.format(myOrder.getShipping())
    			+ '\n' + "Tax: 	" +
    			formatDecimalFormat.format(myOrder.getTax()) + '\n'+ 
    			"Total:	" +	formatDecimalFormat.format(myOrder.getTotal())+'\n' +'\n' +
    			"Total Quantity To-Date: " + (myOrder.getNumberOfOrders()) + '\n'
    			+ "Grand Total To-Date: " + formatDecimalFormat.format(myOrder.getGrandTotal())+ '\n';
    			outputTextArea.append(outputString);
    		}
    	}
    	// }
     
    	public void  clear()
    	{
    //		clear existing text from text fields and request cursor to top
    		bookNameTextField.setText("");
    		quantityTextField.setText("");
    		discountInvisibleRadioButton.setSelected(true);
    		priceTextField.setText("");
    		bookNameTextField.requestFocus();
    		priceTextField.setEditable(false);
    		calculateButton.setEnabled(false);  
    	}    
    }


    if you have any questions about this let me know.

  7. The Following User Says Thank You to Engineeringserver For This Useful Post:

    javapenguin (May 7th, 2011)

  8. #7
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 10 Times in 4 Posts

    Default Re: Please HELP!!! Need help with Output

    I was almost there crazydeo but ES beat me too it!

    Good work Engineeringserver!! - That is working perfectly for me

  9. #8
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Please HELP!!! Need help with Output

    Quote Originally Posted by Flash View Post
    I was almost there crazydeo but ES beat me too it!

    Good work Engineeringserver!! - That is working perfectly for me
    Your welcome

  10. #9
    Junior Member
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please HELP!!! Need help with Output

    Thank you FLASH and Engineeringserver!!!! You both seriously saved me a lot of headache!!

  11. #10
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 10 Times in 4 Posts

    Default Re: Please HELP!!! Need help with Output

    Thank you FLASH and Engineeringserver!!!! You both seriously saved me a lot of headache!!
    Glad you got your answer here!!
    Please Mark this thread as solved
    Last edited by Flash; May 14th, 2008 at 11:07 AM.

Similar Threads

  1. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM