Advertisement
Guest User

Moss v2.0 Assessment Code

a guest
Aug 27th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. /* Social Planner V2.0
  12.  * Aaron Moss
  13.  * 28/08/14
  14.  */
  15.  
  16. namespace SocialPlanner
  17. {
  18.     public partial class frmSocialPlanner : Form
  19.     {
  20.         int user = 0;   //  Stores User Number
  21.         Button[] btnArray = new Button[6];  //  Array of Option Buttons
  22.         List<int> inputtedValues = new List<int> { };   //  List of Inputted Values (Mutable Array)
  23.  
  24.         public frmSocialPlanner()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private void txtEventName_TextChanged(object sender, EventArgs e)
  30.         {
  31.             if (txtEventName.Text.Length > 4)   //  Once user inputs characters...
  32.             {
  33.                 btnDone.Enabled = true; //  Done becomes enabled, guides user
  34.             }
  35.         }
  36.  
  37.         private void btnDone_Click(object sender, EventArgs e)  //  Once done clicked...
  38.         {
  39.             txtEventName.ReadOnly = true;   //  Event Name Becomes Final
  40.             txtEventName.TextAlign = HorizontalAlignment.Center;    //  Centered
  41.             btnDone.Visible = false;
  42.             lblTutorialText.Text = "Please Rate Your Interest in the Event Below from 0-5";
  43.  
  44.             foreach (Button b in btnArray)
  45.             {
  46.                 b.Enabled = true;   //  Enables the option buttons in fast enumeration
  47.             }
  48.         }
  49.  
  50.         private void frmSocialPlanner_Load(object sender, EventArgs e)
  51.         {
  52.             /* This code runs on loadtime, takes care of boring processes, adding buttons
  53.              * to the array, giving them properties */
  54.             btnArray[0] = btnRate0;
  55.             btnArray[1] = btnRate1;
  56.             btnArray[2] = btnRate2;
  57.             btnArray[3] = btnRate3;
  58.             btnArray[4] = btnRate4;
  59.             btnArray[5] = btnRate5;
  60.  
  61.             foreach ( Button b in btnArray)
  62.             {
  63.                 b.Click += new EventHandler(anyButton_Click);
  64.  
  65.                 /* This gives the buttons special events on hover and dehover which can
  66.                  * be used for visual feedback ; see below */
  67.                 b.MouseHover += new EventHandler(anyButton_Hover);
  68.                 b.MouseLeave += new EventHandler(anyButton_Unhover);
  69.             }
  70.         }
  71.         void anyButton_Click(object sender, EventArgs e)
  72.         {
  73.             user += 1;  //  Once option button is clicked, new user
  74.  
  75.             if (user > 1)   //  Once the program has 2 values...
  76.             {
  77.                 btnFinish.Enabled = true;   //  The program can average
  78.             }
  79.  
  80.             lblPerson.Text = "Friend " + user;  //  Friend Number x using program
  81.  
  82.             /* More boring code that was simply faster to do manually than programatically.
  83.              * In this case it checks which button is being pressed, and changes an input
  84.              * variable to that balue */
  85.  
  86.             int inputtedValue = 9;  //  Error Code
  87.  
  88.             if ( sender == btnArray[0] )
  89.             {
  90.                 inputtedValue = 0;
  91.                 MessageBox.Show("You Selected Option 0. You don't want to go at all.");
  92.             }
  93.             else if ( sender == btnArray[1] )
  94.             {
  95.                 inputtedValue = 1;
  96.                 MessageBox.Show("You Selected Option 1.");
  97.             }
  98.             else if (sender == btnArray[2])
  99.             {
  100.                 inputtedValue = 2;
  101.                 MessageBox.Show("You Selected Option 2.");
  102.             }
  103.             else if (sender == btnArray[3])
  104.             {
  105.                 inputtedValue = 3;
  106.                 MessageBox.Show("You Selected Option 3.");
  107.             }
  108.             else if (sender == btnArray[4])
  109.             {
  110.                 inputtedValue = 4;
  111.                 MessageBox.Show("You Selected Option 4.");
  112.             }
  113.             else
  114.             {
  115.                 inputtedValue = 5;
  116.                 MessageBox.Show("You Selected Option 5. You absolutely want to go!");
  117.             }
  118.  
  119.             if ( inputtedValue == 9)    //  If Error Code Comes Through
  120.             {
  121.                 MessageBox.Show("Critical Error."); //  Display Error Message
  122.             }
  123.  
  124.             inputtedValues.Add(inputtedValue);  //  Add the Value of the Button to Array of Values
  125.         }
  126.         void anyButton_Hover(object sender, EventArgs e)    //  On Hover
  127.         {
  128.             foreach (Button b in btnArray)
  129.             {
  130.                 if (b == sender)
  131.                 {
  132.                     b.BackColor = Color.LightGreen; //  Make Button Green
  133.                 }
  134.             }
  135.         }
  136.         void anyButton_Unhover(object sender, EventArgs e)  //  And revert on unhover
  137.         {
  138.             foreach (Button b in btnArray)
  139.             {
  140.                 if (b == sender)
  141.                 {
  142.                     b.BackColor = SystemColors.Control;
  143.                 }
  144.             }
  145.         }
  146.  
  147.         /* Aethetic change gives user some feedback */
  148.  
  149.         private void btnFinish_Click(object sender, EventArgs e)
  150.         {
  151.             /* Averaging will be done here */
  152.  
  153.             foreach (Button b in btnArray)
  154.             {
  155.                     b.Enabled = false;  //  Disables option buttons
  156.             }
  157.  
  158.             btnFinish.Enabled = false;
  159.  
  160.             /* Averaging */
  161.  
  162.             double average = inputtedValues.Average();
  163.             int roundedAverage = Convert.ToInt32(average);
  164.             bool lowerBoundPresence = false;
  165.  
  166.             MessageBox.Show("Average Answer was " + roundedAverage);
  167.  
  168.             /* Checks for the cases that will evaluate to true or false */
  169.  
  170.             foreach (int i in inputtedValues)   //  check values for 0 or 1
  171.             {
  172.                 if (i == 1 || i == 0)
  173.                 {
  174.                     lowerBoundPresence = true;
  175.                 }
  176.             }
  177.  
  178.             if ( roundedAverage >= 3 && !lowerBoundPresence)
  179.             {
  180.                 MessageBox.Show("You will be going to the " + txtEventName.Text + " this weekend as the average is higher than 3!");
  181.             }
  182.             else if ( lowerBoundPresence )
  183.             {
  184.                 MessageBox.Show("You will not be going to the " + txtEventName.Text + " this weekend as there are some people do not wish to go at all!");
  185.             }
  186.             else
  187.             {
  188.                 MessageBox.Show("You will not be going to the " + txtEventName.Text + " this weekend because the average is lower than 3!");
  189.             }
  190.  
  191.             btnReset.Enabled = true;
  192.         }
  193.  
  194.         private void btnReset_Click(object sender, EventArgs e)
  195.         {
  196.             Application.Restart();
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement