Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Unit 3 IP - Two Dimension Array Code Segment Cassandra Beyer
- // American Intercontinental University , October 24, 2010
- using System;
- using Systems.Collections;
- using Systems.Collections.Generic;
- // Benefits Class
- public class Benefits {
- private int profit;
- private int years;
- private string name;
- Benefits ( string n, int y, int p) {
- name = n;
- years = y;
- profit = p;
- //Constructor, which provides initialization of the data
- }
- }
- // Now we choose the data structure you would use
- // internally for the class. For the purposes
- // of this assignment, a list would be best.
- list <Benefits> newlist = new list <Benefits> ();
- // creates the list of objects in the Benefits class titled "newlist"
- Public static void insert()
- {
- newlist.Add (new Benefits ("Roth", 12, 7);
- newlist.Add (new Benefits ("Pension", 9, 3);
- newlist.Add (new Benefits ("SocialSecurity", 5, 1);
- }
- //Inserts new elements to the list in the "n,y,p" format
- Public static void sort()
- {
- Console.WriteLine("Unsorted list");
- newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
- //Different sorting possibilities below
- List<Benefits> data = newlist.FindAll(delegate(Benefits q) { return q.p < 5; });
- Console.WriteLine("Profit is less than 5 percent");
- data.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
- Console.WriteLine("Sorted list, by name");
- newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.n.CompareTo(q2.n); });
- newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
- newlist.Sort(delegate(Benefits q1, Benefits q2) { return q1.y.CompareTo(p2.y); });
- Console.WriteLine("Sorted list, by years");
- newlist.ForEach(delegate(Benefits q) { Console.WriteLine(String.Format("{0} {1}", q.n, q.y, q.p)); });
- }
- // OK the above code is somewhat complicated but basically it
- // sorts in three different ways, one being by outputing all
- // benefits whose profits are less than 5 percent. The seocnd
- // returns a sorted list by name, and the third returns the
- // sorted list by years. These utlize the ForEach and FindAll
- // commands, which is the basic bubble sort. Because there are
- // 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