public Form1() { InitializeComponent(); parcels = new List<Parcel>(); // List of parcels addresses = new List<Address>(); // List of addresses Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); addresses.Add(a1); addresses.Add(a2); addresses.Add(a3); addresses.Add(a4); parcels.Add(letter1); parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Sort by parcels cost in ascending order parcels.Sort(); Console.WriteLine("Parcels sorted by cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Sort by parcels descending zip parcels.Sort(new DescendingByZip()); Console.WriteLine("Parcels sorted by desc zip:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
public prog_2_main_form() { InitializeComponent(); // Test Data - made by Dr. Wright Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 //Addresses 5-8 updated by Megan Balcom Address a5 = new Address("Erik Davis", "2700 Potomac Mills Circle", "", "Woodbridge", "VA", 22192); //Test Address 5 Address a6 = new Address("Luke Skywalker", "100 Death Star Rd", "Apt 2", "Galaxy", "CA", 90001); //Test Address 6 Address a7 = new Address("Sarah Balcom", "6610 Shelburn Dr", "", "Crestwood", "KY", 40014); //Test Address 7 Address a8 = new Address("Katherine Freid", "956 Treasure Ct", "", "Fort Mill", "SC", 29708); //Test Address 8 address.Add(a1); //Populate list with addresses address.Add(a2); address.Add(a3); address.Add(a4); address.Add(a5); address.Add(a6); address.Add(a7); address.Add(a8); Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); parcels.Add(letter1); //populate list with parcels parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); List<Parcel> parcels; // List of test parcels parcels = new List<Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 12345); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 23456); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 34567); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 01234); // Test Address 4 Address a5 = new Address("Nicholas Goodridge", "1234 1st Street", "Apt #207", "Louisville", "KY", 45678); Address a6 = new Address("Derrek Baxter", "5678 2nd Street", "Cinncinatti", "OH", 56789); Address a7 = new Address("Eric Hoeweler", "0987 3rd Street", "Chicago", "IL", 67890); Address a8 = new Address("Lynnzee Kazee", "6543 4th Street", "Apt #456", "Panama", "KY", 78901); Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 1.00M); Letter letter3 = new Letter(a5, a6, 3.00M); // Letter test object GroundPackage gp1 = new GroundPackage(a7, a8, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a1, a8, 5, 5, 5, 5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a2, a7, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a6, 10, 15, 20, // Next Day test object 53, 4.00M); NextDayAirPackage ndap3 = new NextDayAirPackage(a4, a5, 20, 30, 40, // Next Day test object 50, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a2, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a7, a3, 12, 24, 36, // Two Day test object 90, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(); Console.WriteLine("Sorted List (Ascending Cost Order):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new DescendingZipOrder()); Console.WriteLine("Sorted List (Descending Zip Order:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Joseph Mccoy", "631 Plateau Avenue", "Portland", "OR", 56732); // Test Address 5 Address a6 = new Address("Allison Devers", "1009 Garland Avenue", "Palmetto", "GA", 91110); // Test Address 6 Address a7 = new Address("Lou Fields", "12 Henery St.", "Phoenix", "AZ", 19284); // Test Address 7 Address a8 = new Address("Michael Cruz", "908 Freedom Rd.", "Apt. 23", "Independence", "KY", 41033); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object 1 Letter letter2 = new Letter(a8, a6, 4.56M); // Letter test object 2 GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object 1 GroundPackage gp2 = new GroundPackage(a7, a5, 5, 6, 4.5, 1.5); // Ground test object 2 NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 1 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a4, a8, 20, 6, 3, 6.0, 12.5M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 1 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a6, a2, 4.5, 12.7, 5.5, // Two Day test object 2 5.1, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); // Populate list parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Console.WriteLine("Ordered By Destination ZIP:"); Console.WriteLine("===================="); //variable to hold linq ordered by zip //ordered list by zip code returned var orderByZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; //foreach loop to print ordered by destination zip foreach (Parcel p in orderByZip) { Console.WriteLine(p); Console.WriteLine("===================="); } Console.WriteLine("Ordered By Cost:"); Console.WriteLine("===================="); //variable to hold linq ordered by cost //ordered list based on the calc cost method returned var orderByCost = from p in parcels orderby p.CalcCost() select p; //foreach loop to print the cost sorted search foreach (Parcel p in orderByCost) { Console.WriteLine(p); Console.WriteLine("===================="); } Console.WriteLine("Ordered By Type:"); Console.WriteLine("===================="); //variable to hold linq ordered by first type and then cost //ordered list byt type then cost returned var orderByType = from p in parcels orderby p.GetType().ToString() ascending, p.CalcCost() descending select p; //foreach loop prints ordered output to console. foreach (Parcel p in orderByType) { Console.WriteLine(p); Console.WriteLine("===================="); } Console.WriteLine("Selecting Heavy Air Package:"); Console.WriteLine("===================="); //variable to hold linq ordered by weight, only of heavy air type. //ordered list by weight of only heavy air packages returned var onlyHeavyAir = from p in parcels where p is AirPackage let a = (AirPackage)p where a.IsHeavy() orderby a.Weight descending select a; //foreach loop to print output to console foreach (Parcel a in onlyHeavyAir) { Console.WriteLine(a); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and reports generated using LINQ static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("List of parcels:\n"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); Console.WriteLine("Parcels sorted by cost (ascending):\n"); parcels.Sort(); foreach (Parcel p in parcels) { Console.WriteLine(p.CalcCost().ToString("c")); } Pause(); Console.WriteLine("Parcels sorted by destination zip (descending):\n"); parcels.Sort(new DescendingDestZipOrder()); foreach (Parcel p in parcels) { Console.WriteLine(p.DestinationAddress.Zip.ToString("d5")); } Pause(); Console.WriteLine("Parcels sorted by type (ascending), then by cost (descending):\n"); parcels.Sort(new ExtraCreditOrder()); foreach (Parcel p in parcels) { Console.WriteLine(string.Format("{0,-20}{1,10}", p.GetType(), p.CalcCost().ToString("c"))); } }
// Precondition: None // Postcondition: Parcels have been created, sorted by LINQ, and displayed. static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Sherlock Holmes", "221 Baker Street", "Apt. B", "Boston", "MA", 02108); // Test Address 5 Address a6 = new Address("Tyler Durden", "537 Paper Street", "", "Bradford", "DE", 19808); Address a7 = new Address("Connor MacManus", "567 Red Road", "", "Denver", "CO", 80123); Address a8 = new Address("Kevin Lannister", "2020 Blue Street", "Apt. 20", "Independence", "MO", 64050); Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object 1 Letter letter2 = new Letter(a6, a8, 5.25M); // Letter test object 2 Letter letter3 = new Letter(a4, a7, 4.35M); // Letter test object 3 GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object 1 GroundPackage gp2 = new GroundPackage(a5, a3, 20, 18, 18, 15); // Ground test object 2 GroundPackage gp3 = new GroundPackage(a1, a6, 30, 30, 25, 80); // Ground test object 3 NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 1 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a4, a5, 12, 18, 30, // Next Day test object 2 15, 9.5M); NextDayAirPackage ndap3 = new NextDayAirPackage(a8, a1, 40, 50, 40, // Next Day test object 3 35, 6.85M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 1 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a5, a6, 30, 25, 20, // Two Day test object 2 25, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a7, a4, 50, 40, 35, // Two Day test object 3 56, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); // sort parcels by destination zip var sortedByZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; Console.WriteLine("Parcels sorted by destination zip"); // header // displays parcels foreach (var parcel in sortedByZip) { Console.WriteLine("\n" + parcel); } Console.WriteLine("------------------------------------------"); // divider // sort parcels by cost var sortedByCost = from p in parcels orderby p.CalcCost() select p; Console.WriteLine("\nParcels sorted by cost"); // header // displays parcels foreach (var parcel in sortedByCost) { Console.WriteLine("\n" + parcel); } Console.WriteLine("------------------------------------------"); // divider // sort parcels by type var sortedByParcelType = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; Console.WriteLine("\nParcels sorted by type and cost"); // header // displays parcels foreach (var parcel in sortedByParcelType) { Console.WriteLine("\n" + parcel); } Console.WriteLine("------------------------------------------"); // divider // sort heavy air packages by weight var heavyAirPackagesByWeight = from p in parcels where (p is AirPackage) && (((AirPackage)p).IsHeavy()) orderby((AirPackage)p).Weight select p; Console.WriteLine("\nHeavy Air Packages sorted by weight\n"); // header // displays parcels foreach (var parcel in heavyAirPackagesByWeight) { Console.WriteLine("\n" + parcel); } Console.WriteLine("------------------------------------------"); // divider Console.ReadLine(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90222); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Patrick Warren", "8431 Easton Commons Dr", "Apt. B", "Louisville", "Ky", 40242); //Test Address 5 Address a6 = new Address("Stan Lee", "9440 Santa Monica Blvd", "Suite 620", "Beveryly Hills", "CA", 90210); //Test Address 6 Address a7 = new Address("Eric McDowell", "2940 Yorkshire Blvd", "Louisville", "Ky", 40220); Address a8 = new Address("Harry Potter", "123 6th St.", "Melbourne", "FL", 32904); Letter letter1 = new Letter(a1, a2, 4.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 14.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 90, 8.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 130, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a5, a6, 3.95M); // Letter test object GroundPackage gp2 = new GroundPackage(a7, a8, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap2 = new NextDayAirPackage(a5, a7, 25, 15, 15, // Next Day test object 100.5, 7.50M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a5, 46.5, 39.5, 28.0, // Two Day test object 95, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); // Populate list parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Sort the list of Parcels in descending or by Destination Zip var destZipDesc = from parcel in parcels orderby parcel.DestinationAddress.Zip descending select parcel; //Print list Console.WriteLine("Destination Zip Descending List:"); Console.WriteLine("===================="); foreach (Parcel parcel in destZipDesc) { Console.WriteLine(parcel); Console.WriteLine("===================="); } Pause(); //Sort the list of Parcels by cost (ascending) var costAsc = from parcel in parcels orderby parcel.CalcCost() ascending select parcel; //Print list Console.WriteLine("Cost Ascending List:"); Console.WriteLine("===================="); foreach (Parcel parcel in costAsc) { Console.WriteLine(parcel); Console.WriteLine("===================="); } Pause(); //Sort list of Parcels by type (ascending), then cost (descending) var parcelTypeCost = from parcel in parcels orderby parcel.GetType().ToString() ascending, parcel.CalcCost() descending select parcel; //Print list Console.WriteLine("Cost Ascending List:"); Console.WriteLine("===================="); foreach (Parcel parcel in parcelTypeCost) { Console.WriteLine(parcel); Console.WriteLine("===================="); } Pause(); //Select all AirPackages that are heavy and sort by weight (descending) var isHeavyDesc = from parcel in parcels where parcel is AirPackage && ((AirPackage)parcel).IsHeavy() orderby((AirPackage)parcel).Weight descending select parcel; //Print list Console.WriteLine("Cost Ascending List:"); Console.WriteLine("===================="); foreach (Parcel parcel in isHeavyDesc) { Console.WriteLine(parcel); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Small list of Parcels is created and displayed static void Main(string[] args) { Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); GroundPackage gp2 = new GroundPackage(a1, a3, 10, 12, 2, 50); GroundPackage gp3 = new GroundPackage(a2, a1, 100, 15, 7, 20); GroundPackage gp4 = new GroundPackage(a4, a2, 5, 2, 11, 2); NextDayAirPackage na1 = new NextDayAirPackage(a1, a3, 25, 15, 15, 85, 7.5m); NextDayAirPackage na2 = new NextDayAirPackage(a2, a1, 10, 10, 10, 10, 10m); NextDayAirPackage na3 = new NextDayAirPackage(a3, a4, 45, 125, 15, 85, 0m); NextDayAirPackage na4 = new NextDayAirPackage(a4, a2, 25, 5, 5, 15, 50m); TwoDayAirPackage ta1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28, 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage ta2 = new TwoDayAirPackage(a1, a3, 10, 10, 10, 10, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage ta3 = new TwoDayAirPackage(a2, a4, 5, 5, 5, 50, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage ta4 = new TwoDayAirPackage(a3, a2, 30, 40, 28, 20, TwoDayAirPackage.Delivery.Early); List <GroundPackage> groundPackages = new List <GroundPackage>(); // Test list of parcels List <NextDayAirPackage> nextDayAirPackages = new List <NextDayAirPackage>(); // Test list of parcels List <TwoDayAirPackage> twoDayAirPackages = new List <TwoDayAirPackage>(); // Test list of parcels groundPackages.Add(gp1); groundPackages.Add(gp2); groundPackages.Add(gp3); groundPackages.Add(gp4); nextDayAirPackages.Add(na1); nextDayAirPackages.Add(na2); nextDayAirPackages.Add(na3); nextDayAirPackages.Add(na4); twoDayAirPackages.Add(ta1); twoDayAirPackages.Add(ta2); twoDayAirPackages.Add(ta3); twoDayAirPackages.Add(ta4); foreach (GroundPackage p in groundPackages) { WriteLine(p); WriteLine("--------------------"); } WriteLine("//////////////////////////////////////////////////////////////////////////"); foreach (NextDayAirPackage p in nextDayAirPackages) { WriteLine(p); WriteLine("--------------------"); } WriteLine("//////////////////////////////////////////////////////////////////////////"); foreach (TwoDayAirPackage p in twoDayAirPackages) { WriteLine(p); WriteLine("--------------------"); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 //Addresses 5-8 updated by Megan Balcom Address a5 = new Address("Erik Davis", "2700 Potomac Mills Circle", "", "Woodbridge", "VA", 22192); //Test Address 5 Address a6 = new Address("Luke Skywalker", "100 Death Star Rd", "Apt 2", "Galaxy", "CA", 90001); //Test Address 6 Address a7 = new Address("Sarah Balcom", "6610 Shelburn Dr", "", "Crestwood", "KY", 40014); //Test Address 7 Address a8 = new Address("Katherine Freid", "956 Treasure Ct", "", "Fort Mill", "SC", 29708); //Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a5, a8, 2.45M); // added by Megan Letter letter3 = new Letter(a6, a7, 6.5M); // added by Megan GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a6, a5, 16, 5, 12, 4.7); // added by Megan GroundPackage gp3 = new GroundPackage(a8, a7, 4, 6, 8, 10); // added by Megan NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a5, a7, 10, 3.2, 14, // added by Megan 6, 6.75M); NextDayAirPackage ndap3 = new NextDayAirPackage(a3, a8, 2, 2.5, 5, // added by Megan 6, 3.5M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a7, a8, 5, 10, 15, // added by Megan 20, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a5, a6, 8, 5, 9, // added by Megan 4.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); // LINQ query that selects parcels by their zip code var sortDestZip = // Holds the results of this query from p in parcels orderby p.DestinationAddress.Zip descending select p; // Outputs the query result to the console Console.WriteLine("Sort parcels by Destination Zip:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // LINQ query that sorts parcels by their cost var sortByCost = // Holds the results of this query from p in parcels orderby p.CalcCost() select p; // Outputs the query result to the console Console.WriteLine("Sort parcels by Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // LINQ query that that selects parcels by their type // and then orders them by descending cost var sortParcelType = //Holds the results of this query from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; // Outputs the query result to the console Console.WriteLine("Sort parcels by Parcel Type and Descending Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // LINQ query that selects Air Packages and // orders they by descending weight var apHeavyByWeight = // Holds the results of this query from p in parcels let ap = p as AirPackage where ap != null && ap.IsHeavy() orderby ap.Weight descending select ap; // Outputs the query result to the console Console.WriteLine("Sort Air Packages by Descending Weight:"); Console.WriteLine("===================="); foreach (var ap in parcels) { Console.WriteLine(ap); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Luke Skywalker", "101 dessert way", "Hut 1", "Mos Eisley", "TA", 12345); // Test Address 5 Address a6 = new Address("Bilbo Baggins", "202 Bagshot Row", "Bag End", "Hobbiton", "TS", 12346); // Test Address 6 Address a7 = new Address("Jon Snow", "303 Kings Rd", "", "Winterfell", "TN", 12347); // Test Address 7 Address a8 = new Address("Sherlock Holmes", "221B Baker St", "", "London", "GL", 12348); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a8, a7, 4.1M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a6, 10, 12, 4, 20.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a6, a5, 26, 16, 16, // Next Day test object 86, 8.5M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a5, a4, 47.7, 40.5, 29.0, // Two Day test object 81.5, TwoDayAirPackage.Delivery.Early); List<Parcel> parcels; // List of test parcels parcels = new List<Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(tdap1); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); var parcelsByZip = // Parcels sorted by zip code from p in parcels orderby p.DestinationAddress.Zip descending select p; Console.WriteLine("Parcels sorted by zip code"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByZip) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); var parcelsByCost = // Parcels sorted by cost from p in parcels orderby p.CalcCost() ascending select p; Console.WriteLine("Parcels sorted by cost"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByCost) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); var parcelsByType = // Parcels sorted by type from p in parcels orderby p.GetType().ToString() ascending select p; var parcelsByCost2 = // Sorted by type parcels sorted by cost from p in parcelsByType orderby p.CalcCost() descending select p; Console.WriteLine("Parcels sorted by type then cost"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByCost2) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); var airPackages = // Parcels that are air packages and heavy from p in parcels where p is AirPackage && ((AirPackage)p).IsHeavy() orderby ((AirPackage)p).Weight select p; Console.WriteLine("Heavy air packages sorted by weight"); Console.WriteLine("===================="); foreach (Parcel p in airPackages) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and reports generated using LINQ static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels)//displays the original original list { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort();//calls the parcel class, compareto method Console.WriteLine("Sorted List (by CalcCost in Ascending Order)"); Console.WriteLine("==============================================="); foreach (Parcel p in parcels)//displays the sorted data { Console.WriteLine(p); Console.WriteLine("======================="); } Pause(); parcels.Sort(new ReverseZipOrder());//sorts the data according to the reverse zip order class, compare method Console.WriteLine("Sorted in Reverse Order"); Console.WriteLine("========================"); foreach (Parcel p in parcels)//displays the sorted data { Console.WriteLine(p); Console.WriteLine("=================="); } Pause(); parcels.Sort(new MultiLevelSort());//sorts the data according to the multilevel sort class, compare method Console.WriteLine("Multilevel Sort"); Console.WriteLine("================"); foreach (Parcel p in parcels)//displays the sorted data { Console.WriteLine(p); Console.WriteLine("==================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Ivy Cat", "1234 Catnip St.", "Meowville", "KY", 56789); // Test Address 5 Address a6 = new Address("Mia Dog", "9876 Bone St.", "Growlsville", "NY", 78463); // Test Address 6 Address a7 = new Address("Rex Paw", "321 Treats Blvd.", "Apt. 6", "Barksville", "TN", 31254); // Test Address 7 Address a8 = new Address("Athena Slithers", "587 Snake Rd.", "Apt. 23", "Hissington", "LA", 87436); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Test Letter 1 Letter letter2 = new Letter(a5, a6, 5M); // Test Letter 2 Letter letter3 = new Letter(a8, a7, 2.50M); // Test Letter 3 Letter letter4 = new Letter(a6, a8, 4M); // Test Letter 4 GroundPackage gp1 = new GroundPackage(a2, a1, 5, 6, 7, 8); // Test GroundPackage 1 GroundPackage gp2 = new GroundPackage(a4, a2, 75, 75, 75, 50); // Test GroundPackage 2 GroundPackage gp3 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Test GroundPackage 3 GroundPackage gp4 = new GroundPackage(a5, a8, 20, 30, 30, 45); // Test GroundPackage 4 NextDayAirPackage ndap1 = new NextDayAirPackage(a4, a3, 50, 50, 50, 25, 10); // Test NextDayAirPackage 1 NextDayAirPackage ndap2 = new NextDayAirPackage(a1, a3, 25, 25, 25, 100, 8); // Test NextDayAirPackage 2 NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a4, 50, 50, 50, 100, 15); // Test NextDayAirPackage 3 NextDayAirPackage ndap4 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Test NextDayAirPackage 4 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a3, a2, 50, 25, 50, 30, // Test TwoDayAirPackage 1 TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a4, a1, 10, 10, 10, 10, // Test TwoDayAirPackage 2 TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Test TwoDayAirPackage 3 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap4 = new TwoDayAirPackage(a7, a6, 50, 75, 60, 80, // Test TwoDayAirPackage 4 TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(letter4); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(gp4); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(ndap4); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); parcels.Add(tdap4); Console.WriteLine("Original List:\n"); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Orders by Destination Address Zip in Descending Order:\n"); // 1) Select all Parcels and order by destination zip (descending) var zipDescending = from p in parcels orderby p.DestinationAddress.Zip descending select p; foreach (var p in zipDescending) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Orders by Cost in Ascending Order:\n"); // 2) Select all Parcels and order by cost (ascending) var costAscending = from p in parcels orderby p.CalcCost() ascending select p; foreach (var p in costAscending) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Orders by Parcel Type Ascending and then Cost Descending\n"); // 3) Select all Parcels and order by Parcel type (ascending) and then cost (descending) var typeThenCost = from p in parcels orderby p.GetType().ToString() ascending, p.CalcCost() descending select p; foreach (var p in typeThenCost) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Selects all AirPackages that are Heavy and orders them by Weight\n"); // 4) Select all AirPackage objects that are heavy and order by weight (descending) var heavyAirPackages = from p in parcels where p is AirPackage let aPackage = (AirPackage)p where aPackage.IsHeavy() orderby aPackage.Weight descending select aPackage; foreach (var p in heavyAirPackages) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and reports generated using LINQ static void Main(string[] args) { // Verbose Setting - true means complete output of parcel data // false means only relevant data output bool VERBOSE = false; // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Parcels by Destination Zip (desc) var parcelsByDestZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; Console.WriteLine("Parcels by Destination Zip (desc):"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByDestZip) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0:D5}", p.DestinationAddress.Zip); } Console.WriteLine("===================="); } Pause(); // Parcels by cost var parcelsByCost = from p in parcels orderby p.CalcCost() select p; Console.WriteLine("Parcels by Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByCost) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0,8:C}", p.CalcCost()); } Console.WriteLine("===================="); } Pause(); // Parcels by type and cost (desc) var parcelsByTypeCost = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; Console.WriteLine("Parcels by Type and Cost (desc):"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByTypeCost) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0,-17} {1,8:C}", p.GetType().ToString(), p.CalcCost()); } Console.WriteLine("===================="); } Pause(); // Heavy AirPackages by Weight (desc) var heavyAirPackagesByWeight = from p in parcels let ap = p as AirPackage // Downcast if AirPackage, null otherwise where (ap != null) && ap.IsHeavy() // Safe because of short-circuit orderby ap.Weight descending select p; // Alternate Solutions //var heavyAirPackagesByWeight = // from p in parcels // where (p is AirPackage) && ((AirPackage)p).IsHeavy() // Safe downcast // orderby ((AirPackage)p).Weight descending // Safe downcast // select p; // AirPackages first //var airPackages = // from p in parcels // where p is AirPackage // select (AirPackage) p; // Filter from AirPackages //var heavyAirPackagesByWeight = // from ap in airPackages // where ap.IsHeavy() // orderby ap.Weight descending // select ap; Console.WriteLine("Heavy AirPackages by Weight (desc):"); Console.WriteLine("===================="); foreach (AirPackage ap in heavyAirPackagesByWeight) { if (VERBOSE) { Console.WriteLine(ap); } else { Console.WriteLine("{0,-17} {1,4:F1}", ap.GetType().ToString(), ap.Weight); } Console.WriteLine("===================="); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Ryan Leezer", "666 Spring St", "", "New York CIty", "NY", 37219); //Test Address 5 Address a6 = new Address("Jessica Lewis", "100 7th St", "", "Tampa", "FL", 57611); //Test Address 6 Address a7 = new Address("Mark Hunt", "123 11th St", "Apt. 2", "Phoenix", "AZ", 21399); //Test Address 7 Address a8 = new Address("Mary Gates", "2838 W Jefferson St", "", "Atlanta", "GA", 18042); //Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object 1 GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object 1 NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 1 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 1 80.5, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a6, a8, 2.50M); //Letter test object 2 GroundPackage gp2 = new GroundPackage(a1, a6, 7, 5, 5, 10); //Ground test object 2 NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a8, 20, 18, 15, //Next Day test object 2 40, 5.5M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a2, a7, 40, 30, 30, //Two Day test object 2 61, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(tdap2); //LINQ query sorted by destination address zip var sortDestZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; // Display the query thorugh a foreach loop. Console.WriteLine("Parcels sorted by dest zip:"); Console.WriteLine("============================="); foreach (var P in sortDestZip) { Console.WriteLine(P.DestinationAddress.Zip); } Console.WriteLine(); //LINQ query sorted by cost var sortByCost = from p in parcels orderby p.CalcCost() select p; // Display the query thorugh a foreach loop. Console.WriteLine("Parcels sorted by cost:"); Console.WriteLine("============================="); foreach (var P in sortByCost) { Console.WriteLine(P.CalcCost()); } Console.WriteLine(); //LINQ query sorted by type, then cost var sortByTypeCost = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; // Display the query thorugh a foreach loop. Console.WriteLine("Parcels sorted by type, then cost:"); Console.WriteLine("===================================="); foreach (var P in sortByTypeCost) { Console.WriteLine(P.GetType() + ": " + P.CalcCost()); } Console.WriteLine(); //LINQ query sorted by the weight of heavy Air Packages var sortByAirPackageWeight = from p in parcels where p is AirPackage let P = (AirPackage)p where P.IsHeavy() == true orderby P.Weight descending select P; // Display the query thorugh a foreach loop. Console.WriteLine("Heavy Air Packages sorted by weight:"); Console.WriteLine("===================================="); foreach (var P in sortByAirPackageWeight) { Console.WriteLine(P.Weight); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Stan Marsh", "ShTapa Town", "4321 Your Street", "Pittsburgh", "Pennsylvania", 15201); // Test Address 5 Address a6 = new Address("Eric Cartman", "South Park Elementary", "Detention Room 102", "New York City", "New York", 10001); // Test Address 6 Address a7 = new Address("Kenny McCormick", "SoDoSoapa", "1111 Gheto Avenue", "South Park", "Colorado", 80019); // Test Address 7 Address a8 = new Address("Kyle Broflovski", "The Church of God", "5676 Jewish Lane", "Jersey City", "New Jersey", 07208); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter 1 test object Letter letter2 = new Letter(a2, a3, 4.50M); // Letter 2 test object Letter letter3 = new Letter(a3, a4, 1.50M); // Letter 3 test object Letter letter4 = new Letter(a4, a8, .75M); // Letter 4 test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground 1 test object GroundPackage gp2 = new GroundPackage(a5, a7, 10, 5, 2, 50); // Ground 2 test object GroundPackage gp3 = new GroundPackage(a7, a1, 15, 20, 25, 35.8); // Ground 3 test object GroundPackage gp4 = new GroundPackage(a2, a6, 60, 50, 23, 150); // Ground 4 test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day 1 test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a5, a6, 20, 10, 5, // Next Day 2 test object 80, 10.50M); NextDayAirPackage ndap3 = new NextDayAirPackage(a1, a8, 15, 40, 32, // Next Day 3 test object 95, 4.55M); NextDayAirPackage ndap4 = new NextDayAirPackage(a2, a7, 100, 50, 75, // Next Day 4 test object 70, 20.95M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day 1 test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a2, a4, 30.2, 19.4, 20.5, // Two Day 2 test object 90, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a3, a5, 6.5, 9.54, 30.04, // Two Day 3 test object 60.25, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap4 = new TwoDayAirPackage(a6, a8, 40.0, 50.5, 33.33, // Two Day 4 test object 50.75, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populates list with 4 letter objects parcels.Add(letter2); parcels.Add(letter3); parcels.Add(letter4); parcels.Add(gp1); // Populates list with 4 ground package objects parcels.Add(gp2); parcels.Add(gp3); parcels.Add(gp4); parcels.Add(ndap1); // Populates list with 4 next day air packages objects parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(ndap4); parcels.Add(tdap1); // Populates list with 4 two day air packages objects parcels.Add(tdap2); parcels.Add(tdap3); parcels.Add(tdap4); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } // selects all Parcels and orders them by destination zip in a descending order var destinationZip = from destZip in parcels orderby destZip.DestinationAddress.Zip descending select destZip; Console.WriteLine("\nParcels Ordered by Destination Zip"); // displays a header for the query Console.WriteLine("----------------------------------"); // displays all parcels ordered by their destination zip foreach (var destZip in destinationZip) { Console.WriteLine(destZip); } // selects all Parcels and orders them by their Cost in a ascending order var cost = from c in parcels orderby c.CalcCost() select c; Console.WriteLine("\nParcels Ordered by Cost"); // displays a header for the query Console.WriteLine("-----------------------"); // displays all parcels ordered by their cost foreach (var c in cost) { Console.WriteLine(c); } // selects all Parcels and orders them by their type (ascending) and their cost (descending) var parcelTypeandCost = from pTC in parcels orderby pTC.GetType().ToString() ascending, pTC.CalcCost() descending select pTC; Console.WriteLine("\nParcels Ordered by Type and Cost"); // displays a header for the query Console.WriteLine("-----------------------"); // displays all parcels ordered by their type and cost foreach (var pTC in parcelTypeandCost) { Console.WriteLine(pTC); } Console.WriteLine("\nHeavy AirPackages Ordered by Weight"); // displays a header for the query Console.WriteLine("-----------------------------------"); // selects all AirPackages in parcels that are heavy and orders them by their weight in a descending order var heavyAirPackage = from hAP in parcels where hAP is AirPackage let airPackage = (AirPackage)hAP where airPackage.IsHeavy() == true orderby airPackage.Weight descending select airPackage; // displays all Air Package parcels that are heavy and ordered by their weight foreach (var airPackage in heavyAirPackage) { Console.WriteLine(airPackage); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a2, a3, 4.4M); // Letter test object Letter letter3 = new Letter(a3, a4, 5.5M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a1, a2, 15, 11, 6, 13.5); // Groud test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a2, a4, 35, 25, 25, // Next Day test object 95, 8.5M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a2, 47.5, 40.5, 29, // Two Day test object 81.5, TwoDayAirPackage.Delivery.Early); List<Parcel> parcels; // List of test parcels parcels = new List<Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(tdap1); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(); Console.WriteLine("Items sorted in natural order (asc by Cost):\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new DescByDestZip()); Console.WriteLine("Parcels sorted descending by destination zip:\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new AscsTypeDescCost()); Console.WriteLine("Parcels sorted ascending by type then sorted descending by cost:\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Nicholas Goodridge", "1234 1st Street", "Apt #207", "Louisville", "KY", 12345); Address a6 = new Address("Derrek Baxter", "5678 2nd Street", "Cinncinatti", "OH", 67890); Address a7 = new Address("Eric Hoeweler", "0987 3rd Street", "Chicago", "IL", 45678); Address a8 = new Address("Lynnzee Kazee", "6543 4th Street", "Apt #456", "Panama", "KY", 10293); Letter l1 = new Letter(a1, a3, 0.50M); // Test Letter 1 Letter l2 = new Letter(a2, a4, 1.20M); // Test Letter 2 Letter l3 = new Letter(a4, a1, 1.70M); // Test Letter 3 GroundPackage g1 = new GroundPackage(a5, a6, 5, 5, 5, 10); GroundPackage g2 = new GroundPackage(a7, a8, 3.3, 4.4, 5.5, 6.6); GroundPackage g3 = new GroundPackage(a8, a5, 4, 5, 6, 1); NextDayAirPackage n1 = new NextDayAirPackage(a1, a8, 5, 5, 5, 10, 5); NextDayAirPackage n2 = new NextDayAirPackage(a2, a7, 40, 40, 40, 6, 10); NextDayAirPackage n3 = new NextDayAirPackage(a3, a6, 10, 10, 10, 75, 20); NextDayAirPackage n4 = new NextDayAirPackage(a4, a5, 40, 40, 40, 100, 30); TwoDayAirPackage t1 = new TwoDayAirPackage(a2, a5, 5, 5, 5, 10, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage t2 = new TwoDayAirPackage(a4, a7, 40, 40, 40, 6, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(l1); // Populate list parcels.Add(l2); parcels.Add(l3); parcels.Add(g1); parcels.Add(g2); parcels.Add(g3); parcels.Add(n1); parcels.Add(n2); parcels.Add(n3); parcels.Add(n4); parcels.Add(t1); parcels.Add(t2); Console.WriteLine("Select all Parcels and order by destination zip(descending)\n"); var part1 = from p in parcels orderby p.DestinationAddress.Zip descending select p; foreach (Parcel p in part1) { Console.WriteLine($"{p.DestinationAddress.Zip:D5}"); } Console.WriteLine("\n\nSelect all Parcels and order by cost (ascending)\n"); var part2 = from p in parcels orderby p.CalcCost() ascending select p; foreach (Parcel p in part2) { Console.WriteLine($"{p.CalcCost():C}"); } Console.WriteLine("\n\nSelect all Parcels and order by Parcel type (ascending) and then cost (descending)\n"); var part3 = from p in parcels orderby p.GetType().ToString() ascending, p.CalcCost() descending select p; foreach (Parcel p in part3) { Console.WriteLine($"type: {p.GetType().ToString()} \tcost: {p.CalcCost():C}"); } Console.WriteLine("Select all AirPackage objects that are heavy and order by weight (descending)\n"); var part4 = from p in parcels let ap = p as AirPackage where ap != null && ap.IsHeavy() orderby ap.Weight descending select ap; foreach (Parcel p in part4) { Console.WriteLine(p); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test objects (3) Letter letter2 = new Letter(a4, a3, 4.00M); Letter letter3 = new Letter(a2, a3, 2.00M); GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test objects (3) GroundPackage gp2 = new GroundPackage(a1, a2, 20, 5, 10, 15.5); GroundPackage gp3 = new GroundPackage(a2, a1, 2, 13, 4, 30.5); NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test objects (2) 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a2, 5, 16, 25, 20.0, 15.0M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test objects (2) 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a1, 50, 20, 35, 25, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Adds letter object to the list parcels.Add(letter2); // Adds letter object to the list parcels.Add(letter3); // Adds letter object to the list parcels.Add(gp1); // Adds GroundPackage object to the list parcels.Add(gp2); // Adds GroundPackage object to the list parcels.Add(gp3); // Adds GroundPackage object to the list parcels.Add(ndap1); // Adds NextDayAirPackage object to the list parcels.Add(ndap2); // Adds NextDayAirPackage object to the list parcels.Add(tdap1); // Adds TwoDayAirPackage object to the list parcels.Add(tdap2); // Adds TwoDayAirPackage object to the list Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //default sort // displays all parcels in the lsit by Cost in Ascending order parcels.Sort(); Console.Out.WriteLine("Default Sort, Ascending by Cost"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); //descending sort //displays all the parcels in the list by Destination Zip in Descending order parcels.Sort(new DescendingZipOrder()); Console.Out.WriteLine("Sorted List, Descending by Destination Zip Code"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a3, a2, 6.99M); // Letter test object GroundPackage gp2 = new GroundPackage(a4, a3, 11, 8, 3, 9.5); // Ground test object NextDayAirPackage ndap2 = new NextDayAirPackage(a2, a3, 35, 25, 20, // Next Day test object 95, 9.50M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a1, 26.5, 19.5, 18.0, // Two Day test object 40, TwoDayAirPackage.Delivery.Early); Letter letter3 = new Letter(a2, a3, 5.95M); // Letter test object GroundPackage gp3 = new GroundPackage(a3, a4, 5, 10, 2, 4); // Ground test object NextDayAirPackage ndap3 = new NextDayAirPackage(a3, a1, 20, 14, 12, // Next Day test object 15, 5M); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a2, a1, 12, 8, 3.5, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(letter3); parcels.Add(gp3); parcels.Add(ndap3); parcels.Add(tdap3); parcels.Add(tdap3); // Lists parcel in original list Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Lists parcel in order by cost parcels.Sort(); Console.Out.WriteLine("Sorted list (natural order):"); foreach (Parcel p in parcels) { Console.WriteLine(p.CalcCost().ToString("C")); } Pause(); // Lists parcels in descending order by Destination Zip Code parcels.Sort(new ParcelDescOrderByDestZip()); Console.Out.WriteLine("Sorted list (descending order) using Comparer:"); foreach (Parcel p in parcels) { Console.WriteLine(p.DestinationAddress.Zip.ToString("D5")); } Pause(); // Lists parcel in ascending order by Type and descending by cost parcels.Sort(new ParcelAscTypeDescCost()); Console.Out.WriteLine("Sorted list (type ascending order then\r\ncost descending order) using Comparer:"); foreach (Parcel p in parcels) { Console.Write($"{p.GetType(),-20} {p.CalcCost().ToString("C"), 10}"); Console.WriteLine(); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a4, a2, 5.5M); Letter letter3 = new Letter(a3, a2, 2M); GroundPackage gp2 = new GroundPackage(a4, a2, 16, 15, 15, 36); GroundPackage gp3 = new GroundPackage(a3, a1, 5, 5, 5, 15); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a1, a4, 13, 16, 14, 32, TwoDayAirPackage.Delivery.Early); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a2, 17, 10, 10, 27, 13M); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(tdap2); parcels.Add(ndap2); // Displays the parcels list as is Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Displays the parcel list in ascending order by cost parcels.Sort(); Console.WriteLine(); Console.WriteLine("List Parcels by Cost in Ascending Order"); Console.WriteLine("======================================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Displays the parcel list by zip code using the ParcelSort class parcels.Sort(new ParcelSort()); Console.WriteLine(); Console.WriteLine("List Parcels by Zip Code in descending Order"); Console.WriteLine("========================================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Displays the parcel list by typ then cost in descending order using the ParcelSortEC Class (or at least it should, couldn't get it to work) parcels.Sort(new ParcelSortEC()); Console.WriteLine(); Console.WriteLine("List Parcels by type in ascending order and then cost by descending order"); Console.WriteLine("====================================================================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed // linq query results are displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Rolfe", "3142 Drivy Drive", "Apt. 9", "Springfield", "IL", 11111); // Test Address 5 Address a6 = new Address("Alex Anderson", "2354 IDC Road", "Apt. 10", "Bucket", "LA", 22222); // Test Address 6 Address a7 = new Address("Ricky Robinson", "7465 Nowhere BLVD", "Suit 54", "Glendale", "FL", 33333); // Test Address 7 Address a8 = new Address("Bob Lob Law", "8765 Don't Care Parkway", "Apt. 1", "dat boi", "NY", 45445); Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a2, a3, 4.95M); // Test Letter 2 Letter letter3 = new Letter(a3, a4, 5.95M); // Test Letter 3 Letter letter4 = new Letter(a4, a5, 6.95M); // Test Letter 4 GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a4, a5, 10, 10, 10, 10); // Test GroundPackage 2 GroundPackage gp3 = new GroundPackage(a5, a6, 11, 11, 11, 11); // Test GroundPackage 3 GroundPackage gp4 = new GroundPackage(a6, a7, 12, 12, 12, 12); // Test GroundPackage 4 NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a5, a6, 13, 13, 13, 13, 7.95M); // Test NextDayAirPackage 2 NextDayAirPackage ndap3 = new NextDayAirPackage(a6, a7, 10, 20, 40, 80, 8.95M); // Test NextDayAirPackage 3 NextDayAirPackage ndap4 = new NextDayAirPackage(a7, a8, 80, 90, 100, 200, 9.95M); // Test NextDayAirPackage 4 TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 14, 14, 14, 14, TwoDayAirPackage.Delivery.Early); // Test TwoDayAirPackage 2 TwoDayAirPackage tdap3 = new TwoDayAirPackage(a7, a2, 16, 14, 16, 14, TwoDayAirPackage.Delivery.Saver); // Test TwoDayAirPackage 3 TwoDayAirPackage tdap4 = new TwoDayAirPackage(a6, a3, 40, 56, 67, 31, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); // Populate list parcels.Add(letter1); parcels.Add(letter2); parcels.Add(letter3); parcels.Add(letter4); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(gp4); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(ndap4); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); parcels.Add(tdap4); Console.WriteLine("Original List\n"); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("==============="); } Pause(); // Select all Parcels and order by destination zip (descending) Console.WriteLine("Descending Destination Zip\n"); var descendingZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; // display descendingZip foreach (Parcel p in descendingZip) { Console.WriteLine(p.DestinationAddress.Zip); Console.WriteLine(); } Pause(); // Select all Parcels and order by cost (ascending) Console.WriteLine("Ascending Costs\n"); var ascendingCosts = from p in parcels orderby p.CalcCost() select p; // display ascendingCosts foreach (Parcel p in ascendingCosts) { Console.WriteLine(p.CalcCost().ToString("C2")); Console.WriteLine(); } Pause(); // Select all Parcels and order by Parcel type (ascending) and then cost (descending) Console.WriteLine("Ascending Type"); Console.WriteLine("Descending Cost\n"); var typeThenCost = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; // display typeThenCost foreach (Parcel p in typeThenCost) { Console.WriteLine(p.GetType().ToString()); Console.WriteLine(p.CalcCost().ToString("C2")); Console.WriteLine(); } Pause(); // Select all AirPackage objects that are heavy and order by weight (descending) Console.WriteLine("Heavy Air Packages Sorted By Weight\n"); var heavyAirPackageByWeight = from p in parcels where p is AirPackage let apkg = (AirPackage)p // downcaste to AirPackage where apkg.IsHeavy() // gain access to AirPackage methods orderby apkg.Weight select apkg; // display heavyAirPackageByWeight foreach (AirPackage apkg in heavyAirPackageByWeight) { Console.WriteLine(apkg.GetType().ToString()); Console.WriteLine(apkg.Weight); Console.WriteLine(); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a5, a6, 2.99m); GroundPackage gp2 = new GroundPackage(a7, a8, 9, 11, 7, 10.99); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a7, 39, 13, 9, 92, 4.85M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 56.2, 28.7, 30, 79.4, TwoDayAirPackage.Delivery.Early); Letter letter3 = new Letter(a4, a6, 5.67m); Letter letter4 = new Letter(a2, a7, 4.99m); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(tdap2); parcels.Add(letter3); parcels.Add(letter4); parcels.Sort(); Console.WriteLine("Natural Order:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new DescendingParcelZip()); Console.WriteLine("Sorted list (descending by destination zip) using Comparer:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a2, 22.99M); // Letter test object Letter letter3 = new Letter(a2, a4, 22.98M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a1, a2, 41, 20, 3, 121); // Ground test object GroundPackage gp3 = new GroundPackage(a2, a4, 1, 1.3, 9, 11.1); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 80, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a1, a3, 22, 11, 14, // Next Day test object 85, 23.99M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a4, a1, 4, 9.5, 13, // Two Day test object 44.30, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(tdap1); parcels.Add(tdap2); //Unsorted List Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(); // Sort using that natural order //Sorted List ascending by CalcCost Console.Out.WriteLine("Sorted List (naturally):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); parcels.Sort(new ZipCodeSort()); //Sort using the created class //Sorted List descending by zipcode Console.Out.WriteLine("Sorted List using Comparer (descending naturally)"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Lewis Clark", "613 Rainy Way", "Portland", "ME", 04101); // Test Address 5 Address a6 = new Address("Titus Carrol", "3612 Sierra Lane", "Louisville", "KY", 40202); // Test Address 6 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a1, a4, 5.65M); Letter letter3 = new Letter(a4, a6, 4.12M); GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a1, a6, 12, 14, 7, 16.7); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a5, a6, 13, 22, 17, // Next Day test object 76, 8.90M); NextDayAirPackage ndap3 = new NextDayAirPackage(a1, a2, 17, 12, 20, // Next Day test object 40, 7.23M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a2, a3, 34.5, 23.5, 46.0, 60.2, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort();//sort in ascending order Console.WriteLine("Sorted List (Ascending):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Original List:");//display orginal list again, as per instructions Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new DescendingParcelOrder()); //sort with comparer class Console.WriteLine("Sorted List (Descending):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Original List:");//display orginal list again, as per instructions Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //sorted by Parcel type and then cost descending parcels.Sort(new ParcelTypeSort()); //sort with EC comparer class Console.WriteLine("Sorted List (Parcel Type and Descending cost):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and reports generated using LINQ static void Main(string[] args) { // Verbose Setting - true means complete output of parcel data // false means only relevant data output bool VERBOSE = false; // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List<Parcel> parcels; // List of test parcels parcels = new List<Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); // Parcels by Destination Zip (desc) var parcelsByDestZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; Console.WriteLine("Parcels by Destination Zip (desc):"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByDestZip) { if (VERBOSE) Console.WriteLine(p); else Console.WriteLine("{0:D5}", p.DestinationAddress.Zip); Console.WriteLine("===================="); } Pause(); // Parcels by cost var parcelsByCost = from p in parcels orderby p.CalcCost() select p; Console.WriteLine("Parcels by Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByCost) { if (VERBOSE) Console.WriteLine(p); else Console.WriteLine("{0,8:C}", p.CalcCost()); Console.WriteLine("===================="); } Pause(); // Parcels by type and cost (desc) var parcelsByTypeCost = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; Console.WriteLine("Parcels by Type and Cost (desc):"); Console.WriteLine("===================="); foreach (Parcel p in parcelsByTypeCost) { if (VERBOSE) Console.WriteLine(p); else Console.WriteLine("{0,-17} {1,8:C}", p.GetType().ToString(), p.CalcCost()); Console.WriteLine("===================="); } Pause(); // Heavy AirPackages by Weight (desc) var heavyAirPackagesByWeight = from p in parcels let ap = p as AirPackage // Downcast if AirPackage, null otherwise where (ap != null) && ap.IsHeavy() // Safe because of short-circuit orderby ap.Weight descending select p; // Alternate Solutions //var heavyAirPackagesByWeight = // from p in parcels // where (p is AirPackage) && ((AirPackage)p).IsHeavy() // Safe downcast // orderby ((AirPackage)p).Weight descending // Safe downcast // select p; // AirPackages first //var airPackages = // from p in parcels // where p is AirPackage // select (AirPackage) p; // Filter from AirPackages //var heavyAirPackagesByWeight = // from ap in airPackages // where ap.IsHeavy() // orderby ap.Weight descending // select ap; Console.WriteLine("Heavy AirPackages by Weight (desc):"); Console.WriteLine("===================="); foreach (AirPackage ap in heavyAirPackagesByWeight) { if (VERBOSE) Console.WriteLine(ap); else Console.WriteLine("{0,-17} {1,4:F1}", ap.GetType().ToString(), ap.Weight); Console.WriteLine("===================="); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test objects Letter letter2 = new Letter(a2, a3, 7.00M); Letter letter3 = new Letter(a3, a4, 10.00M); GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test objects GroundPackage gp2 = new GroundPackage(a1, a2, 20, 15, 30, 70); NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test objects 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a2, a4, 30, 30, 30, 30, 8.00M); NextDayAirPackage ndap3 = new NextDayAirPackage(a1, a2, 2, 2, 2, 2, 1.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test objects 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a2, 6, 6, 6, 6, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(); Console.WriteLine("Items sorted in natural order (asc by Cost):\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new DescByDestZip()); Console.WriteLine("Parcels sorted descending by destination zip:\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new AscsTypeDescCost()); Console.WriteLine("Parcels sorted ascending by type then sorted descending by cost:\n"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Patrick Warren", "8431 Easton Commons Dr", "Apt. B", "Louisville", "Ky", 40242); //Test Address 5 Address a6 = new Address("Stan Lee", "9440 Santa Monica Blvd", "Suite 620", "Beveryly Hills", "CA", 90210); //Test Address 6 Address a7 = new Address("Eric McDowell", "2940 Yorkshire Blvd", "Louisville", "Ky", 40220); Address a8 = new Address("Harry Potter", "123 6th St.", "Melbourne", "FL", 32904); Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); Letter letter2 = new Letter(a5, a6, 6M); // Letter test object GroundPackage gp2 = new GroundPackage(a8, a7, 20, 15, 10, 15.5); // Ground test object NextDayAirPackage ndap2 = new NextDayAirPackage(a6, a7, 30, 20, 20, // Next Day test object 90, 7.50M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a7, a5, 50, 45, 35, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 20, 10, 10, // Next Day test object 80, 7.50M); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a8, a1, 40, 35, 25, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list - 10 Parcel Objects parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(letter2); parcels.Add(gp2); parcels.Add(ndap2); parcels.Add(tdap2); parcels.Add(ndap3); parcels.Add(tdap3); //Main list Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Ascending Order parcels.Sort(); // sorts parcels - natural order Console.WriteLine("Ascending List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Descending list parcels.Sort(new DescendingParcel()); //Sort in descending order Console.WriteLine("Descending List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); //Ascending Type Descending Cost Order parcels.Sort(new AscTypeDescCost()); // Sort parcels first in ascending order by type. // Then sort in descending order by cost Console.WriteLine("Ascending Type Descending Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and reports generated using LINQ static void Main(string[] args) { // Verbose Setting - true means complete output of parcel data // false means only relevant data output bool VERBOSE = false; // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List<Parcel> parcels; // List of test parcels parcels = new List<Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("Original List Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8:C}", p.GetType().ToString(), p.CalcCost()); Console.WriteLine("===================="); } Pause(); parcels.Sort();// Sort - uses natural order Console.WriteLine("Parcels by cost (ascending) (default):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8:C}", p.GetType().ToString(), p.CalcCost()); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Original List Zip:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8}", p.GetType().ToString(), p.DestinationAddress.Zip); Console.WriteLine("===================="); } Pause(); parcels.Sort(new OrderByDesZip());// Sort - uses specified Comparer class Console.WriteLine("Parcels by destination Zip (descending):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8}", p.GetType().ToString(), p.DestinationAddress.Zip); Console.WriteLine("===================="); } Pause(); Console.WriteLine("Original List Type and Cost:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8:c}", p.GetType().ToString(), p.CalcCost()); Console.WriteLine("===================="); } Pause(); parcels.Sort(new SortByTypeThenCost());// Sort - uses specified Comparer class Console.WriteLine("Parcels by Type (ascending) then cost (descending):"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine("{0,-17} {1,8:c}", p.GetType().ToString(), p.CalcCost()); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); GroundPackage gp2 = new GroundPackage(a3, a1, 47, 6, 13.5, 10); // Ground test object Letter letter2 = new Letter(a3, a1, 5.75M); // Letter test object NextDayAirPackage ndap2 = new NextDayAirPackage(a4, a1, 30, 20, 10, // Next Day test object 85, 7.50M); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a1, a2, 43.5, 37.5, 24, // Two Day test object 60.5, TwoDayAirPackage.Delivery.Saver); GroundPackage gp3 = new GroundPackage(a3, a2, 12, 7, 5, 20.5); // Ground test object Letter letter3 = new Letter(a1, a4, 7.75M); // Letter test object List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(gp2); parcels.Add(letter2); parcels.Add(ndap2); parcels.Add(tdap2); parcels.Add(gp3); parcels.Add(letter3); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); parcels.Sort(new AscendingSortOrder()); // Sort - uses specified Comparer class Console.Out.WriteLine("Sorted list (ascending natural order) using Comparer:"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); parcels.Sort(new DestinationZipDescendingOrder()); // Sort - uses specified Comparer class Console.Out.WriteLine("Sorted list (descending natural order) using Comparer:"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); parcels.Sort(new OrderAll()); // Sort - uses specified Comparer class Console.Out.WriteLine("Sorted list (based on package) using Comparer:"); foreach (Parcel p in parcels) { Console.WriteLine(p); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a1, a2, 4.54M); Letter letter3 = new Letter(a2, a4, 7.76M); Letter letter4 = null; //Null test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a1, a2, 15, 11, 6, 13.5); GroundPackage gp3 = new GroundPackage(a4, a1, 20, 14, 9, 16.7); NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a2, a4, 13, 22, 55, // Next Day test object 55, 10.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a1, 44.5, 55.5, 30.0, // Two Day test object 85.5, TwoDayAirPackage.Delivery.Early); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(tdap1); parcels.Add(ndap2); parcels.Add(letter2); parcels.Add(tdap2); parcels.Add(letter3); parcels.Add(gp3); parcels.Add(letter4); Console.WriteLine("Original List:"); Console.WriteLine("===================="); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine(); Console.WriteLine("Descending Zip Order: "); parcels.Sort(new DescendingZipOrder()); //sort, using specified comparer class DescendingZipOrder foreach (Parcel p in parcels) //print list { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine(); Console.WriteLine("Ascending Cost Order: "); parcels.Sort(new AscendingCostOrder()); //sort, using specified comparer class AscendingCostOrder foreach (Parcel p in parcels) //print list { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); Console.WriteLine(); Console.WriteLine("Ascending Parcel Type/Descending Cost Order: "); parcels.Sort(new AscendingParcelSort()); //sort, using specified comparer class AscendingParcelSort foreach (Parcel p in parcels) //print list { Console.WriteLine(p); Console.WriteLine("===================="); } }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Namey McNamerson", "666 McAddresson Way", "Palo Alto", "CA", 94020); //Test Address 5 Address a6 = new Address("John Lennon", "123 E Main St.", "New York", "NY", 10037); //Test Address 6 Address a7 = new Address("Rosetta Williams", "1793 Jefferson St.", "Eastville", "VA", 23347); //Test Address 7 Address a8 = new Address("Ryan Colby", "4704 Happy Hollow Road", "Wallace", "NC", 28466); //Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test objects Letter letter2 = new Letter(a7, a8, 5.00M); Letter letter3 = new Letter(a5, a6, 2.50M); GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test objects GroundPackage gp2 = new GroundPackage(a5, a8, 10, 20, 30, 76); GroundPackage gp3 = new GroundPackage(a6, a7, 5, 30, 100, 40); NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test objects 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a1, a8, 30, 30, 30, 50, 10.00M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10, 15, 20, 30, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test objects 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a4, a5, 32, 65, 12, 100, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a5, a6, 5, 5, 5, 3, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); //Select all Parcels and order by destination zip (descending) var orderByDestZip = from parcel in parcels orderby parcel.DestinationAddress.Zip descending select parcel; // display query results Console.WriteLine("Results of query orderByDestZip"); //First task output label Console.WriteLine("================================"); //Line foreach (var parcel in orderByDestZip) { Console.WriteLine(parcel); Console.WriteLine("===================="); } //Select all Parcels and order by cost (ascending) var orderByCost = from parcel in parcels orderby parcel.CalcCost() ascending select parcel; //display query results Console.WriteLine("Results of query orderByCost"); Console.WriteLine("================================"); foreach (var parcel in orderByCost) { Console.WriteLine(parcel); Console.WriteLine("===================="); } //Select all Parcels and order by Parcel type (ascending) and then cost (descending) var orderByTypeAndCost = from parcel in parcels orderby parcel.GetType() ascending orderby parcel.CalcCost() descending select parcel; //Display query results Console.WriteLine("Results of query orderByTypeAndCost"); Console.WriteLine("================================"); foreach (var parcel in parcels) { Console.WriteLine(parcel); Console.WriteLine("===================="); } //Select all AirPackage objects that are heavy and order by weight (descending) var selectHeavyAirPackages = from parcel in parcels let ap = parcel as AirPackage where (ap != null) && ap.IsHeavy() orderby ap.Weight ascending select parcel; //Display query results Console.WriteLine("Results of query orderByTypeAndCost"); Console.WriteLine("================================"); foreach (var parcel in parcels) { Console.WriteLine(parcel); Console.WriteLine("===================="); } //Display entire list of parcels Console.WriteLine("================================"); Console.WriteLine("Original List:"); Console.WriteLine("================================"); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("===================="); } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("[Redacted]", "000 X Street", "Layer 10", "Duckwater", "NV", 00001); // Test Address 5 Address a6 = new Address("Mike", "1 Mike Street", "The Mike Zone", "Mike Peak", "MN", 75892); // Test Address 6 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, 85, 7.50M); // Next Day test object TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Saver); // Two Day test object Letter letter2 = new Letter(a3, a5, 0.00M); // Letter test object TwoDayAirPackage tdap2 = new TwoDayAirPackage(a3, a2, 5, 4, 2, 8, TwoDayAirPackage.Delivery.Saver); // Two Day test object TwoDayAirPackage tdap3 = new TwoDayAirPackage(a4, a6, 50, 40, 20, 80, TwoDayAirPackage.Delivery.Early); // Two Day test object Letter letter3 = new Letter(a1, a2, 0.58M); // Letter test object Letter letter4 = new Letter(a3, a4, 9.00M); // Letter test object Letter letter5 = new Letter(a5, a6, 1.25M); // Letter test object Letter letter6 = new Letter(a6, a5, 125.125M); // Letter test object List <Parcel> parcels = new List <Parcel>(); // List of test parcels // Populate list parcels.Add(letter1); // 1 parcels.Add(gp1); // 2 parcels.Add(ndap1); // 3 parcels.Add(tdap1); // 4 parcels.Add(letter2); // 5 parcels.Add(tdap2); // 6 parcels.Add(tdap3); // 7 parcels.Add(letter3); // 8 parcels.Add(letter4); // 9 parcels.Add(letter5); // 10 parcels.Add(letter6); // 11 // ------------------------------------------------------------- Original List // no sorting WriteLine("Original List:"); WriteLine("===================="); sortedList(parcels); // lists parcels // ------------------------------------------------------------- Default Sort: ascending cost parcels.Sort(); // default sort WriteLine("Cost Ascending (Default Sort):"); WriteLine("===================="); sortedList(parcels); // lists parcels based on default sort // ------------------------------------------------------------- Descending ZIP parcels.Sort(new Parcel_DescZipSort()); // sorts based on the specified class (descending destination zip) WriteLine("Destination Zip Descending:"); WriteLine("===================="); sortedList(parcels); // lists parcels based on specified sort above // ------------------------------------------------------------- Extra Credit parcels.Sort(new Parcel_TypeCostSort()); // sorts based on the specified class (asc. type, desc. cost) WriteLine("Type Ascending, Cost Descending:"); WriteLine("===================="); sortedList(parcels); // lists parcels based on specified sort above }
// Precondition: None // Postcondition: Parcels have been created and sorted in a variety of different orders static void Main(string[] args) { // Verbose Setting - true means complete output of parcel data // false means only relevant data output bool VERBOSE = false; // Test Data - Magic Numbers OK Address a1 = new Address("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 Address a6 = new Address("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 Address a7 = new Address("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 Address a8 = new Address("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a3, a4, 4.25M); // Letter test object GroundPackage gp1 = new GroundPackage(a5, a6, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a7, a8, 8.5, 9.5, 6.5, 2.5); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a3, a5, 9.5, 6.0, 5.5, // Next Day test object 5.25, 5.25M); NextDayAirPackage ndap3 = new NextDayAirPackage(a2, a7, 10.5, 6.5, 9.5, // Next Day test object 15.5, 5.00M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a5, a7, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a1, 15.0, 9.5, 6.5, // Two Day test object 75.5, TwoDayAirPackage.Delivery.Early); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a6, a4, 12.0, 12.0, 6.0, // Two Day test object 5.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels; // List of test parcels parcels = new List <Parcel>(); parcels.Add(letter1); // Populate list parcels.Add(letter2); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); Console.WriteLine("Original List:"); Console.WriteLine("========================"); foreach (Parcel p in parcels) { Console.WriteLine(p); Console.WriteLine("========================"); } Pause(); parcels.Sort(); Console.WriteLine("Sorted in Natural Order (Ascending by Cost):"); Console.WriteLine("========================"); foreach (Parcel p in parcels) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0,6:C}", p.CalcCost()); } } Pause(); parcels.Sort(new DescendingByDestZipComparer()); Console.WriteLine("Sorted using first Comparer (Descending by Destination Zip):"); Console.WriteLine("========================"); foreach (Parcel p in parcels) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0:D5}", p.DestinationAddress.Zip); } } Pause(); parcels.Sort(new TypeAndCostComparer()); Console.WriteLine("Sorted using EC Comparer (Ascending By Type Then Descending by Cost):"); Console.WriteLine("========================"); foreach (Parcel p in parcels) { if (VERBOSE) { Console.WriteLine(p); } else { Console.WriteLine("{0,17} {1,6:C}", p.GetType(), p.CalcCost()); } } Pause(); }
// Precondition: None // Postcondition: Parcels have been created and displayed static void Main(string[] args) { // Test Data - Magic Numbers OK Address a1 = new Address(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 Address a2 = new Address("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 Address a3 = new Address("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 Address a4 = new Address("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 Address a5 = new Address("Luke Skywalker", "123 Desert Rd", "Tattoine", "TA", 20495); // Test Address 5 Address a6 = new Address("Darth Vader", "1 Death Star Rd", "Death Star", "DS", 05953); // Test Address 6 Address a7 = new Address("Obi-Wan Kenobi", "69 Hello There St.", "Judland Wastes", "TA", 95302); // Test Address 7 Address a8 = new Address("Padme Amidala", "10 Will To Live Dr.", "Mustafar", "MU", 20193); // Test Address 8 Letter letter1 = new Letter(a1, a2, 3.95M); // Letter test object Letter letter2 = new Letter(a2, a1, 4.8M); // Letter test object Letter letter3 = new Letter(a3, a7, 6.99M); // Letter test object GroundPackage gp1 = new GroundPackage(a3, a4, 14, 10, 5, 12.5); // Ground test object GroundPackage gp2 = new GroundPackage(a3, a4, 4, 1, 2, 1.5); // Ground test object GroundPackage gp3 = new GroundPackage(a3, a4, 32, 33, 13, 23); // Ground test object NextDayAirPackage ndap1 = new NextDayAirPackage(a1, a3, 25, 15, 15, // Next Day test object 85, 7.50M); NextDayAirPackage ndap2 = new NextDayAirPackage(a4, a6, 55, 14, 155, // Next Day test object 8, 7.50M); NextDayAirPackage ndap3 = new NextDayAirPackage(a7, a8, 25, 3, 12, // Next Day test object 82, 7.50M); TwoDayAirPackage tdap1 = new TwoDayAirPackage(a4, a1, 46.5, 39.5, 28.0, // Two Day test object 80.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap2 = new TwoDayAirPackage(a8, a3, 4.5, 34, 44, // Two Day test object 20.5, TwoDayAirPackage.Delivery.Saver); TwoDayAirPackage tdap3 = new TwoDayAirPackage(a4, a5, 20.5, 32.5, 20, // Two Day test object 50.5, TwoDayAirPackage.Delivery.Saver); List <Parcel> parcels = new List <Parcel>(); // List of test parcels // Populate list parcels.Add(letter1); parcels.Add(letter2); parcels.Add(letter3); parcels.Add(gp1); parcels.Add(gp2); parcels.Add(gp3); parcels.Add(ndap1); parcels.Add(ndap2); parcels.Add(ndap3); parcels.Add(tdap1); parcels.Add(tdap2); parcels.Add(tdap3); //Selects all Parcels and orders by destination zip (descending) var byZip = from p in parcels orderby p.DestinationAddress.Zip descending select p; //Selects all Parcels and orders by cost (ascending) var byCost = from p in parcels orderby p.CalcCost()//orders by cost using CalcCost method select p; //Selects all Parcels and orders them by type (ascending), and then cost (descending var byTypeThenCost = from p in parcels orderby p.GetType().ToString(), p.CalcCost() descending select p; //Selects all AirPackages that are heavy and orders them by weight (descending) var heavyByWeight = from p in parcels where ((p is AirPackage) && (p as AirPackage).IsHeavy()) //making sure only airpackages that are heavy show up let ap = p as AirPackage where (ap != null) && ap.IsHeavy() //filtering so that no null airpackages show up and they are heavy orderby ap.Weight descending select ap; //displaying the output of byZip foreach (var p in byZip) { WriteLine(p); WriteLine("================="); } WriteLine("========================================================"); //displaying the output of byCost foreach (var p in byCost) { WriteLine(p); WriteLine("================="); } WriteLine("========================================================"); //displaying the output of byTypeThenCost foreach (var p in byTypeThenCost) { WriteLine(p); WriteLine("================="); } WriteLine("========================================================"); //displaying the output of heavyByWeight foreach (var p in heavyByWeight) { WriteLine(p); WriteLine("================="); } Pause(); }