Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,617 people online right now. Registration is fast and FREE... Join Now!




please help with classes and arrays

 
Reply to this topicStart new topic

please help with classes and arrays

dogz
16 May, 2007 - 04:45 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2007
Posts: 13


My Contributions
I have created a invoice class that asks the user to enter different information about a part, I have also created a loop which goes through three times. the problems I am having are listed below i have been working at this for ages and just cant see what i need to do for it. the program and driver program are listed below also.


oCreate an object eg if num = 0 this will create an object in the first cell of the array:
inv [num] = new Invoice();

oUse the dialog boxes and setters to capture the data as before but include the invoice number ie 1 to 3 as shown below in the dialog box. eg
inv[num].setPartNumber(partNo);

•Print the Invoice heading as before

•Then use another For loop using the getter methods to display the values of each instance variable and the new method getInvoiceAmount to display the total of each line.

•Add each line total and print the overall total.

• have the final dialog box show like below,

part description qty price total
nun01 nut 12 2 $24
bol01 bolt 12 3 $36
scw01 screw 24 1 $24
$84


CODE

/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
public class Invoice
{
    private String partNumber;
    private String description;
    private double quantity;
    private double price;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        
    public Invoice()
    {
        partNumber = "";
        description = "";
        quantity = 0;
        price = 0;
    }
      
    /**
     * The setPart method stores value in part field.
     * @param partNumber the value to store in part.
     */
    public void setPartNumber(String piece)
    {
        partNumber = piece;
    }  
        
    /**
     * The setDescription method stores value in part field.
     * @param desc the value to store in description.
     */
    public void setDescription(String desc)
    {
        description = desc;
    }
    
    /**
     * The setQuantity method stores value in part field.
     * @param quan the value to store in quantity.
     */
    public void setQuantity(double quan)
    {
         quantity = quan;
    }
    
    /**
     * The setPrice method stores value in part field.
     * @param amount the value to store in price.
     */
     public void setPrice(double amount)
     {
         price = amount;
     }
    
     /**
      * The getPart method returns value.
      * @return the value in the part field.
      */
     public String getPartNumber()
     {
         return partNumber;
     }
  
     /**
      * The getdescription method returns value.
      * @return the value in the description field.
      */
     public String getDescription()
     {
         return description;
     }
    
     /**
      * The getQuantity method returns value.
      * @return the value in the quantity field.
      */
     public double getQuantity()
     {
         return quantity;
     }
    
     /**
      * The getPrice method returns value.
      * @return the value in the price field.
      */
     public double getPrice()
     {
         return price;
     }
  
     public double getTotal()
     {
         return quantity * price;
     }
}


And now the Driver Program

CODE

import javax.swing.JOptionPane;

/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
public class TestInvoice
{
    
    public static void main(String[] args)
    {
        String input;
        double number;
        double partTotal;
        double invoiceTotal;
        Invoice myInvoice = new Invoice();
                
        for (int index = 0; index < 3; index++)
        {
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Number");
            myInvoice.setPartNumber(input);
        
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Description");
            myInvoice.setDescription(input);
        
            input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) + " Required");
            number = Double.parseDouble(input);
            myInvoice.setQuantity(number);
        
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Price");
            number = Double.parseDouble(input);
            myInvoice.setPrice(number);
            
            partTotal = myInvoice.getQuantity() * myInvoice.getPrice();
        }    
        
        invoiceTotal =
              
        
        JOptionPane.showMessageDialog(null, "Part " + "   " + "Description" + "   " + "Qty" + "   " + "Price\n" +  
        " $" + myInvoice.getPartNumber() +   " $" + myInvoice.getDescription() + " $" +  
        myInvoice.getQuantity() + " $" + myInvoice.getPrice() + "\n" + "            $" + partTotal);
        
    }
}

User is offlineProfile CardPM
+Quote Post

Ellie
RE: Please Help With Classes And Arrays
17 May, 2007 - 04:24 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
I've added a total field to your Invoice class:

CODE

/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
public class Invoice
{
    private String partNumber;
    private String description;
    private double quantity;
    private double price;
    private double total;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        
    public Invoice()
    {
        partNumber = "";
        description = "";
        quantity = 0;
        price = 0;
        total = 0.0;
    }
      
    /**
     * The setPart method stores value in part field.
     * @param partNumber the value to store in part.
     */
    public void setPartNumber(String piece)
    {
        partNumber = piece;
    }  
        
    /**
     * The setDescription method stores value in part field.
     * @param desc the value to store in description.
     */
    public void setDescription(String desc)
    {
        description = desc;
    }
    
    /**
     * The setQuantity method stores value in part field.
     * @param quan the value to store in quantity.
     */
    public void setQuantity(double quan)
    {
         quantity = quan;
    }
    
    /**
     * The setPrice method stores value in part field.
     * @param amount the value to store in price.
     */
     public void setPrice(double amount)
     {
         price = amount;
     }
    
     /**
      * The getPart method returns value.
      * @return the value in the part field.
      */
     public String getPartNumber()
     {
         return partNumber;
     }
  
     /**
      * The getdescription method returns value.
      * @return the value in the description field.
      */
     public String getDescription()
     {
         return description;
     }
    
     /**
      * The getQuantity method returns value.
      * @return the value in the quantity field.
      */
     public double getQuantity()
     {
         return quantity;
     }
    
     /**
      * The getPrice method returns value.
      * @return the value in the price field.
      */
     public double getPrice()
     {
         return price;
     }
  
     public double getTotal()
     {
         return quantity * price;
     }
     public void setTotal(double d)
     {
         total = d;
     }
}


And here's your driver class. You had no array declared to store your Invoice objects in as you created them, which was why you could only get one. Once the Invoice objects are added to an array, you can retrieve them again in another loop to make a string to display in your JOptionPane.

I think though that you should really construct your Invoice once you have all the variables and include them in your constructor rather than only by using set methods, but maybe that it how you have been told to do it, so I've left that as it is:

CODE

import javax.swing.JOptionPane;
import javax.swing.*;

/**
* This program demonstrates classes.
*
* @author (Simon Orazi)
* @version (11/05/07)
*/
public class TestInvoice
{
    
    public static void main(String[] args)
    {
        String input;
        double number;
        double partTotal = 0;
        double invoiceTotal;
        Invoice[] arr = new Invoice[3];
        Invoice myInvoice;
                
        for (int index = 0; index < 3; index++)
        {
            myInvoice = new Invoice();
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Number");
            myInvoice.setPartNumber(input);
        
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Description");
            myInvoice.setDescription(input);
        
            input = JOptionPane.showInputDialog("Please Enter Quantity of Part " + (index + 1) + " Required");
            number = Double.parseDouble(input);
            myInvoice.setQuantity(number);
        
            input = JOptionPane.showInputDialog("Please enter Part " + (index + 1) + " Price");
            number = Double.parseDouble(input);
            myInvoice.setPrice(number);
            
            partTotal = myInvoice.getQuantity() * myInvoice.getPrice();
            myInvoice.setTotal(partTotal);
            
            arr[index] = myInvoice;
        }    
        
        //invoiceTotal =
              
        String s = "";
        s+="Part " + "   " + "Description" + "   " + "Qty" + "   " + "Price\n";
           for (int j = 0; j < 3; j++)
           {
               s = s + arr[j].getPartNumber() + " " + arr[j].getDescription() + " " +
               arr[j].getQuantity() + " $" + arr[j].getPrice() + "\n";
           }
           JOptionPane.showMessageDialog(null, s);
        //JOptionPane.showMessageDialog(null, "Part " + "   " + "Description" + "   " + "Qty" + "   " + "Price\n" +  
       // " $" + myInvoice.getPartNumber() +   " $" + myInvoice.getDescription() + " $" +  
       // myInvoice.getQuantity() + " $" + myInvoice.getPrice() + "\n" + "            $" + partTotal);
        System.exit(0);
    }
}


Hope it helps icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month