“PartNumber cannot be resolved to a variable” line 24 “PartDescription cannot be
ID: 3880079 • Letter: #
Question
“PartNumber cannot be resolved to a variable” line 24 “PartDescription cannot be resolves to a variable” Line 25 “PartQuantity cannot be resolved to a variable” Line 26 “PricePerItem cannot be resolved to a variable” Line 27Copy of my question. 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. Write a test app named InvoiceTest that demonstrates class Invoice’s capabilities. 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. Write a test app named InvoiceTest that demonstrates class Invoice’s capabilities.
17 public class Invoice//Public class Invoice begins 18 19 private String PartNumber: //Instance variable with type String for part numbers 20 private String PartDescription://Instance variable vith type String tor part deacriptions 21 private int PartQuantity://Inatance variable with type int for part quantities 22 private double PricePerItem://Inatance variable with type double for prices per itema 23 24 25 6 publie Invoice (String PartNumber, String PartDescription, int PartQuantity, double PricePerItem )//Constructor receivir 27 2this.PartNumber -PartNumber://PartNumbez ia assigned to Nun this.PartDeacription PartDescription:l/PartDescription is assigned to Det 30 31 32 r Partouantity
Explanation / Answer
import java.util.*;
class Invoice
{
private String PartNumber;
private String PartDescription;
private int PartQuantity;
private double PricePerItem;
public Invoice(String PartNumber,String PartDescription,int PartQuantity,double PricePerItem)
{
this.PartNumber = PartNumber;
this.PartDescription = PartDescription;
if(PartQuantity < 0)
this.PartQuantity = 0;
else
this.PartQuantity = PartQuantity;
if(PricePerItem < 0)
this.PricePerItem = 0;
else
this.PricePerItem = PricePerItem;
}
public double getInvoiceAmount()
{
double amount;
amount = PartQuantity * PricePerItem;
return amount;
}
public void setPartNumber(String PartNumber)
{
this.PartNumber = PartNumber;
}
public String getPartNumber()
{
return PartNumber;
}
public void setPartDescription(String PartDescription)
{
this.PartDescription = PartDescription;
}
public String getPartDescription()
{
return PartDescription;
}
public void setPartQuantity(int PartQuantity)
{
this.PartQuantity = PartQuantity;
}
public int getPartQuantity()
{
return PartQuantity;
}
public void setPricePerItem(double PricePerItem)
{
this.PricePerItem = PricePerItem;
}
public double getPricePerItem()
{
return PricePerItem;
}
}
class InvoiceTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
//declare variables
System.out.println("Enter Part Number : ");
String PartNumber = input.nextLine();
System.out.println("Enter Part Description : ");
String PartDescription = input.nextLine();
System.out.println("Enter Part Quantity : ");
int PartQuantity = input.nextInt();
System.out.println("Enter Price Per Item : ");
double PricePerItem = input.nextDouble();
//use variables in object initialization
Invoice invoice1 = new Invoice(PartNumber,PartDescription,PartQuantity,PricePerItem);
System.out.println();
System.out.println("Part Number : "+ invoice1.getPartNumber());
System.out.println("Part Description : "+ invoice1.getPartDescription());
System.out.println("Part Quantity : "+ invoice1.getPartQuantity());
System.out.println("Price Per Item : "+ invoice1.getPricePerItem());
}
}
output:
Enter Part Number : p1006
Enter Part Description : pliers
Enter Part Quantity : 2
Enter Price Per Item :34.6
Part Number : p1006
Part Description : pliers
Part Quantity : 2
Price Per Item : 34.6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.