NEEDED IN ACTUAL C# TO COPY please!! Use the class below to create an array of I
ID: 3686796 • Letter: N
Question
NEEDED IN ACTUAL C# TO COPY please!!
Use the class below to create an array of Invoice objects. Use the sample data shown in Fig. 9.8. Class Invoice includes four properties, a PartNumber (type int), a PartDescription (type string), a Quantity of the item being purchased (type int) and a Price (type decimal). Perform the following queries on the array of Invoice objects and display the results:
a) Use LINQ to sort the Invoice objects by PartDescription.
b) Use LINQ to sort the Invoice objects by Price.
c) Use LINQ to select the PartDescription and Quantity and sort the results by Quantity.
d) Use LINQ to select from each Invoice the PartDescription and the value of the Invoice (i.e., Quantity * Price).
Name the calculated column InvoiceTotal. Order the results by Invoice value. [Hint: Use let to store the result of Quantity * Price in a new range variable total]. e) Using the results of the LINQ query in part d, select the InvoiceTotals in the range $200 to $500
Class Invoice:
// Invoice.cs
// Invoice for a hardware company
using System;
public class Invoice
{
string partNum;
string partDescription;
int quantity;
decimal unitPrice;
public Invoice(string pNum, string pDesciption, int q, decimal uPrice)
{
PartNum = pNum;
PartDescription = pDesciption;
Quantity = q;
UnitPrice = uPrice;
}
public string PartNum
{
get
{
return partNum;
} // end get
set
{
partNum = value;
} // end set
} // end
public string PartDescription
{
get
{
return partDescription;
} // end get
set
{
partDescription = value;
} // end set
} // end
public int Quantity
{
get
{
return quantity;
} // end get
set
{
if (value >= 0)
quantity = value;
} // end set
} // end
public decimal UnitPrice
{
get
{
return unitPrice;
} // end get
set
{
if (value >= 0M)
unitPrice = value;
} // end set
} // end
public decimal GetInvoiceAmount()
{
decimal amt = Quantity * UnitPrice;
return amt;
}
public override string ToString()
{
return string.Format("{0,-10}{1,-20}{2,-10}{3,10:C}", PartNum,
PartDescription, Quantity, UnitPrice);
}
}
Part number Part description Quantity Price Electric sander Power saw Sledge hammer Hammer Lawn mower Screwdriver Jig saw Wrench 57.98 99.99 21.50 11.99 79. 50 6.99 11.00 7.50 83 24 18 76 39 68 56 106 21 34 Fig. 9.8 | Sample data for Exercise 9.3Explanation / Answer
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
static class LINQ
{
public static void Main()
{
// array of invoices
Invoice[] invoices = {
new Invoice("83", "Electric Sander", "7", 57.98),
new Invoice("24", "Power Saw", "18", 99.99),
new Invoice("7", "Sledge Hammer", "11", 21.5),
new Invoice("77", "Hammer", "76", 11.99),
new Invoice("39", "Lawn Mower", "3", 79.5),
new Invoice("68", "Screwdriver", "106", 6.99),
new Invoice("56", "Jig Saw", "21", 11.0),
new Invoice("3", "Wrench", "34", 7.5)
};
//Display(invoices, "Original Array")
//Part a Order by PartDescription
dynamic Desc = from e in invoicesorderby e.PartDescription ascending;
Display(Desc, "By Part Description Ascending");
//Part b Order by Price
dynamic Price = from e in invoicesorderby e.Price ascending;
Display(Price, "By Price Ascending");
//Part c just Description & quantity, sorted by quantity
dynamic DescQuant = from invoice in invoicesinvoice.PartDescriptioninvoice.Quantity;
dynamic SortDescQuant = from invoice in DescQuantorderby invoice.Quantity ascending;
Display(SortDescQuant, "Part Description and Quantity, sort by Quantity");
//Part d add InvoiceTotal and multiply quantity & price
invoice.@add(InvoiceTotal);
dynamic total = from invoice in invoicesinvoice.PartDescriptioninvoice.Quantityinvoice.Price;
dynamic total1 = from invoice in totalwhere (invoice.Quantity * invoice.Price) == invoice.InvoiceTotal;
Display(total1, "total");
//Part e select InvoiceTotal between $200 & $500
dynamic between = from invoice in total1where invoice.InvoiceTotal >= 200 && invoice.InvoiceTotal <= 500invoice;
Display(between, "Total Prices Between $200 & $500");
Use the class below to create an array of Invoice objects
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Invoice
{
// declare variables for Invoice object
private int partNumberValue;
private string partDescriptionValue;
private int quantityValue;
private decimal priceValue;
// four-argument constructor
public Invoice(int part, string description, int count, decimal pricePerItem)
{
PartNumber = part;
PartDescription = description;
Quantity = count;
Price = pricePerItem;
}
// New
// property for partNumberValue; no validation necessary
public int PartNumber {
get { return partNumberValue; }
set { partNumberValue = value; }
}
// PartNumber
// property for partDescriptionValue; no validation necessary
public string PartDescription {
get { return partDescriptionValue; }
set { partDescriptionValue = value; }
}
// PartDescription
// property for quantityValue; ensures value is positive
public int Quantity {
get { return quantityValue; }
set {
// determine whether quantity is positive
if (value > 0) {
quantityValue = value;
// valid quantity assigned
}
}
}
// Quantity
// property for pricePerItemValue; ensures value is positive
public decimal Price {
get { return priceValue; }
set {
// determine whether price is positive
if (value > 0) {
priceValue = value;
// valid price assigned
}
}
}
// Price
// return String containing the fields in the Invoice in a nice format
public override string ToString()
{
// left justify each field, and give large enough spaces so
// all the columns line up
return string.Format("{0,-5} {1,-20} {2,-5} {3,6:C}", PartNumber, PartDescription, Quantity, Price);
}
// ToString
}
// Invoice
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.