Register for Courses Java












0















I have been working on a code that registers students for courses. I was given a teachers copy and needed to write the code for the validateChoice method. I have it completed but I am still getting two errors. The program is recognizing 0 as a duplicate instead of an invalid course number, and is allowing the user to duplicate twice before throwing an invalid message. There should be no duplicates allowed. Any ideas on why this would happen? I have already turned this in because it runs correctly with the course codes I was given for the assignment, I just want to know for personal reference. Thanks!



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u6a1_consoleregisterforcourse;

import java.util.Scanner;

/**
*
* @author omora
*/
public class U6A1_ConsoleRegisterForCourse {

/**
* @param args the command line arguments
*/
public static void main(String args) {
// TODO code application logic here


System.out.println("Kim's Copy");

Scanner input = new Scanner(System.in);

//choice is the current menu selection
//firstChoice is the first menu selection mande by the user
//secondChoice is the second menu selection mande by the user
//thirdChoice is the third menu selection mande by the user
// a choice of 0 means the choice has not been made yet
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
String yesOrNo = "";



do {

choice = getChoice(input);

switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit)) {
case -1:
System.out.println("**Invalid** - Your selection of " + choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " + ChoiceToCourse(choice) + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " + 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);

System.out.print("nDo you want to try again? (Y|N)? : ");

yesOrNo = input.next().toUpperCase();

} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");

}


public static int getChoice(Scanner input) {
System.out.println("Please type the number inside the to register for a course");
System.out.println("[1]IT4782n[2]IT4784n[3]IT4786n[4]IT4789n[5]IT2230n[6]IT3345n[7]IT3349");
System.out.print("Enter your choice : ");
return (input.nextInt());

}

//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {

// TO DO - Add Code to:
// Validate user menu selection (the int choice method arguement)
// against the given registration business rules
int result = 0;


if (choice <=0 ||choice >7){
result = -1;
}

if(choice==firstChoice||choice == secondChoice||choice == thirdChoice) {
result = -2;
}
if(totalCredit == 3) {
result = 0;
}

if (totalCredit > 9) {
result = -3;
}

return result;
}

public static void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice) {

if (firstChoice == 0)
System.out.println("Current course registration: { none } " );
else if (secondChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) + " }" );
else if (thirdChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + " }");
else
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + ", " + ChoiceToCourse(thirdChoice) + " }");

}

public static String ChoiceToCourse(int choice) {
String course = "";
switch (choice)
{
case 1:
course = "IT4782";
break;
case 2:
course = "IT4784";
break;
case 3:
course = "IT4786";
break;
case 4:
course = "IT4789";
break;
case 5:
course = "IT2230";
break;
case 6:
course = "IT3345";
break;
case 7:
course = "IT3349";
break;
default:
break;
}
return course;
}


}









share|improve this question


















  • 1





    If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

    – Makoto
    Nov 24 '18 at 19:35











  • You should follow the Java Naming Conventions: variable and method names always start with lowercase.

    – MC Emperor
    Nov 24 '18 at 19:38
















0















I have been working on a code that registers students for courses. I was given a teachers copy and needed to write the code for the validateChoice method. I have it completed but I am still getting two errors. The program is recognizing 0 as a duplicate instead of an invalid course number, and is allowing the user to duplicate twice before throwing an invalid message. There should be no duplicates allowed. Any ideas on why this would happen? I have already turned this in because it runs correctly with the course codes I was given for the assignment, I just want to know for personal reference. Thanks!



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u6a1_consoleregisterforcourse;

import java.util.Scanner;

/**
*
* @author omora
*/
public class U6A1_ConsoleRegisterForCourse {

/**
* @param args the command line arguments
*/
public static void main(String args) {
// TODO code application logic here


System.out.println("Kim's Copy");

Scanner input = new Scanner(System.in);

//choice is the current menu selection
//firstChoice is the first menu selection mande by the user
//secondChoice is the second menu selection mande by the user
//thirdChoice is the third menu selection mande by the user
// a choice of 0 means the choice has not been made yet
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
String yesOrNo = "";



do {

choice = getChoice(input);

switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit)) {
case -1:
System.out.println("**Invalid** - Your selection of " + choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " + ChoiceToCourse(choice) + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " + 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);

System.out.print("nDo you want to try again? (Y|N)? : ");

yesOrNo = input.next().toUpperCase();

} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");

}


public static int getChoice(Scanner input) {
System.out.println("Please type the number inside the to register for a course");
System.out.println("[1]IT4782n[2]IT4784n[3]IT4786n[4]IT4789n[5]IT2230n[6]IT3345n[7]IT3349");
System.out.print("Enter your choice : ");
return (input.nextInt());

}

//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {

// TO DO - Add Code to:
// Validate user menu selection (the int choice method arguement)
// against the given registration business rules
int result = 0;


if (choice <=0 ||choice >7){
result = -1;
}

if(choice==firstChoice||choice == secondChoice||choice == thirdChoice) {
result = -2;
}
if(totalCredit == 3) {
result = 0;
}

if (totalCredit > 9) {
result = -3;
}

return result;
}

public static void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice) {

if (firstChoice == 0)
System.out.println("Current course registration: { none } " );
else if (secondChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) + " }" );
else if (thirdChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + " }");
else
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + ", " + ChoiceToCourse(thirdChoice) + " }");

}

public static String ChoiceToCourse(int choice) {
String course = "";
switch (choice)
{
case 1:
course = "IT4782";
break;
case 2:
course = "IT4784";
break;
case 3:
course = "IT4786";
break;
case 4:
course = "IT4789";
break;
case 5:
course = "IT2230";
break;
case 6:
course = "IT3345";
break;
case 7:
course = "IT3349";
break;
default:
break;
}
return course;
}


}









share|improve this question


















  • 1





    If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

    – Makoto
    Nov 24 '18 at 19:35











  • You should follow the Java Naming Conventions: variable and method names always start with lowercase.

    – MC Emperor
    Nov 24 '18 at 19:38














0












0








0








I have been working on a code that registers students for courses. I was given a teachers copy and needed to write the code for the validateChoice method. I have it completed but I am still getting two errors. The program is recognizing 0 as a duplicate instead of an invalid course number, and is allowing the user to duplicate twice before throwing an invalid message. There should be no duplicates allowed. Any ideas on why this would happen? I have already turned this in because it runs correctly with the course codes I was given for the assignment, I just want to know for personal reference. Thanks!



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u6a1_consoleregisterforcourse;

import java.util.Scanner;

/**
*
* @author omora
*/
public class U6A1_ConsoleRegisterForCourse {

/**
* @param args the command line arguments
*/
public static void main(String args) {
// TODO code application logic here


System.out.println("Kim's Copy");

Scanner input = new Scanner(System.in);

//choice is the current menu selection
//firstChoice is the first menu selection mande by the user
//secondChoice is the second menu selection mande by the user
//thirdChoice is the third menu selection mande by the user
// a choice of 0 means the choice has not been made yet
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
String yesOrNo = "";



do {

choice = getChoice(input);

switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit)) {
case -1:
System.out.println("**Invalid** - Your selection of " + choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " + ChoiceToCourse(choice) + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " + 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);

System.out.print("nDo you want to try again? (Y|N)? : ");

yesOrNo = input.next().toUpperCase();

} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");

}


public static int getChoice(Scanner input) {
System.out.println("Please type the number inside the to register for a course");
System.out.println("[1]IT4782n[2]IT4784n[3]IT4786n[4]IT4789n[5]IT2230n[6]IT3345n[7]IT3349");
System.out.print("Enter your choice : ");
return (input.nextInt());

}

//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {

// TO DO - Add Code to:
// Validate user menu selection (the int choice method arguement)
// against the given registration business rules
int result = 0;


if (choice <=0 ||choice >7){
result = -1;
}

if(choice==firstChoice||choice == secondChoice||choice == thirdChoice) {
result = -2;
}
if(totalCredit == 3) {
result = 0;
}

if (totalCredit > 9) {
result = -3;
}

return result;
}

public static void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice) {

if (firstChoice == 0)
System.out.println("Current course registration: { none } " );
else if (secondChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) + " }" );
else if (thirdChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + " }");
else
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + ", " + ChoiceToCourse(thirdChoice) + " }");

}

public static String ChoiceToCourse(int choice) {
String course = "";
switch (choice)
{
case 1:
course = "IT4782";
break;
case 2:
course = "IT4784";
break;
case 3:
course = "IT4786";
break;
case 4:
course = "IT4789";
break;
case 5:
course = "IT2230";
break;
case 6:
course = "IT3345";
break;
case 7:
course = "IT3349";
break;
default:
break;
}
return course;
}


}









share|improve this question














I have been working on a code that registers students for courses. I was given a teachers copy and needed to write the code for the validateChoice method. I have it completed but I am still getting two errors. The program is recognizing 0 as a duplicate instead of an invalid course number, and is allowing the user to duplicate twice before throwing an invalid message. There should be no duplicates allowed. Any ideas on why this would happen? I have already turned this in because it runs correctly with the course codes I was given for the assignment, I just want to know for personal reference. Thanks!



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u6a1_consoleregisterforcourse;

import java.util.Scanner;

/**
*
* @author omora
*/
public class U6A1_ConsoleRegisterForCourse {

/**
* @param args the command line arguments
*/
public static void main(String args) {
// TODO code application logic here


System.out.println("Kim's Copy");

Scanner input = new Scanner(System.in);

//choice is the current menu selection
//firstChoice is the first menu selection mande by the user
//secondChoice is the second menu selection mande by the user
//thirdChoice is the third menu selection mande by the user
// a choice of 0 means the choice has not been made yet
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
String yesOrNo = "";



do {

choice = getChoice(input);

switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit)) {
case -1:
System.out.println("**Invalid** - Your selection of " + choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " + ChoiceToCourse(choice) + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " + 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);

System.out.print("nDo you want to try again? (Y|N)? : ");

yesOrNo = input.next().toUpperCase();

} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");

}


public static int getChoice(Scanner input) {
System.out.println("Please type the number inside the to register for a course");
System.out.println("[1]IT4782n[2]IT4784n[3]IT4786n[4]IT4789n[5]IT2230n[6]IT3345n[7]IT3349");
System.out.print("Enter your choice : ");
return (input.nextInt());

}

//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {

// TO DO - Add Code to:
// Validate user menu selection (the int choice method arguement)
// against the given registration business rules
int result = 0;


if (choice <=0 ||choice >7){
result = -1;
}

if(choice==firstChoice||choice == secondChoice||choice == thirdChoice) {
result = -2;
}
if(totalCredit == 3) {
result = 0;
}

if (totalCredit > 9) {
result = -3;
}

return result;
}

public static void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice) {

if (firstChoice == 0)
System.out.println("Current course registration: { none } " );
else if (secondChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) + " }" );
else if (thirdChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + " }");
else
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + ", " + ChoiceToCourse(thirdChoice) + " }");

}

public static String ChoiceToCourse(int choice) {
String course = "";
switch (choice)
{
case 1:
course = "IT4782";
break;
case 2:
course = "IT4784";
break;
case 3:
course = "IT4786";
break;
case 4:
course = "IT4789";
break;
case 5:
course = "IT2230";
break;
case 6:
course = "IT3345";
break;
case 7:
course = "IT3349";
break;
default:
break;
}
return course;
}


}






java methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 19:31









Kimberly BrandKimberly Brand

11




11








  • 1





    If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

    – Makoto
    Nov 24 '18 at 19:35











  • You should follow the Java Naming Conventions: variable and method names always start with lowercase.

    – MC Emperor
    Nov 24 '18 at 19:38














  • 1





    If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

    – Makoto
    Nov 24 '18 at 19:35











  • You should follow the Java Naming Conventions: variable and method names always start with lowercase.

    – MC Emperor
    Nov 24 '18 at 19:38








1




1





If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

– Makoto
Nov 24 '18 at 19:35





If I had to hazard a guess - it doesn't look like you're storing state anywhere...that is to say, your application doesn't actually keep track of anything it's written before. I'd need to actually run the application to see why it's doing this, but I would also recommend that you look into an IDE like IntelliJ to debug and trace what's going on in your application, too.

– Makoto
Nov 24 '18 at 19:35













You should follow the Java Naming Conventions: variable and method names always start with lowercase.

– MC Emperor
Nov 24 '18 at 19:38





You should follow the Java Naming Conventions: variable and method names always start with lowercase.

– MC Emperor
Nov 24 '18 at 19:38












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461680%2fregister-for-courses-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461680%2fregister-for-courses-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks