Results 1 to 5 of 5
  1. #1
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Question Constructors. Invoice program

    (Invoice Class ) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store . An Invoice should include four pieces of information as instance variables -a part number (type String ), a part description (type String ), a quantity of the item being purchased (type int ) and a price per item (double ). Your class should have a constructor that initializes the four instance variables . Provide a set and a get method for each instance variable . In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value . If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Add a main method to the class that demonstrates class Invoice's capabilities by reading in valuies from stdin and prints to stdout the complete state of the object each time the state of the object is changed.

    Java Code:
    import java.util.Scanner; //imports scanner class to read data
    public class Invoice
    {
    	//main method required in every java program
    	public static void main( String[] args )
    	{
    		//creates a scanner and enables apps to read bytes of info typed in
            System.out.println(" Enter item number, description, quantity, price -  all separated by commas: ");
    		Scanner input = new Scanner( System.in);
    
            String str = input.next();
            String [] str1 = str.split(",");
    
    
    
           Invoice in =  new  Invoice(str1[0], str1[1], Integer.parseInt(str1[2]), Double.parseDouble(str1[3]) );
            System.out.println(in.toString());
    
        }
    		//instance variables
    		String number;
    		String description;
    		int quantity;
    		double prices;
    
    
    
    		//constructor
    	public Invoice( String pt, String dsc, int count, double prices )
       {
          number = pt;
          description = dsc;
          setQuantity( count );
          setPrices( prices );
       }
    
    	//set part number
    	public void setNumber( String pt )
       {
          number = pt;
       }
    	//get part number
       public String getNumber()
       {
          return number;
       }
    
       	//set part description
    	public void setDescription( String dsc )
       {
          number = dsc;
       }
    	//get part description
       public String getDescription()
       {
          return description;
       }
    
       // set part quantity
       public void setQuantity( int count )
       {
          quantity = ( count < 0 ) ? 0 : count;
       }
    
       // get part quantity
       public int getQuantity()
       {
          return quantity;
       }
    
       // set price per item
       public void setPrices( double price )
       {
          prices = ( price < 0.0 ) ? 0.0 : price;
       }
    
       // get price per item
       public double getPrices()
       {
          return prices;
       }
    
          // return String
       public String toString()
       {
          return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f \n%s: $%,.2f",
             "invoice", "part number", getNumber(), getDescription(), "quantity", getQuantity(), "price per item", getPrices(),  "Total", getInvoiceAmount() );
       }
    
       //method calculates invoice amount and returns double as a value
       public double getInvoiceAmount()
       {
          return getQuantity() * getPrices(); // calculate total cost
       } // end method getPaymentAmount
    } // end class Invoice
    Can anyone tell me whats wrong with this code?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructors. Invoice program

    What is it supposed to do that it is not doing?
    What attempts have you made to debug the code yourself?

    Try some print statements placed in key areas throughout the program to see if
    certain parts are working as expected.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Default Re: Constructors. Invoice program

    So I redid it differently. And Im still getting a lot of errors.

    Java Code:
    import java.util.Scanner;
    
    
    public class Invoice {
    
     private String partNumber;
     private String partDescription;
     private int Qty;
     private double itemPrice;
     
     public Invoice(String prtNum,String prtDesc,int qty,double iTpr){
      
      setPartNumber(prtNum);
      setPartDescription(prtDesc);
      setQty(qty);
      setItemPrice(iTpr);
       
       }
    
     public double getItemPrice() {
      return itemPrice;
      }
    
     public void setItemPrice(double itemPrice) {
      this.itemPrice = itemPrice;
      }
    
     public int getQty() {
      return Qty;
      }
    
     public void setQty(int qty) {
      Qty = qty;
      }
    
     public String getPartDescription() {
      return partDescription;
      }
    
     public void setPartDescription(String partDescription) {
      this.partDescription = partDescription;
      }
    
     public String getPartNumber() {
      return partNumber;
      }
    
     public void setPartNumber(String partNumber) {
      this.partNumber = partNumber;
      }
     
     public double getInvoiceAmount(int qty,double price){
      
      double value=qty*price;
      return value;
      }
    
     public static void main(String[] args) {
      Invoice invoice=new Invoice("","",1,10.0);
      // TODO Auto-generated method stub
      String partnum;
      String partdesc;
      String qty;
      String price;
      
      System.out.print("Enter Part Number: "); 
      partnum = input.nextString(); 
      display.setPartNumber(partnum);
      
      System.out.print("Enter Part Description: "); 
      partdesc = input.nextString();
      display.setPartDescription(partdesc); 
      
      System.out.print("Enter Item Price: "); 
      price = input.nextString();
      display.setItemPrice(price); 
      
      System.out.print("Enter Quantity: ");
      qty = input.nextString(); 
      display.setQty(qty); 
      
     
     String prtnm=invoice.getPartNumber();
     String prtdsc=invoice.getPartDescription();
     int q=invoice.getQty();
     double pr= invoice.getItemPrice();
     double amount=invoice.getInvoiceAmount(invoice.getQty(),invoice.getItemPrice());
     
     //JOptionPane.showMessageDialog(null,"Part Number : "+prtnm+"\nPart Description : "+prtdsc+"\nQuantity : "+q+"\nPrice : "+pr+"\nTotal Cost : "+amount);
      
    
     }
    
    }
    Attached Thumbnails Attached Thumbnails Constructors. Invoice program-error.png  

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Constructors. Invoice program

    Can you copy and paste the text of the error messages here so text can be copied to be included in the response.

    The compiler can not find the variable shown in the message.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Constructors. Invoice program

    The error message are telling you the problem. It say it cannot find the symbol.
    That means it can't find the variable that the caret is pointing to.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Invoice java program
    By cherians in forum New To Java
    Replies: 8
    Last Post: 10-10-2013, 04:24 AM
  2. Constructors not right?
    By nikki101 in forum New To Java
    Replies: 2
    Last Post: 05-26-2013, 08:21 AM
  3. invoice reciept
    By acelajines in forum NetBeans
    Replies: 0
    Last Post: 01-16-2012, 11:00 AM
  4. Need Help for coding invoice total
    By maxb in forum New To Java
    Replies: 3
    Last Post: 11-22-2008, 04:22 PM
  5. Help with Invoice app
    By callofthektulu in forum Java Applets
    Replies: 1
    Last Post: 08-08-2008, 04:17 AM

Tags for this Thread

Posting Permissions

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