ID:1827249
 

Poll: How would you rate the effort i put into accomplishing a simple task out of 10?

Over 9000/10 33% (3)
10/10 0% (0)
9/10 0% (0)
8/10 11% (1)
7/10 0% (0)
6/10 0% (0)
5/10 0% (0)
4/10 0% (0)
3/10 0% (0)
2/10 0% (0)
1/10 55% (5)

Login to vote.

This is pretty much what I've been doing lately. Just homework. It's literally been taking up all of my time and I'm just making the best out of it by making it fun and challenging for myself.

I'll be revising it to clean it up. I honestly just tried to get the job done as fast as possible (which, with a lot of second guesses and thoughts like "I'm really over-complicating this project when I could do it in 100 lines of code or less.", only took me 3 hours to create without any debugging necessary).

Instructions:

(two parts, each worth 50 points)
(p1) Create a Student class that includes the following fields: last name, first name, gpa, and major (use appropriate identifers and types for each). Include properties for each of the fields. Include at least two constructors. You can decide what each of the two constructors does.
(p2) Create a C# program (Console or GUI) that uses your Student class. The code should instantiate at least two objects (using your two constructors). Your program should set and get (and display) each of the fields in each of the two objects (in other words, your program should exercise the fields in the objects such that you demonstrate that the fields are being written to and read from properly).
Be sure to closely follow the "Turning in your assignments" instructions from the syllabus.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentsClass
{
class Program
{
static List<Student> students = new List<Student>();
static bool running = true;
static bool goingBack = false;
static string userInput;

/// <summary>
/// The student Class.
/// Contains the first and last name (strings), GPA (float),
/// and major (string) of a student.
///
/// All values can be empty.
///
/// If first or last names are empty, they return N/A.
///
/// If the major is empty, it returns N/A.
///
/// If the GPA is empty it returns 0 as a float.
/// If the GPA is less than zero it is set to 0.
/// </summary>
class Student
{
//Variables and properties
private string firstName;
public string FirstName
{
get
{
return firstName != null ? firstName : "N/A";
}
set
{
firstName = value;
}
}

private string lastName;
public string LastName
{
get
{
return lastName != null ? lastName : "N/A";
}
set
{
lastName = value;
}
}

private float gpa;
public float GPA
{
get
{
return gpa != null ? gpa : 0;
}
set
{
if(value < 0)
{
gpa = 0;
}
else
{
gpa = value;
}
}
}

private string major;
public string Major
{
get
{
return major != null ? major : "N/A";
}
set
{
major = value;
}
}

public Student()
{
//Create an empty student
}

//Constructors
public Student(string studentFirstName)
{
//Set the student's first name only.
FirstName = studentFirstName;
}

public Student(string studentFirstName, string studentLastName)
{
//Set the student's first name.
FirstName = studentFirstName;

//Set the student's last name too.
LastName = studentLastName;
}

public Student(string studentFirstName, string studentLastName, float studentGpa)
{
//Set the student's first name.
FirstName = studentFirstName;

//Set the student's last name.
LastName = studentLastName;

//Set the student's GPA.
//Note, GPAs can only be positive.
//Negative values result in 0.
GPA = studentGpa;
}

public Student(string studentFirstName, string studentLastName, float studentGpa, string studentMajor)
{
//Set the student's first name.
FirstName = studentFirstName;

//Set the student's last name.
LastName = studentLastName;

//Set the student's GPA.
GPA = studentGpa;

//Set the student's major.
Major = studentMajor;
}
}

static void Main(string[] args)
{
//Prompt the user for data for the student.
//Make a new student with the given information.
//Give them the option to end their entry and check where they left off to create a proper construction.
//Give them toe option to end the program as a whole.
//Display student data.
//Make it like a database console where you press 0 to exit, 1 to make new student, - to back, 2 to delete a student
while (running)
{
BeginProgram(); //Start the magic
}
}

public static void BeginProgram()
{
//User-friendly dialog instructions
Console.WriteLine(@"
*** *** *** *** *** *** *** ***
Main Menu
-Controls-
1: Add a new student
2: Delete a student
3: Display student data
-: Exit the program
*** *** *** *** *** *** *** ***
");
Console.Write("Enter a valid value: ");

//Main menu switch
switch (Console.ReadLine())
{
//Add new student
case "1":
AddNewStudent();
break;

case "2":
DeleteAStudent();
break;

case "3":
DisplayStudentData();
break;

case "-":
ExitProgram();
break;

default:
Console.WriteLine("\n\n\tMESSAGE: *Invalid value. Try again.\n");
break;
}
}

public static void AddNewStudent()
{
//User-friendly dialog instructions
Console.WriteLine(@"
*** *** *** *** *** *** *** ***
Add Students
Enter the requested values.
To exit, enter \
*** *** *** *** *** *** *** ***
");

string userInputFirstName = null, userInputLastName = null, userInputMajor = null;
float userInputGPA = -1; //Instead of making it null, set it to -1 since we'll be comparing it that way.

while (running)
{
if (goingBack)
{
goingBack = false;

//Before actually going back... Since the user entered Y to starting a new student entry,
//start the creation update of the new student.
if(userInputFirstName != null && userInputLastName != null && userInputGPA >= 0 && userInputMajor != null)
{
students.Add(new Student(userInputFirstName, userInputLastName, userInputGPA, userInputMajor));
}
else
{
if (userInputFirstName != null && userInputLastName != null && userInputGPA >= 0)
{
students.Add(new Student(userInputFirstName, userInputLastName, userInputGPA));
}
else
{
if (userInputFirstName != null && userInputLastName != null)
{
students.Add(new Student(userInputFirstName, userInputLastName));
}
else
{
if (userInputFirstName != null && userInputLastName != null)
{
students.Add(new Student(userInputFirstName, userInputLastName));
}
else
{
if (userInputFirstName != null)
{
students.Add(new Student(userInputFirstName));
}
else
{
students.Add(new Student());
}
}
}
}
}
break;
}
Console.Write("Would you like to create a new user? (Y for yes, N for No): ");
userInput = Console.ReadLine();

if(userInput.ToLower() == "y")
{
//Reset values, even if they were never set.
userInputFirstName = null;
userInputLastName = null;
userInputMajor = null;
userInputGPA = -1;

while(true)
{
Console.Write("What is the first name of the student?: ");
userInput = Console.ReadLine();

if(userInput.Length > 0)
{
if(userInput == "\\")
{
goingBack = true;
break;
}

userInputFirstName = userInput;
break;
}

Console.WriteLine("\nInvalid entry. Please, enter a value.\n");
}
//Just a hint as to why I don't actually break down here.
//It'd be better to have everything happen in a central location.
//These if statements just send them to that central location.
//That way if I want to change how "goingBack" is handled,
//I only have to change it in one spot.

//It is also being checked for here because of the while loop above.
//The while loop has the potential of having a goingBack value come out
//of it. So, it's best to check right after we get out of it.
if (goingBack)
{
continue;
}


while(true)
{
Console.Write("What is the last name of the student?: ");
userInput = Console.ReadLine();

if (userInput.Length > 0)
{
if (userInput == "\\")
{
goingBack = true;
break;
}

userInputLastName = userInput;
break;
}

Console.WriteLine("\nInvalid entry. Please, enter a value.\n");
}
if (goingBack)
{
continue;
}


while (true)
{
Console.Write("What is the GPA of the student? (Negative numbers will result in a zeroed GPA): ");
userInput = Console.ReadLine();

if (userInput.Length > 0)
{
if (userInput == "\\")
{
goingBack = true;
break;
}

//If the parse was successful, break and move on to the next input.
//Else, retry again.
if (Single.TryParse(userInput, out userInputGPA))
{
break;
}

Console.WriteLine("\nInvalid entry. Please, only enter a number value.\n");
}
}
if (goingBack)
{
continue;
}

while(true)
{
Console.Write("What is the Major of the student?: ");
userInput = Console.ReadLine();

if (userInput.Length > 0)
{
if (userInput == "\\")
{
goingBack = true;
continue;
}

userInputMajor = userInput;
break;
}

Console.WriteLine("\nInvalid entry. Please, enter a value.\n");
}

//They have reached the end of the entry attempt. All values are entered. Create the student.
students.Add(new Student(userInputFirstName, userInputLastName, userInputGPA, userInputMajor));
}

//Don't do anything. Just go back.
if(userInput.ToLower() == "n")
{
break;
}

}
}

public static void DeleteAStudent()
{
//User-friendly dialog instructions
Console.WriteLine(@"
*** *** *** *** *** *** *** ***
Delete Students
Enter the requested values.
To exit, enter \
*** *** *** *** *** *** *** ***
");
if (students.Count > 0)
{
DisplayStudents();

while (running)
{
if (goingBack)
{
goingBack = false;
break;
}

Console.Write("Enter the number of the student you want to remove (Their numbers are to the left of their name): ");
userInput = Console.ReadLine();
int indexVal;

if (userInput == "\\")
{
goingBack = true;
continue;
}

if (Int32.TryParse(userInput, out indexVal) && indexVal >= 1 && indexVal <= students.Count())
{
Console.WriteLine(students[indexVal - 1].LastName + ", " + students[indexVal - 1].FirstName + " was deleted.");
students.RemoveAt(indexVal - 1);
}
else
{
Console.WriteLine("Invalid entry. Please, enter a numerical value only that is either 1 or greater and is presented on the students list.\n");
}
}
}
else
Console.WriteLine("No student data entered.");
}

public static void DisplayStudentData()
{
if (students.Count > 0)
DisplayStudents(true);
else
Console.WriteLine("No student data entered.");
}

public static void DisplayStudents(bool allData = false)
{
int counter = 0;
Console.WriteLine("\n\n***** STUDENTS LIST *****\n\n");
foreach(Student stu in students)
{
counter++;
Console.WriteLine(counter + ") " + stu.LastName + ", " + stu.FirstName);
if (allData)
Console.WriteLine("\t GPA: " + stu.GPA + " | Major: " + stu.Major);

}

Console.WriteLine("\n\n***** END OF LIST *****\n\n");
}

//Stop all switch statements from looping again.
public static void ExitProgram()
{
running = false;
}
}
}


P.S. The power at my College went out at the time of writing this. Somehow.. the computer itself didn't shut off. I'm quite happy. UPS, maybe? The monitor turned off but not the desktop itself.