I'm fairly new to programming and newer still to Java.
I'm trying to write some code that will take input from a user and create a new object (instance?) from it. The concept is adding and updating a student info system. So the user is presented a menu (made up of switch statements) and they choose an option and that int choice number calls the method that is supposed to add, update or view the student.
I have two classes: StudentSystem and Student. StudentSystem does most of the work. Student, obviously is the Class that defines the details of the student object we are to create.
import java.util.Scanner;
public class StudentSystem {
boolean exit = false;
public static void main(String[] args)
{
StudentSystem menu = new StudentSystem();
menu.runMenu();
}
public void runMenu()
{
printHeader();
while (!exit)
{
printOptions();
int choice = getInput();
executeChoice(choice);
}
}
public void printHeader()
{
System.out.println("+-------------------------------------- +");
System.out.println("| Welcome to the |");
System.out.println("| Student Admin System |");
System.out.println("+-------------------------------------- +");
}
public void printOptions()
{
System.out.println("\n1: Add Student");
System.out.println("2: Update Student");
System.out.println("3: View Student");
System.out.println("0: Exit");
}
private int getInput() {
Scanner keyInput = new Scanner(System.in);
int choice = -1; // Set to -1 so that the number is outside the number choice. This way it will not conflict.
while (choice < 0 || choice > 2) {
try {
System.out.print("\nPlease make a Selection: ");
choice = Integer.parseInt(keyInput.nextLine());
}
catch (NumberFormatException exception) {
System.out.println("You have made an invalid selection. Please try again.");
}
}
return choice;
}
private void executeChoice(int choice) { // Executes the choice provided.
switch (choice) {
case 0:
exit = true;
System.out.println("Thank you for using the Student Admin System.\nGoodbye! ");
break;
case 1:
addStudent();
break;
case 2:
// updateStudent();
case 3:
// viewStudent();
default:
System.out.println("Does not compute!");
}
}
public static void addStudent(){ /**/This is just a minor attempt at what method should go here.**
Student mystudent = new Student("Harry", 10,3.0, 'B');
System.out.println(mystudent);
}
}
Here is the Student Class:
public class Student {
private static Student newStudent;
String name;
int age;
double gpa;
char grade;
public Student(String name, int age, double gpa, char grade) {
this.name = name;
this.age = age;
this.gpa = gpa;
this.grade = grade;
}
}
Most of the code in StudentSystem can be ignored, but it might give some context. The stuck point is creating a method that is called when you choose an option under executeChoice that creates a student object that can be viewed and edited. The extra tricky pary is adding/manipulating the name, grade, major etc also through user input. I could just type it into the method like I did in the addStudent method, but that wouldn't be very helpful or dynamic.
I'm thinking i could use a method like:
public static void userChoice(Scanner keyInput) {
<some code>
}
But I'm not sure exactly how I would write it.
Hope it kinda makes sense what I'm trying to accomplish. Any advice would be greatly appriciated. I'm here to learn.