Answer on Question#37723- Programming, C#
1. You are going to implement a command-line student management system.
Users can type four commands (case insensitive):
- Create command to create new student record with 4 pieces of information separated by comma: student ID (string), name (string), major (string), GPA (floating point)
o For example: create:105,Alice,EECS,2.54
- Sort command to display all the student records in sorted order either by name or by GPA (the sorting criteria is case insensitive)
o For example: sort:name
- Search command to find a student record by the student ID
o For example: search:105
Solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
PrintResult pr = new PrintResult();
pr.Print();
Console.Read();
}
}
public class Student
{
public string ID;
public string name;
public string major;
public double GPA;
public void View()
{
Console.WriteLine(ID + " " + name + " " + major + " " + GPA);
}
}
}
class PrintResult
{
SortedList<string, Student> std;
public PrintResult()
{
std = new SortedList<string, Student>();
}
public void GetData()
{
Student stud = new Student();
Console.WriteLine("Enter the ID of the student:");
stud.ID = Console.ReadLine();
Console.WriteLine("Enter the name of the student:");
stud.name = Console.ReadLine();
Console.WriteLine("Enter the major of the student:");
stud.major = Console.ReadLine();
Console.WriteLine("Enter the GPA of the student:");
stud.GPA = Convert.ToDouble(Console.ReadLine());
std.Add(stud.ID, stud);
returnPrint();
}
public void Print()
{
Console.Clear();
Console.WriteLine("Select the menu item:");
Console.WriteLine("===================\n");
Console.WriteLine("1. Create command to create new student record");
Console.WriteLine("2. Sort command to display all the student records in sorted order either by name or by GPA");
Console.WriteLine("3. Search command to find a student record by the student ID");
Console.WriteLine("4. Show all students");
string sMenu;
sMenu = Console.ReadLine();
switch (sMenu)
{
case "1":
{ GetData(); break; }
case "2":
{ Sort(); break; }
case "3":
{ Find(); break; }
case "4":
{ AllView(); break; }
default:
{ Console.WriteLine("Wrong the chosen menu item!"); returnPrint();
break;
}
}
}
public void Find()
{
string s;
s = Console.ReadLine();
std[s].View();
returnPrint();
}
public void AllView()
{
foreach (string s in std.Keys)
{
std[s].View();
}
returnPrint();
}
public void Sort()
{
SortedList s = new SortedList();
Console.WriteLine("1.By Name " + "2. By GPA");
if (Console.ReadLine() == "1")
{
foreach (string sid in std.Keys)
{
s.Add(std[sid].name, std[sid]);
}
for (int i = 0; i < s.Count; i++)
{
Console.WriteLine("{0}", s.GetKey(i).ToString());
}
}
if (Console.ReadLine() == "2")
{
foreach (string sid in std.Keys)
{
s.Add(std[sid].GPA, std[sid]);
}
for (int i = 0; i < s.Count; i++)
{
Console.WriteLine("{0}", s.GetKey(i));
}
}
returnPrint();
}
public void returnPrint()
{
Console.WriteLine("Return in main menu?\n" + "1. Yes " + "2. No");
if (Console.ReadLine() == "1") { Print(); }
}
}You can also run the file in the attached file .exe