rcham68
asked on
Class Invoice using LINQ
Can anyone help I am having a problem getting the program to display the way I need it to.
This is the way it is displaying
Invoices sorted by part description :
Electric Sander
Hammer
Jig saw
Lawn Mower
Power Saw
Screwdriver
Sledge Hammer
Wrench
Invoices sorted by unit price :
6.99
7.5
11
11.99
21.5
57.98
79.5
99.99
And this is the way I need it to display
Invoices sorted by part description
83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50
Invoices sorted by unit price
68 Screwdriver 106 $6.99
3 Wrench 34 $7.50
56 Jig saw 21 $11.00
77 Hammer 76 $11.99
7 Sledge hammer 11 $21.50
83 Electric sander 7 $57.98
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
This is the way it is displaying
Invoices sorted by part description :
Electric Sander
Hammer
Jig saw
Lawn Mower
Power Saw
Screwdriver
Sledge Hammer
Wrench
Invoices sorted by unit price :
6.99
7.5
11
11.99
21.5
57.98
79.5
99.99
And this is the way I need it to display
Invoices sorted by part description
83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50
Invoices sorted by unit price
68 Screwdriver 106 $6.99
3 Wrench 34 $7.50
56 Jig saw 21 $11.00
77 Hammer 76 $11.99
7 Sledge hammer 11 $21.50
83 Electric sander 7 $57.98
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
// 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);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class InvoiceTest
{
public static void Main(string[] args)
{
int[] partNum = { 83, 24, 7, 77, 39, 68, 56, 3 };
string[] partDescription = { "Electric Sander", "Power Saw", "Sledge Hammer", "Hammer", "Lawn Mower", "Screwdriver", "Jig saw", "Wrench" };
int[] quantity = { 7, 18, 11, 76, 3, 106, 21, 34 };
double[] unitPrice = { 57.98, 99.99, 21.50, 11.99, 79.50, 6.99, 11.00, 7.50 };
//Console.WriteLine(partNum.ToString() + "Original: {0}" , partNum );
var sortPartDescription =
from value in partDescription
orderby value ascending
select value;
Display( sortPartDescription, string.Format("Invoices sorted by part description " ));
Console.WriteLine();
var sortUnitPrice =
from value in unitPrice
orderby value ascending
select value;
Display(sortUnitPrice, string.Format("Invoices sorted by unit price "));
}
public static void Display<T>(IEnumerable<T> results, string header)
{
Console.WriteLine("{0}: ", header);
foreach (T element in results)
Console.WriteLine(element);
Console.WriteLine();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And of course after sorting, display like this :
Display(sortPartDescriptio n,"Sorted by description");
Display(sortPartDescriptio
Although you have a class called Invoice, you are not using it. Are you aware of this?
So first of all, you should create a List of Invoice like this for example :
Open in new window
and after this, you can sort by Description :
Open in new window
and then Display code should be like this :
Open in new window
I hope you get the idea.