Results 1 to 5 of 5
  1. #1
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Question [SOLVED] Please Help!!

    I've been working on this project all night :mad: 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!!!


    Java 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);
            
        //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);
            
        }    
    }
    Last edited by crazydeo; 12-08-2008 at 12:47 PM.

  2. #2
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Post the code for Calculation class or let me know if this is some api ?
    i am the future

  3. #3
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    Calculation Class


    Java Code:
    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()
    	{
    		switch(discountTypeInteger)
    		{
    			case 1:
    				calculateADiscount();
    				totalNumberOfOrdersInteger++;
    
    				break;
    			case 2:
    				calculateBDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    			case 3:
    				calculateCDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    		}
    		
    	}
    	
    	//calculation of fees
    	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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    
    		
    	}
    	//calculation of 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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    	}
    	//calculation of 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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    	}
    
    	
    	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;
    	}	
    }

  4. #4
    crazydeo is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    Fixed acouple of things in the Presentation Class as well.

    Java 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();
        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
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Java Code:
    package com.faaltu.PriceCalculation;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.text.DecimalFormat;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Presentation extends JFrame implements ActionListener, ItemListener 
    {
        // All the GUI objects
        JPanel mainPanel = 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(myOrder, 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(myOrder, 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(myOrder, 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( Calculation myCalcs, 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");
           
                    
         
            
            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);
            
        }    
    }


    Java Code:
    package com.faaltu.PriceCalculation;
    
    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();
    	}
    	
    	public void setQuantity(int quantityNewInteger)
    	{
    		//assign public variable to private
    		quantityInteger = quantityNewInteger;
    
    		}
    	public void setPrice(double priceNewDouble)
    	{
    		//assign public variable to private
    		priceDouble = priceNewDouble;
    
    		}
    	public void setDiscount(int discountTypeNewInteger)
    	{
    		//assign public variable to private
    		discountTypeInteger = discountTypeNewInteger;
    	}
    	public void calculate()
    	{
    		switch(discountTypeInteger)
    		{
    			case 1:
    				calculateADiscount();
    				totalNumberOfOrdersInteger++;
    
    				break;
    			case 2:
    				calculateBDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    			case 3:
    				calculateCDiscount();
    				totalNumberOfOrdersInteger++;
    				break;
    		}
    		
    	}
    	
    	//calculation of fees
    	public 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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    
    		
    	}
    	//calculation of fees
    	public 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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    	}
    	//calculation of fees
    	public 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;
    		//total number of orders processed
    		grandTotalDouble += totalDouble;
    	}
    
    	
    	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;
    	}	
    }

    Now it is working fine, :)
    but my friend whichever practice you are following is not fine :mad:
    i am the future

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •