Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 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 internally for the class. For the purposes of this assignment, a list would be best.
  26.  
  27.  
  28.  
  29. list <Benefits> newlist = new list <Benefits> ();
  30.  
  31. // creates the list of objects in the Benefits class titled "newlist"
  32.  
  33.  
  34.  
  35.  
  36. Public static void insert()
  37.  
  38. {
  39. newlist.Add (new Benefits ("Roth", 12, 7);
  40. newlist.Add (new Benefits ("Pension", 9, 3);
  41. newlist.Add (new Benefits ("SocialSecurity", 5, 1);
  42.  
  43. }
  44.  
  45. //Inserts new elements to the list in the "n,y,p" format
  46.  
  47.  
  48.  
  49. Public static void sort()
  50. {
  51.  
  52. Console.WriteLine("Unsorted list");
  53. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  54.  
  55. //Different sorting possibilities below
  56.  
  57. List<Benefits> data = newlist.FindAll(delegate(Benefits q) { return q.p < 5; });
  58.  
  59. Console.WriteLine("Profit is less than 5 percent");
  60. data.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  61.  
  62. Console.WriteLine("Sorted list, by name");
  63. newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.n.CompareTo(q2.n); });
  64.  
  65.  
  66. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  67.  
  68. newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.y.CompareTo(p2.y); });
  69.  
  70. Console.WriteLine("Sorted list, by years");
  71. newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
  72. }
  73.  
  74. // OK the above code is somewhat complicated but basically it sorts in three different ways, one being by outputing all
  75. // benefits whose profits are less than 5 percent. The seocnd returns a sorted list by name, and the third returns the
  76. // sorted list by years. These utlize the ForEach and FindAll commands, which is the basic bubble sort. Because there are
  77. // not many retirement plans (maybe thirty at the most?), Bubble sort will be more than efficient to be effectual.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement