Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. // Unit 3 IP - Two Dimension Array Code Segment Cassandra Beyer
  2. // American Intercontinental University , October 24, 2010
  3.  
  4. using System;
  5. using Systems.Collections;
  6. using Systems.Collections.Generic;
  7.  
  8.  
  9. // Benefits Class
  10.  
  11. public class Benefits {
  12. private int profit;
  13. private int years;
  14. private string name;
  15.  
  16. Benefits ( string n, int y, int p) {
  17.     name = n;
  18.     years = y;
  19.     profit = p;
  20. //Constructor, which provides initialization of the data
  21.  
  22.     }
  23. }
  24.  
  25. // Now we choose the data structure you would use
  26. // internally for the class. For the purposes
  27. // of this assignment, a list would be best.
  28.  
  29.  
  30.  
  31. list <Benefits> newlist = new list <Benefits> ();
  32.  
  33. // creates the list of objects in the Benefits class titled "newlist"
  34.  
  35.  
  36.  
  37.  
  38. Public static void insert()
  39.  
  40. {
  41. newlist.Add (new Benefits ("Roth", 12, 7);
  42. newlist.Add (new Benefits ("Pension", 9, 3);
  43. newlist.Add (new Benefits ("SocialSecurity", 5, 1);
  44.  
  45. }
  46.  
  47. //Inserts new elements to the list in the "n,y,p" format
  48.  
  49.  
  50.  
  51. Public static void sort()
  52. {
  53.  
  54. Console.WriteLine("Unsorted list");
  55. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  56.  
  57. //Different sorting possibilities below
  58.  
  59. List<Benefits> data = newlist.FindAll(delegate(Benefits q) { return q.p < 5; });
  60.  
  61. Console.WriteLine("Profit is less than 5 percent");
  62. data.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  63.  
  64. Console.WriteLine("Sorted list, by name");
  65. newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.n.CompareTo(q2.n); });
  66.  
  67.  
  68. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  69.  
  70. newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.y.CompareTo(p2.y); });
  71.  
  72. Console.WriteLine("Sorted list, by years");
  73. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  74. }
  75.  
  76. // OK the above code is somewhat complicated but basically it
  77. // sorts in three different ways, one being by outputing all
  78. // benefits whose profits are less than 5 percent. The seocnd
  79. // returns a sorted list by name, and the third returns the
  80. // sorted list by years. These utlize the ForEach and FindAll
  81. // commands, which is the basic bubble sort. Because there are
  82. // not many retirement plans (maybe thirty at the most?),
  83. // Bubble sort will be more than efficient to be effectual.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement