Skip to content

Instantly share code, notes, and snippets.

@jinan-kordab
Created February 1, 2019 00:11
Show Gist options
  • Save jinan-kordab/79cc9d08f2498b5089164a3502d1ee66 to your computer and use it in GitHub Desktop.
Save jinan-kordab/79cc9d08f2498b5089164a3502d1ee66 to your computer and use it in GitHub Desktop.
Final exam requirement - helping students
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainProject
{
class Program
{
//Drivers License Variable
int totalNumberOfCorrectAnswers = 0;
static void Main(string[] args)
{
Console.WriteLine("Main Menu");
Console.WriteLine("1. Students & Grades");
Console.WriteLine("2. Rainfall Statistics");
Console.WriteLine("3. Charge Account Validation");
Console.WriteLine("4. Payroll");
Console.WriteLine("5. Drivers Licence Exam");
Console.WriteLine("6. Quit this menu");
Console.WriteLine("Enter your choice");
int uInput = Convert.ToInt32(Console.ReadLine());
do
{
switch (uInput)
{
case 1:
{
// Console.WriteLine("Students & Grades");
StudentsAndGrades();
break;
}
case 2:
{
//Console.WriteLine("Rainfall Statistics");
RainfallStatistics();
break;
}
case 3:
{
//Console.WriteLine("Charge Account Validation");
ChargeAccountValidation();
break;
}
case 4:
{
//Console.WriteLine("Payroll");
Payroll();
break;
}
case 5:
{
//Console.WriteLine("Drivers Licence Exam");
DriversLicenceExam();
break;
}
default:
{
//Console.WriteLine("ERROR: Please enter a number between 1 and 6");
Environment.Exit(0);
break;
}
}
uInput = Convert.ToInt32(Console.ReadLine());
}
while (uInput != 6);
}
private static void StudentsAndGrades()
{
int numberOfStudents;
int numberOfTestsPerStudent;
//Number of students
do
{
Console.WriteLine("How many students ?");
numberOfStudents = Convert.ToInt32(Console.ReadLine());
}
while (numberOfStudents < 0 || numberOfStudents == 0);
do
{
//Number of tests per student
Console.WriteLine("How many tests ?");
numberOfTestsPerStudent = Convert.ToInt32(Console.ReadLine());
}
while (numberOfTestsPerStudent < 0 || numberOfTestsPerStudent == 0);
//Grade per student pair
var TestsPerStudentlist = new List<KeyValuePair<int, int>>();
int s = 0;
//per each student
do
{
int t = 0;
//per each test
do
{
Console.WriteLine("Enter grade for student " + (s + 1) + " test " + (t + 1) + " : ");
int sGrade = Convert.ToInt32(Console.ReadLine());
TestsPerStudentlist.Add(new KeyValuePair<int, int>((s + 1), sGrade));
t++;
}
while (t < numberOfTestsPerStudent);
s++;
}
while (s < numberOfStudents);
// output results
// number of students* tests
double globalAverage = 0.0;
for (int student = 0; student < numberOfStudents; student++)
{
int result = TestsPerStudentlist.Where(x => x.Key == (student + 1)).Sum(x => x.Value);
string studentsAverage = Convert.ToString((double)(result / numberOfTestsPerStudent));
globalAverage = globalAverage + Convert.ToDouble(studentsAverage);
Console.WriteLine("For student " + (student + 1) + " the average is: " + studentsAverage + " and the sum of grades is " + result);
}
Console.WriteLine();
Console.WriteLine("The average of the average of all students is: " + (double)(globalAverage / numberOfStudents));
Console.WriteLine("Enter MAIN MENU choice");
}
private static void RainfallStatistics()
{
//Rainfall Statistics
int counter = 0;
string[] Months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
var RainfallPerMonth = new List<KeyValuePair<int, int>>();
do
{
Console.WriteLine("Enter the total rainfall for " + (Months[counter].ToString()) + ":");
int rainfallUnits = Convert.ToInt32(Console.ReadLine());
RainfallPerMonth.Add(new KeyValuePair<int, int>(counter, rainfallUnits));
counter++;
}
while (counter < Months.Length);
int totalRainfallForTheYear = TotalRainfallForTheYear(RainfallPerMonth);
double averagemonthlyRainfall = AverageMonthlyRainfall(RainfallPerMonth);
int[] highestAndLowest = HighestMonthAndLowest(RainfallPerMonth);
Console.WriteLine("The total rainfall for the year is: " + totalRainfallForTheYear);
Console.WriteLine("The average monthly rainfall is: " + averagemonthlyRainfall);
Console.WriteLine("Month with the highest amount of rain: " + Months[highestAndLowest[0] - 1].ToString());
Console.WriteLine("Month with the lowest amount of rain: " + Months[highestAndLowest[1] - 1].ToString());
Console.WriteLine("Enter MAIN MENU choice");
}
private static void ChargeAccountValidation()
{
//Charge Account Validation
int[] ValidChargeAccountNumbers = new int[18] { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651, 7881200, 4581002 };
Console.WriteLine("Charge Account Validation - Sequential Search");
Console.WriteLine("Please enter a number");
int uInput = Convert.ToInt32(Console.ReadLine());
bool isValidNumber = IsValid(ValidChargeAccountNumbers, uInput);
if (isValidNumber)
{
Console.WriteLine("The number is VALID !");
}
else
{
Console.WriteLine("The number is NOT VALID !");
}
Console.WriteLine("Enter MAIN MENU choice");
}
private static void Payroll()
{
int[] empId = new int[7] { 56588, 45201, 78951, 87775, 84512, 13028, 75804 };
int[] hours = new int[7];
double[] payRate = new double[7];
double[] wages = new double[7];
int t = 0;
//per each test
do
{
Console.WriteLine("Employee number: " + empId[t]);
Console.WriteLine("Enter hours: ");
hours[t] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter pay rate: ");
string doublePayRate = Console.ReadLine();
double tmp = Convert.ToDouble(doublePayRate, CultureInfo.InvariantCulture);
payRate[t] = tmp;
Console.WriteLine("----------------------------------------");
t++;
}
while (t < 7);
//Calculating gross wages for each employee
wages[0] = Convert.ToDouble(hours[0], CultureInfo.InvariantCulture) * payRate[0];
wages[1] = Convert.ToDouble(hours[1], CultureInfo.InvariantCulture) * payRate[1];
wages[2] = Convert.ToDouble(hours[2], CultureInfo.InvariantCulture) * payRate[2];
wages[3] = Convert.ToDouble(hours[3], CultureInfo.InvariantCulture) * payRate[3];
wages[4] = Convert.ToDouble(hours[4], CultureInfo.InvariantCulture) * payRate[4];
wages[5] = Convert.ToDouble(hours[5], CultureInfo.InvariantCulture) * payRate[5];
wages[6] = Convert.ToDouble(hours[6], CultureInfo.InvariantCulture) * payRate[6];
for (int j = 0; j < 7; j++)
{
displayWages(wages, empId, j);
}
Console.WriteLine("Enter MAIN MENU choice");
}
private static void DriversLicenceExam()
{
var CorrectExamAnswers = new List<KeyValuePair<int, string>>();
CorrectExamAnswers.Add(new KeyValuePair<int, string>(1, "B"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(2, "D"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(3, "A"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(4, "A"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(5, "C"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(6, "A"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(7, "B"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(8, "A"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(9, "C"));
CorrectExamAnswers.Add(new KeyValuePair<int, string>(10, "D"));
Console.WriteLine("Please enter user's answers: ");
var StudentExamAnswers = new List<KeyValuePair<int, string>>();
string ans;
int t = 0;
//per each test
do
{
//validation
do
{
Console.WriteLine("Answer for Question " + (t + 1) + " : ");
ans = Console.ReadLine();
}
while (ans != "A" && ans != "B" && ans != "C" && ans != "D");
StudentExamAnswers.Add(new KeyValuePair<int, string>((t + 1), ans));
t++;
}
while (t < 10);
bool PassOrFail = passFail(CorrectExamAnswers, StudentExamAnswers);
//NEW LINE
Console.WriteLine();
// Displaying passed or failed
if (PassOrFail)
Console.WriteLine("PASSED");
else
Console.WriteLine("FAILED");
//NEW LINE
Console.WriteLine();
// Display stats graph
Console.WriteLine("Question |Answer |User Input |Result");
Console.WriteLine("------------------------------------------------");
for (int i = 0; i < 10; i++)
{
//FORMATTING DIGIT 10
if (i == 9)
{
Console.WriteLine((i + 1) + "." + " |" + CorrectExamAnswers[i].Value.ToString() + " |" + StudentExamAnswers[i].Value.ToString() + " |" + (CorrectExamAnswers[i].Value.ToString().Trim() == StudentExamAnswers[i].Value.ToString().Trim() ? "Correct" : "Incorrect"));
}
else
{
Console.WriteLine((i + 1) + "." + " |" + CorrectExamAnswers[i].Value.ToString() + " |" + StudentExamAnswers[i].Value.ToString() + " |" + (CorrectExamAnswers[i].Value.ToString().Trim() == StudentExamAnswers[i].Value.ToString().Trim() ? "Correct" : "Incorrect"));
}
}
Console.WriteLine("Total number of correct answers: " + GetTotalNumberOfCorrectAnswers(CorrectExamAnswers, StudentExamAnswers));
Console.WriteLine("Total number of incorrect answers: " + (10 - GetTotalNumberOfCorrectAnswers(CorrectExamAnswers, StudentExamAnswers)));
Console.WriteLine("Enter MAIN MENU choice");
}
//helper functions
static int TotalRainfallForTheYear(List<KeyValuePair<int, int>> rainPerMonth)
{
int totalRainfallForTheYear = 0;
for (int i = 0; i < rainPerMonth.Count; i++)
{
totalRainfallForTheYear = totalRainfallForTheYear + rainPerMonth[i].Value;
}
return totalRainfallForTheYear;
}
static double AverageMonthlyRainfall(List<KeyValuePair<int, int>> rainPerMonth)
{
int totalRainfallForTheYear = 0;
double averageMonthlyrainfall = 0.0;
totalRainfallForTheYear = TotalRainfallForTheYear(rainPerMonth);
averageMonthlyrainfall = totalRainfallForTheYear / 12;
return averageMonthlyrainfall;
}
static int[] HighestMonthAndLowest(List<KeyValuePair<int, int>> rainPerMonth)
{
int[] highestLowest = new int[2];
string highestMonth = "";
string lowestMonth = "";
int maxmonth = Convert.ToInt32(rainPerMonth.Max(y => y.Value));
int minmonth = Convert.ToInt32(rainPerMonth.Min(y => y.Value));
highestLowest[0] = maxmonth;
highestLowest[1] = minmonth;
return highestLowest;
}
private static bool IsValid(int[] validchargeaccnumbers, int userInput)
{
bool isValid = false;
for (int i = 0; i < validchargeaccnumbers.Length; i++)
{
if (validchargeaccnumbers[i] == userInput)
{
isValid = true;
break;
}
}
return isValid;
}
private static void displayWages(double[] wges, int[] employeeids, int empIndex)
{
Console.WriteLine("Employee Identification Number: " + employeeids[empIndex] + "- Gross Wage: " + wges[empIndex]);
}
private static bool passFail(List<KeyValuePair<int, string>> correctAnswers, List<KeyValuePair<int, string>> stuAns)
{
bool pass = false;
int passFailcounter = 0; // 6 out of 10 should match
for (int i = 0; i < 10; i++)
{
if (correctAnswers[i].Value == stuAns[i].Value)
{
passFailcounter = passFailcounter + 1;
}
}
if (passFailcounter >= 6)
pass = true;
else
pass = false;
return pass;
}
private static int GetTotalNumberOfCorrectAnswers(List<KeyValuePair<int, string>> correctAnswers, List<KeyValuePair<int, string>> stuAns)
{
bool pass = false;
int passFailcounter = 0; // 6 out of 10 should match
for (int i = 0; i < 10; i++)
{
if (correctAnswers[i].Value == stuAns[i].Value)
{
passFailcounter = passFailcounter + 1;
}
}
return passFailcounter;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment