Write a menu based program to maintain student records. Your program should take
ID: 3591260 • Letter: W
Question
Write a menu based program to maintain student records. Your program should take the following inputs:
1. Student first name (max. 20 characters)
2. Student last name, (max. 20 characters)
3. Student scores (float/double), eg. 85.4
Your program should be able to take records of a minimum of 5 students and maximum of 15 students.
After taking the records, you should provide 6 functionalities to the user.
1. Print records – prints records of all students
2. Search by first name – prints record of the student with a given first name. If there are multiple students with the same first name, print records for all of them.
3. Search by last name – prints record of the student with a given last name. If there are multiple students with the same last name, print records for all of them.
4. Sort by score – sort the records of students according to their scores, and then print the sorted records.
5. Sort by last name – sort the records of students according to their names alphabetically, and then print the sorted records.
6. Find Max score – prints record of the student with the maximum score. If there are multiple students with the same maximum score, print records for all of them.
7. Find Min score – prints record of the student with the minimum score. If there are multiple students with the same minimum score, print records for all of them. .
8. Exit the program – terminate on a specific input from the user. Let that specific input be an integer of value 0.
You should print the record in the following format:
First Name: firstname 1, Last Name: lastname 1, Score: score 1
First Name: firstname 2, Last Name: lastname 2, Score: score 2
.
.
.
You should write each functionality from 1-5 in separate functions. You should provide a menu to the user as following:
For inputs: Please indicate number of records you want to enter (min 5, max 15):
#of records
After user gives the number of records, you should inform the user how to enter the records:
Please input records of students (enter a new line after each record), with following format first name last name score
After user gives the inputs for the records, inform the user about the functionalities:
Print records (press 1)
Search by first name (press 2)
Search by last name (press 3)
Sort by score (press 4)
Sort by last name (press 5)
Find Max Score (press 6)
Find Min Score (press 7)
Exit the program (press 0)
After user chooses functionality, your program performs that and provides this menu again to select another functionality. This goes on until user presses 0.
Explanation / Answer
Please check the below code and execute it. I have used two classes only.
StudentPortal.java
package com.Students;
import java.util.*;
import java.util.Scanner;
public class StudentPortal {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int input=8;
int numOfRec=0;
while(numOfRec<=0){
System.out.println("Please indicate number of records you want to enter (min 5, max 15):");
numOfRec = scanner.nextInt();
}
int i=0;
String studentFirstName="";
String studentLastName="";
double score=0.0;
Student s1;
List<Student> studentList = new ArrayList<Student>();
while(i<numOfRec){
boolean fname=true;
while(studentFirstName.isEmpty()){
if(fname){
System.out.println("Enter Student First Name:");
fname=false;
}
studentFirstName = scanner.nextLine();
}
while(studentLastName.isEmpty()){
if(studentLastName.isEmpty())
System.out.println("Enter Student Last Name:");
studentLastName = scanner.nextLine();
}
while(score<=0){
if(score==0.0)
System.out.println("Enter Student Score:");
score = scanner.nextDouble();
}
s1 = new Student(studentFirstName, studentLastName, score);
studentList.add(s1);
i++;
studentFirstName="";
studentLastName="";
score=0.0;
}
while(input!=0){
System.out.println(" ");
System.out.println("Print records (press 1)");
System.out.println("Search by first name (press 2)");
System.out.println("Search by last name (press 3)");
System.out.println("Sort by score (press 4)");
System.out.println("Sort by LastName (press 4)");
System.out.println("Find Max Score (press 6)");
System.out.println("Find Min Score (press 7)");
System.out.println("Exit the program (press 0)");
input=scanner.nextInt();
if(input==1){
System.out.println("Display Students");
for(Student s:studentList){
System.out.println(s.toString());
}
}
if(input==2){
System.out.println(" ");
String m ="";
boolean fname=true;
while(m.isEmpty()){
if(fname){
System.out.println("Enter Student FirstName:");
fname=false;
}
m = scanner.nextLine();
}
boolean check=false;
for(Student s:studentList){
if(m.equals(s.getFirstName())){
check = true;
System.out.println("Search Results:"+s.toString());
}
}
if(!check){
System.out.println("No results found for "+m);
}
}
if(input==3){
System.out.println(" ");
String m ="";
boolean lname=true;
while(m.isEmpty()){
if(lname){
System.out.println("Enter Student LastName:");
lname=false;
}
m = scanner.nextLine();
}
boolean check=false;
for(Student s:studentList){
if(m.equals(s.getLastName())){
check = true;
System.out.println("Search Results:"+s.toString());
}
}
if(!check){
System.out.println("No results found for "+m);
}
}
if(input==4){
System.out.println(" ");
List<Student> sortedlist = studentList;
Collections.sort(sortedlist, new Comparator<Student>() {
public int compare(Student s, Student s2) {
return (int) (s.getScore() - s2.getScore());
}
});
System.out.println("Sorted By Score:");
for(Student s:sortedlist){
System.out.println(s.toString());
}
}
if(input==5){
System.out.println(" ");
List<Student> sortedlist = studentList;
Collections.sort(sortedlist, new Comparator<Student>() {
public int compare(Student s, Student s2) {
String fruitName1 = s.getLastName().toUpperCase();
String fruitName2 = s2.getLastName().toUpperCase();
//ascending order
return fruitName1.compareTo(fruitName2);
}
});
System.out.println("Sorted By Score:");
for(Student s:sortedlist){
System.out.println(s.toString());
}
}
if(input==6){
System.out.println(" ");
List<Student> sortedlist = studentList;
Collections.sort(sortedlist, new Comparator<Student>() {
public int compare(Student s, Student s2) {
return (int) (s.getScore() - s2.getScore());
}
});
Student m1 = sortedlist.get(sortedlist.size()-1);
System.out.println(m1.getFirstName()+" has Max Score::"+m1.getScore());
}
if(input==7){
System.out.println(" ");
List<Student> sortedlist = studentList;
Collections.sort(sortedlist, new Comparator<Student>() {
public int compare(Student s, Student s2) {
return (int) (s.getScore() - s2.getScore());
}
});
Student m1 = sortedlist.get(0);
System.out.println(m1.getFirstName()+" has Min Score::"+m1.getScore());
}
if(input>8){
System.out.println("Invalid choics try again!");
}
}
System.out.println("Thank You for Using");
}
}
Student.java
package com.Students;
public class Student {
private String firstName;
private String lastName;
private double score;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student(String firstName, String lastName, double score) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
@Override
public String toString() {
return "First Name:" + firstName + " Last Name:" + lastName
+ " Score:" + score + "";
}
}
Any queries please comment here.
Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.