Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Invoice that a hardware store might use to represent an in

ID: 3887506 • Letter: C

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 private 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 (decimal). Your class should have a constructor that initializes the four values. - Provide a property with a get and set accessor for each variables. For the Quantity and PricePerItem properties, if the value passed to the set accessor is negative, the value of the instance variable should be left unchanged. - Also, 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 decimal value. - Write a test class named InvoiceTest that demonstrates class Invoice’s following capabilities: * create an instance by calling its constructor; * call the method GetInvoiceAmount * set a new value to each field via its property; * get the new value of each field via its property;

Explanation / Answer

using System.IO;
using System;


public class Invoice
{
string partNumber, partName;
string partDescription;
int quantity;
double price;

public string PartNumber
{
get { return partNumber; }
set { partNumber = value; }
}

public string PartName
{
get { return partName; }
set { partName = value; }
}

public string PartDescription
{
get { return partDescription; }
set { partDescription = value; }
}

public int Quantity
{
get { return quantity; }
set { if (value > 0) {quantity = value;} }
}

public double Price
{
get { return price; }
set
{
if (value > 0)
price = value;
}
}

public Invoice(string partNumber, string partName, string partDescription, int quantity, double price)
{
PartName = partName;
PartNumber = partNumber;
PartDescription = partDescription;
Quantity = quantity;
Price = price;
}

public double GetInvoiceAmount ()
{
return quantity * price;
}
}

class InvoiceTest
{
static void Main()
{
Invoice obj1 = new Invoice("P1", "AAA", "Fan", 5, 120.5);
double cost = obj1.GetInvoiceAmount ();
Console.WriteLine("Part {0}, Name {1}, Desc {2}, Quantity {3}, Price Each {4:C}, Total Cost {5:C}", obj1.PartNumber, obj1.PartName, obj1.PartDescription, obj1.Quantity, obj1.Price, cost);
Invoice obj2 = new Invoice("P2", "BBB", "Fan Fly", 2, 20.5);
double cost2 = obj2.GetInvoiceAmount ();
Console.WriteLine("Part {0}, Name {1}, Desc {2}, Quantity {3}, Price Each {4:C}, Total Cost {5:C}", obj2.PartNumber, obj2.PartName, obj2.PartDescription, obj2.Quantity, obj2.Price, cost2);
obj2.PartNumber = "P3";
obj2.PartName = "CCC";
obj2.PartDescription = "Fan Part2";
obj2.Quantity = 6;
obj2.Price = 2.2;
Console.WriteLine("Part {0}, Name {1}, Desc {2}, Quantity {3}, Price Each {4:C}, Total Cost {5:C}", obj2.PartNumber, obj2.PartName, obj2.PartDescription, obj2.Quantity, obj2.Price, cost2);
obj2.Quantity = -6;
obj2.Price = -2.2;
Console.WriteLine("Part {0}, Name {1}, Desc {2}, Quantity {3}, Price Each {4:C}, Total Cost {5:C}", obj2.PartNumber, obj2.PartName, obj2.PartDescription, obj2.Quantity, obj2.Price, cost2);
Console.ReadKey();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote