Solved

JAVA

Posted on 2011-03-14
17
1,941 Views
Last Modified: 2012-05-11
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
Comment
Question by:anshealth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 5
  • +1
17 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35132138
Use

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

just use 0 instead of 0.0   anywhere
0
 

Author Comment

by:anshealth
ID: 35132152
I can't the specs are:

If the price per item is not positive, it should be set to 0.0.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35132312
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 47

Expert Comment

by:for_yan
ID: 35132371
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35132434
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
 

Author Comment

by:anshealth
ID: 35132462
What does this error mean:

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

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35132505
>>What does this error mean:

You need to recompile
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35132525
FYI
0 when assigned to a variable of type float or double automatically gives 0.0
0
 

Author Comment

by:anshealth
ID: 35132600
CEHJ I debugged it again and I still got the same error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35132614
Please post current code
0
 

Author Comment

by:anshealth
ID: 35132695
@ CEHJ  Invoice.java
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35132722
That doesn't look any different to the original ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35132763
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35132844
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35132875


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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35132917
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
 

Author Closing Comment

by:anshealth
ID: 35137761
Thank you all for the help
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question