The program must follow these registration business rules: No registration of ot
ID: 3870650 • Letter: T
Question
The program must follow these registration business rules:
No registration of other courses not displayed by the program.
No registration more than once for the same course.
No registration for more than nine credit hours (e.g., no more than three courses).
The program validates the user integer menu selection, and if valid, registers the student for the selected course. Otherwise, the program outputs an error message. The program then outputs the current list of registered classes and asks the user if he or she wants to register for another course. You have been hired to debug and fix these program errors so that the program will run and produce the correct output.
The C# code to be corrected is below:
namespace ConsoleRegisterStudent
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
string yesOrNo = "";
System.Console.WriteLine("Teacher's Copy");
do
{
WritePrompt();
choice = Convert.ToInt32(Console.ReadLine());
switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit))
{
case -1:
Console.WriteLine("Your entered selection {0} is not a recognized course.", choice);
break;
case -2:
Console.WriteLine("You have already registerd for this {0} course.", ChoiceToCourse(choice));
break;
case -3:
Console.WriteLine("You can not register for more than 9 credit hours.");
break;
case 0:
Console.WriteLine("Registration Confirmed for course {0}.", ChoiceToCourse(choice));
totalCredit += 3;
if (firstChoice == 0)
firstChoice = choice;
else if (secondChoice == 0)
secondChoice = choice;
else if (thirdChoice == 0)
thirdChoice = choice;
break;
}
WriteCurrentRegistration(firstChoice, secondChoice, thirdChoice);
Console.Write(" Do you want to try again? (Y|N)? : ");
yesOrNo = (Console.ReadLine()).ToUpper();
} while (yesOrNo == "Y");
Console.WriteLine("Thank you for registering with us");
}
void WritePrompt()
{
Console.WriteLine("Please select a course for which you want to register by typing the number inside []");
Console.WriteLine("[1]IT 145 [2]IT 200 [3]IT 201 [4]IT 270 [5]IT 315 [6]IT 328 [7]IT 330");
Console.Write("Enter your choice : ");
}
int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit)
{
if (choice < 1 || choice > 70)
return -1;
else if (choice == firstChoice && choice == secondChoice && choice == thirdChoice)
return -2;
else if (totalCredit > 9)
return -3;
return -4;
}
void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("You are currently registered for {0}", ChoiceToCourse(firstChoice));
else if (thirdChoice == 0)
Console.WriteLine("You are currently registered for {0}, {1}", ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice));
else
Console.WriteLine("You are currently registered for {0}, {1}, {2}", ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice), ChoiceToCourse(thirdChoice));
}
string ChoiceToCourse(int choice)
{
string course = "";
switch (choice)
{
case 1:
course = "IT 145";
break;
case 2:
course = "IT 200";
break;
case 3:
course = "IT 201";
break;
case 4:
course = "IT 270";
break;
case 5:
course = "IT 315";
break;
case 6:
course = "IT 328";
break;
case 7:
course = "IT 330";
break;
default:
break;
}
return course;
}
}
}
Sample outputs:
When a student registers for a second class it the program will keep count of the courses registered for.
Below is an error output if the same course is attempted to be registered for twice:
Below is the error output if a student tries to register for more than 3 courses:
Lastly below is an error output if the student inputs something other than the menu choices of 1 - 7.
Please select a course for which you want to register by typing the number inside ] [1]IT 145 2] IT 200 [31IT 201 [4]IT 270 5] IT 315 [6] IT 328 7]IT 330 Enter your choice 1 Registration Confirmed for course IT 145. You are currently registered for IT 145 Do you want to try again (YIN?Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleRegisterStudent
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
string yesOrNo = "";
System.Console.WriteLine("Teacher's Copy");
do
{
WritePrompt();
choice = Convert.ToInt32(Console.ReadLine());
switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit))
{
case -1:
Console.WriteLine("Your entered selection {0} is not a recognized course.", choice);
break;
case -2:
Console.WriteLine("You have already registerd for this {0} course.", ChoiceToCourse(choice));
break;
case -3:
Console.WriteLine("You can not register for more than 9 credit hours.");
break;
case 0:
Console.WriteLine("Registration Confirmed for course {0}.", ChoiceToCourse(choice));
totalCredit += 3;
if (firstChoice == 0)
firstChoice = choice;
else if (secondChoice == 0)
secondChoice = choice;
else if (thirdChoice == 0)
thirdChoice = choice;
break;
}
WriteCurrentRegistration(firstChoice, secondChoice, thirdChoice);
Console.Write(" Do you want to try again? (Y|N)? : ");
yesOrNo = (Console.ReadLine()).ToUpper();
} while (yesOrNo == "Y");
Console.WriteLine("Thank you for registering with us");
}
void WritePrompt()
{
Console.WriteLine("Please select a course for which you want to register by typing the number inside []");
Console.WriteLine("[1]IT 145 [2]IT 200 [3]IT 201 [4]IT 270 [5]IT 315 [6]IT 328 [7]IT 330");
Console.Write("Enter your choice : ");
}
int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit)
{
if (choice < 1 || choice > 7)
return -1;
else if (choice == firstChoice && choice == secondChoice && choice == thirdChoice)
return -2;
else if (totalCredit >= 9)
return -3;
return 0;
}
void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("You are currently registered for {0}", ChoiceToCourse(firstChoice));
else if (thirdChoice == 0)
Console.WriteLine("You are currently registered for {0}, {1}", ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice));
else
Console.WriteLine("You are currently registered for {0}, {1}, {2}", ChoiceToCourse(firstChoice), ChoiceToCourse(secondChoice), ChoiceToCourse(thirdChoice));
}
string ChoiceToCourse(int choice)
{
string course = "";
switch (choice)
{
case 1:
course = "IT 145";
break;
case 2:
course = "IT 200";
break;
case 3:
course = "IT 201";
break;
case 4:
course = "IT 270";
break;
case 5:
course = "IT 315";
break;
case 6:
course = "IT 328";
break;
case 7:
course = "IT 330";
break;
default:
break;
}
return course;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.