CSIS 1624 CLASS TEST 6 Instructions: Use visual studio 2012/2013 Make sure your work is saved correctly Submit your work as instructed by the demmies. This is an open-book test. You may consult the printed text book but no electronic material. You may not communicate to anybody in anyway whatsoever. Marks will be subtracted if your student number and name is not in the program Question 1[63] Consider the scenario of an athletics meeting. Develop a class, CAthlete, with - a static array of items that are presented at the meeting, e.g. "100 m", "200 m", "400 m", "800 m", "1500 m", "5000 m", "110 m hurdles", "400 m hurdles", "Long Jump", "High Jump", "Triple Jump", "Discuss Throw", "Shot Put" and "Javelin Throw"; - a static parallel array containing whether the item is sorted ascending or descending; - a read-only properties for the name and gender of an athlete; - the item in which an athlete participates; - a property for the performance in a specific item; - a constructor that will assign the name of the athlete upon instantiation. Implement the class in a Windows forms application as in the example. - Fill the combobox with items from the static array in the class. Make sure that the user cannot enter anything else in the box. - Prepare a list of athletes. - The Add button should add a new athlete with details as in the controls, to the list. - Write the code to list all athletes in all items alphabetically.
- Write the code to list the athletes of a specific item in order of merit and gender. Note that for some items the order is in ascending order o performance (e.g. High Jump) and for others it is in descending order of performance, e.g. 100 m. You can ask the user for the item to list and gender in input boxes.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Class_test_6 class CAthlete public static string[] arritems = new string [] "100 m", "200 m", "400 m", "800 m", "1500 m", "5000 m", "110 m hurdles", "400 m hurdles", "Long Jump", "High Jump", "Triple Jump", "Discuss Throw", "Shot Put", "Javelin Throw" ; public static char[] arrsorting = new char[] 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'D', 'D', 'D', 'D', 'D', 'D' ; public string Name get; private set; public char Gender get; private set; public string Performance get; set; private string sitem; public string Item get return sitem; set if (arritems.contains(value)) sitem = value; //Item //Constructor public CAthlete(string _Name, char _Gender ) Name = _Name; Gender = _Gender; //class CAthlete //namespace using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.VisualBasic; namespace Class_test_6 public partial class CfrmMeeting : Form private List<CAthlete> lstathletes; public CfrmMeeting() InitializeComponent(); private void CfrmMeeting_Load(object sender, EventArgs e)
cmboitems.items.addrange(cathlete.arritems); lstathletes = new List<CAthlete>(); //CfrmMeeting_Load private void btnclose_click(object sender, EventArgs e) Application.Exit(); private void btnadd_click(object sender, EventArgs e) try char Gender = radmale.checked? 'M' : 'F'; CAthlete Athlete = new CAthlete(txtName.Text, Gender ); Athlete.Item = cmboitems.text; Athlete.Performance = txtperformance.text; lstathletes.add(athlete); txtname.clear(); cmboitems.selecteditem = null; txtperformance.clear(); txtname.focus(); MessageBox.Show("Athlete added to list."); catch MessageBox.Show("Invalid performance. Athlete not added."); //btnadd_click private void btnlistalphabetically_click(object sender, EventArgs e) lstathletes = lstathletes.orderby(athlete => athlete.name).tolist(); string s = "Athlete".PadRight(20) + "Item".PadRight(10) + "\n" + "".PadRight(20, '=') + "".PadRight(10, '=') + "\n"; foreach (CAthlete athlete in lstathletes) s += athlete.name.padright(20) + athlete.item + "\n"; MessageBox.Show(s); //btnlistalphabetically_click private void btnlistmerit_click(object sender, EventArgs e) string sitem = Interaction.InputBox("Item:", "Select item", "", 200, 200); string sgender = Interaction.InputBox("Gender(M/F):", "What Gender", "", 200, 200); int j = CAthlete.arrItems.ToList().IndexOf(sItem); if(cathlete.arrsorting[j] == 'A') lstathletes = lstathletes.orderby(athlete => athlete.performance).tolist(); else lstathletes = lstathletes.orderbydescending(athlete => athlete.performance).tolist(); string s; if(sgender == "M") s = GetString(sItem, 'M'); else s = GetString(sItem, 'F'); MessageBox.Show(s); //btnlistmerit_click private string GetString(string sitem, char Gender) string s; s = "No\t" + "Athlete".PadRight(20) + "Performance".PadRight(10) + "\n" + "".PadRight(20, '=') + "".PadRight(10, '=') + "\n"; int i = 0; foreach (CAthlete athlete in lstathletes) if (athlete.item == sitem && athlete.gender == Gender) s += (++i).tostring().padleft(2) + "\t" + athlete.name.padright(20) + athlete.performance + "\n"; return s;
private void cmboitems_selectedindexchanged(object sender, EventArgs e) if(cmboitems.selecteditem!= null) if (CAthlete.arrSorting[cmboItems.SelectedIndex] == 'A') txtperformance.mask = "00:00.00"; else txtperformance.mask = ""; //public partial class CfrmMeeting : Form