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

In c#,Create a class called Invoice that a hardware store might use to represent

ID: 3887444 • Letter: I

Question

In c#,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 six pieces of information as private instance variables — a part number (type string), a part name (type string), a part description (type string), a part manufacturer (type string), a quantity of the item being purchased (type int) and a price per item (decimal).

- Provide a property with a get and set accessor for int and decimal 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.

- Provide an indexer with a get and set accessor for each string variable.

- 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 default constructor;

* assign the value of each instance variable via the indexers and properties;

* call the method GetInvoiceAmount;

* get the new value of each variable via its property or indexer;

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 { quantity = value; }
}

public double Price
{
get { return price; }
set
{
if (value < 0) 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);
Console.ReadKey();
}
}

Output:

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