In: Computer Science
Design a complete system to allow manipulation of Students (via a designed and developed Student class). in JAVA
It Includes:
1) A Student Class which stores info about the student: name and a generated ID#.
2) User interface (via Scanner) that prompts user for the following:
a) New Student -- user enters the name of the student, the system (your student class will generate the ID#)
b) View Student via Name - the user enters name (not case sens., and partial search works..) outputs full name of student and ID# of student
c) Delete Student - the user enters name (not case sens.) and system deletes this Student from System.
d)View All - displays all students in System and associated ID#s
e) quit -- quits the system
Solution of the above problem is provided below.
Code comments and annotations are provided for better understanding of the code. for moew help write into comment section of the code.
Student.java
public class Student {
private String name;
private int id;
/**
* @param name
*/
public Student(String name) {
super();
this.name = name;
/* Generating random Id in range 1-999 using Math.random();*/
id = (int) Math.random() * (999 - 1 + 1) + 1;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
}
Main.java <Driver code for programe>
import java.util.*;
public class Main {
static ArrayList<Student> students;
public static void main(String[] args) {
// initialising students arraylist to stire student objects
students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
// taking choice from user
int choice;
do {
// calling menu
userInterface();
choice = sc.nextInt();
switch (choice) {
case 1: {
/*creating a new student object by taking student name from the console*/
System.out.println("Enter Student name: ");
String studentName = sc.next();
// creating student object
Student newStudent = new Student(studentName);
// adding student to the arraylist of students
students.add(newStudent);
break;
}
case 2: {
/* View info of student after entering student name*/
System.out.println("Enter name to view info: ");
String enteredName = sc.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equalsIgnoreCase(enteredName))
;
System.out.println(students.get(i).toString());
}
break;
}
case 3: {
/*Delete student from arraylist after searching that element from the list*/
System.out.println("Enter name to delete: ");
String enteredName = sc.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equalsIgnoreCase(enteredName));
students.remove(i);
}
break;
}
case 4: {
System.out.println("All students " + students.size());
for (Student student : students) {
System.out.println(student.toString());
}
break;
}
case 5: {
System.out.println("\nExiting...\n");
System.exit(0);
break;
}
default:
System.out.println("\nInvalid selection!\n");
}
} while (choice != 5);
}
private static void userInterface() {
System.out.print("\n1. Add New Student\n" + "2. View Student Via Name\n" + "3. Delete Student \n"
+ "4. View all Students \n" + "5. Quit.\n" + "Your selection:");
}
}
Output:
End of Solution