Solved

JAVA

Posted on 2011-03-14
17
1,692 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
  • 5
  • 5
  • 5
  • +1
17 Comments
 
LVL 18

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now