9.3 (QueryinganArrayofInvoiceObjects)UsetheclassInvoiceprovidedintheex09_03fold-
ID: 3638135 • Letter: 9
Question
9.3 (QueryinganArrayofInvoiceObjects)UsetheclassInvoiceprovidedintheex09_03fold-erwiththischapter’sexamplestocreateanarrayofInvoiceobjects.Usethesampledatashownin
Fig.9.8.ClassInvoiceincludesfourproperties—aPartNumber(typeint),aPartDescription(type
string),aQuantityoftheitembeingpurchased(typeint)andaPrice(typedecimal).Performthe
followingqueriesonthearrayofInvoiceobjectsanddisplaystheresults:
a)UseLINQtosorttheInvoiceobjectsbyPartDescription.
b)UseLINQtosorttheInvoiceobjectsbyPrice.
c)UseLINQtoselectthePartDescriptionandQuantityandsorttheresultsbyQuantity.
d)UseLINQtoselectfromeachInvoicethePartDescriptionandthevalueoftheIn-
voice(i.e.,Quantity*Price).NamethecalculatedcolumnInvoiceTotal.Orderthe
resultsbyInvoicevalue.[Hint:UselettostoretheresultofQuantity*Priceinanew
rangevariabletotal.]
e)UsingtheresultsoftheLINQqueryinPartd,selecttheInvoiceTotalsintherange
$200to$500.
Explanation / Answer
Use the Invoice Class provided to create an array of Invoice Objects. Class Invoice includes four properties- a PartNumber (type integer), a PartDescription (type String), a Quantity of the item being purchased (type Integer), and a Price (type Decimal). Write a Console Application that performs the following queries on the array of Invoice objects and displays the results:
a. Use LINQ to sort the Invoice Objects by PartNumber
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 PartDescrition and the value of the Invoice (i.e. Quantity * Price). Name the calculated column InvoiceTotal. Order the results by InvoiceTotal.
e. Useing the results of the LINQ query in Pard d, select the InvoiceTotals in the range of $200 to $500
I am pretty sure I have parts a, b, & c completed correctly. Or at least the provide the desired results. Part d I figured out how to multiply the 2 fields but can't create a new column for the result or figure out how to show the PartDescription with the multiplied result. Part c I think I can figure out when I get part d. I already coded what I think will work for part c.
Below are the 2 Objects the Invoice was given and not to be changed & the LINQ is the one I have been working on.
Thank you all for your help I appreciate it.
Module LINQ
Sub Main()
' array of invoices
Dim invoices As Invoice() = { _
New Invoice("83", "Electric Sander", "7", 59.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
Dim Desc = From e In invoices Order By e.PartDescription Ascending
Display(Desc, "By Part Description Ascending")
'Part b Order by Price
Dim Price = From e In invoices Order By e.Price Ascending
Display(Price, "By Price Ascending")
'Part c just Description & quantity, sorted by quantity
Dim DescQuant = From invoice In invoices Select invoice.PartDescription, invoice.Quantity
Dim SortDescQuant = From invoice In DescQuant Order By invoice.Quantity Ascending
Display(SortDescQuant, "Part Description and Quantity, sort by Quantity")
'Part d add InvoiceTotal and multiply quantity & price
Invoice.add(InvoiceTotal)
Dim total = From invoice In invoices Select invoice.PartDescription, invoice.Quantity, invoice.Price
Dim total1 = From invoice In total Where (invoice.Quantity * invoice.Price) = invoice.InvoiceTotal
Display(total1, "total")
'Part e select InvoiceTotal between $200 & $500
Dim between = From invoice In total1 Where invoice.InvoiceTotal >= 200 AndAlso invoice.InvoiceTotal <= 500 Select invoice
Display(between, "Total Prices Between $200 & $500")
End Sub
Sub Display(Of T)(ByVal results As IEnumerable(Of T), ByVal header As String)
Console.WriteLine("{0}:", header)
For Each element As T In results
Console.WriteLine(" {0}", element)
Next
Console.WriteLine()
End Sub
End Module
' Exercise 9.3 Solution: Invoice.vb
' Invoice class.
Public Class Invoice
' declare variables for Invoice object
Private partNumberValue As Integer
Private partDescriptionValue As String
Private quantityValue As Integer
Private priceValue As Decimal
' four-argument constructor
Public Sub New(ByVal part As Integer, ByVal description As String, _
ByVal count As Integer, ByVal pricePerItem As Decimal)
PartNumber = part
PartDescription = description
Quantity = count
Price = pricePerItem
End Sub ' New
' property for partNumberValue; no validation necessary
Public Property PartNumber() As Integer
Get
Return partNumberValue
End Get
Set(ByVal value As Integer)
partNumberValue = value
End Set
End Property ' PartNumber
' property for partDescriptionValue; no validation necessary
Public Property PartDescription() As String
Get
Return partDescriptionValue
End Get
Set(ByVal value As String)
partDescriptionValue = value
End Set
End Property ' PartDescription
' property for quantityValue; ensures value is positive
Public Property Quantity() As Integer
Get
Return quantityValue
End Get
Set(ByVal value As Integer)
If value > 0 Then ' determine whether quantity is positive
quantityValue = value ' valid quantity assigned
End If
End Set
End Property ' Quantity
' property for pricePerItemValue; ensures value is positive
Public Property Price() As Decimal
Get
Return priceValue
End Get
Set(ByVal value As Decimal)
If value > 0 Then ' determine whether price is positive
priceValue = value ' valid price assigned
End If
End Set
End Property ' Price
' return String containing the fields in the Invoice in a nice format
Public Overrides Function ToString() As String
' 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)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.