Solved

Class Invoice using LINQ

Posted on 2010-11-23
3
1,170 Views
Last Modified: 2012-05-10
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

// 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);
    }
}

Open in new window

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();
    }
}

Open in new window

0
Comment
Question by:rcham68
  • 2
3 Comments
 
LVL 5

Expert Comment

by:buraksarica
Comment Utility
Hi there,

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 :

//assuming all of your arrays have same number of elements.. in a real world scenario, you produce this list from a db connection or smthg like that...
List<Invoice> invs = new List<Invoice>();
for(int i=0;i<partNum.Length;i++)
{
     invs.Add(new Invoice(partNum[i],partDescription[i],quantity[i],unitPrice[i]));
}

Open in new window


and after this, you can sort by Description :

var sortPartDescription =
            from invoice in invs
            orderby invoice.PartDescription ascending
            select invoice;

Open in new window


and then Display code should be like this :

public static void Display(IEnumerable<Invoice> invoices, string header)
    {
        Console.WriteLine("{0}: ", header);
        foreach (Invoice element in invoices)
            Console.WriteLine("{0} \t {1}", element.partNum,element.PartDescription);

        Console.WriteLine();
    }

Open in new window



I hope you get the idea.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi rcham68;

I have made changes to this part of your posted code.

Fernando
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 };

	    decimal[] unitPrice = { 57.98m, 99.99m, 21.50m, 11.99m, 79.50m, 6.99m, 11.00m, 7.50m };

	

	    List<Invoice> lstInvoices = new List<Invoice>();

	

			// Create an invoice of all the data above into one object

	    for (int i = 0; i < partNum.Length; i++)

	    {

	        Invoice inv = new Invoice(partNum[i].ToString(), partDescription[i], quantity[i], unitPrice[i]);

	        lstInvoices.Add(inv);

	    }

	

	    //Console.WriteLine(partNum.ToString() + "Original: {0}" , partNum  );

			

			// Now query the lstInvoices

	    var sortPartDescription =

	        from value in lstInvoices

	        orderby value.PartDescription ascending

	        select value;

	

	    Display(sortPartDescription, string.Format("Invoices sorted by part description "));

	

	    Console.WriteLine();

	

	    var sortUnitPrice =

	        from value in lstInvoices

	        orderby value.UnitPrice ascending

	        select value;

	

	    Display(sortUnitPrice, string.Format("Invoices sorted by unit price "));

	

	    Console.ReadLine();

	}

	

	public static void Display<T>(IEnumerable<T> results, string header)

	{

	    Console.WriteLine("{0}: ", header);

	    foreach (T element in results)

	        Console.WriteLine(element.ToString());

	

	    Console.WriteLine();

	}

}

Open in new window

0
 
LVL 5

Expert Comment

by:buraksarica
Comment Utility
And of course after sorting, display like this :

Display(sortPartDescription,"Sorted by description");
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What are the big features of MVC5? 4 50
Change to event 1 72
.NET Error 7 40
String manipulation 15 48
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now