Programming in Visual Basic (invoice class) Create a class called Invoice that a
ID: 3572195 • Letter: P
Question
Programming in Visual Basic
(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 Integer) and a price per item (type Integer). Your class should have a constructor that initializes the four instance variables. Provide a property for each instance variable. 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. Use validation in the properties for these instance variables to ensure that they remain positive. In addition, provide a method named DisplayInvoiceAmount that calculates and displays the invoice amount (that is, mulitplies the quantity by the price per item). Write an app that demonstrats class Invoices capablities.
Explanation / Answer
class Invoice
{
String number;
String description;
int quantity;
double price;
Invoice(String n, String d, int q, double p)
{
number = n;
description = d;
quantity = q;
price = p;
}
void setNumber(String n)
{
number = n;
}
void setDescription(String d)
{
description = d;
}
void setQuantity(int q)
{
quantity = q;
if(q < 0)
quantity = 0;
}
void setPrice(double p)
{
price = p;
if(p < 0)
price = 0.0;
}
String getNumber()
{
return number;
}
String getDescription()
{
return description;
}
int getQuantity()
{
return quantity;
}
double getPrice()
{
return price;
}
double getInvoiceAmount()
{
return quantity*price;
}
}
class InvoiceTest{
public static void main(String[] args)
{
Invoice object1 = new Invoice(“27”, “Blueberry parfait”, 3, 9.9);
Invoice object2 = new Invoice(“12”, “Rainbow Fraffucino”, 5, 6.6);
Invoice object3 = new Invoice(“94”, “Strawberry shortcake”, 7, 29.8);
System.out.println(“This is an Invoice for the Item(s) Sold:” + “ ”);
System.out.println(object1.getNumber() + “ ” + object1.getDescription() + “ ” + object1.getQuantity() + “ ” + object1.getPrice() + “ ” + object1.getInvoiceAmount());
System.out.println(object2.getNumber() + “ ” + object2.getDescription() + “ ” + object2.getQuantity() + “ ” + object2.getPrice() + “ ” + object2.getInvoiceAmount());
System.out.println(object3.getNumber() + “ ” + object3.getDescription() + “ ” + object3.getQuantity() + “ ” + object3.getPrice() + “ ” + object3.getInvoiceAmount());
System.out.println(“ ”);
double total = object1.getInvoiceAmount() + object2.getInvoiceAmount() + object3.getInvoiceAmount();
System.out.println(“TOTAL: “+ total + “ ”);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.