[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2297
  • Last Modified:

JAVA

I'm working on coding a project here are the directions:

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 (type 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 zero. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates the class Invoice's capabilities.

Can anyone tell me what I'm doing wrong that it wont run? Also, how would I do this part of the code: returns the amount as a double value. If the quantity is not positive, it should be set to zero. If the price per item is not positive, it should be set to 0.0.  Invoice.java
0
anshealth
Asked:
anshealth
  • 5
  • 5
  • 5
  • +1
1 Solution
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Use

   prices = ( price < 0 ) ? 0 : price;

just use 0 instead of 0.0   anywhere
0
 
anshealthAuthor Commented:
I can't the specs are:

If the price per item is not positive, it should be set to 0.0.
0
 
for_yanCommented:
Well, you cvode does not compile in the first place - you have constructor Invoice(...)
right inside the main method - I guess you forgot some braces and some other lines as well
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
for_yanCommented:
You should put "}" before "String number;"
you should change setPrices( price ); to setPrices( prices );

You should create method "getPricePerItem()"

And most importantly, I guess, you need to split the "input" line which you get from
the Scanner and place parts of it into constructor.

This is you code with some corrections(mentioned above; with one compilateion mistake-
still no method getPricePerItem())

You want to get to the rest yourself?

import java.util.Scanner; //imports scanner class to read data

/* Created by: Elisa Alberico Creation date: 3/09/11 */


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
		Scanner input = new Scanner( System.in);

    }   

		//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",
         "invoice", "part number", getNumber(), getDescription(), "quantity", getQuantity(), "price per item", getPrices() );
   }

   //method calculates invoice amount and returns double as a value
   public double getInvoiceAmount()
   {
      return getQuantity() * getPricePerItem(); // calculate total cost
   } // end method getPaymentAmount
} // end class Invoice



}
}

Open in new window

0
 
CEHJCommented:
Your ctor should normally use this pattern:
//constructor
    public Invoice(String number, String description, int count, double price) {
        this.number = number;
        this.description = description;
        this.count = count;
        this.price = price;
    }

Open in new window

0
 
anshealthAuthor Commented:
What does this error mean:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

      at Invoice.Invoice.main(Invoice.java:9)

0
 
CEHJCommented:
>>What does this error mean:

You need to recompile
0
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
FYI
0 when assigned to a variable of type float or double automatically gives 0.0
0
 
anshealthAuthor Commented:
CEHJ I debugged it again and I still got the same error
0
 
CEHJCommented:
Please post current code
0
 
anshealthAuthor Commented:
@ CEHJ  Invoice.java
0
 
CEHJCommented:
That doesn't look any different to the original ...
0
 
CEHJCommented:
You need something more like:
import java.util.Scanner; //imports scanner class to read data


/* Created by: Elisa Alberico Creation date: 3/09/11 */
public class Invoice {
    //instance variables
    String number;
    String description;
    int count;
    double prices;

    //constructor
    public Invoice(String number, String description, int count, double prices) {
        this.number = number;
        this.description = description;
        this.count = count;
        this.prices = prices;
    }

    //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
        Scanner input = new Scanner(System.in);
    }

    //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 count
    public void setCount(int count) {
        count = (count < 0) ? 0 : count;
    }

    // get part count
    public int getCount() {
        return count;
    }

    // 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",
            "invoice", "part number", getNumber(), getDescription(),
            "count", getCount(), "price per item", getPrices());
    }

    //method calculates invoice amount and returns double as a value
    public double getInvoiceAmount() {
        return getCount() * getPrices(); // calculate total cost
    } // end method getPaymentAmount
} // end class Invoice

Open in new window

0
 
for_yanCommented:
This would workk without mistakes in the input:

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, 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",
         "invoice", "part number", getNumber(), getDescription(), "quantity", getQuantity(), "price per item", getPrices() );
   }

   //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

Open in new window

0
 
for_yanCommented:


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",
         "invoice", "part number", getNumber(), getDescription(), "quantity", getQuantity(), "price per item", getPrices() );
   }

   //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

Open in new window



results:

 Enter item number, description, quantity, price -  all separated by commas: 
025,book,5,15
invoice: 
part number: 025 (book) 
quantity: 5 
price per item: $15.00

Process finished with exit code 0

Open in new window

0
 
for_yanCommented:
This one with total cost:

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

Open in new window


Result:

Enter item number, description, quantity, price -  all separated by commas: 
034,book,15,2
invoice: 
part number: 034 (book) 
quantity: 15 
price per item: $2.00 
Total: $30.00

Open in new window

0
 
anshealthAuthor Commented:
Thank you all for the help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now