0% found this document useful (0 votes)
33 views

Student List

The document defines a StudentList class that uses a HashMap to store student records with ID numbers as keys and names as values. It prompts the user to input 3 initial records. The program then displays a menu to allow the user to add, replace, delete, display, or clear student records from the map. Based on the user's selection, it performs the corresponding operation and provides feedback on the outcome.

Uploaded by

samanthakylearg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Student List

The document defines a StudentList class that uses a HashMap to store student records with ID numbers as keys and names as values. It prompts the user to input 3 initial records. The program then displays a menu to allow the user to add, replace, delete, display, or clear student records from the map. Based on the user's selection, it performs the corresponding operation and provides feedback on the outcome.

Uploaded by

samanthakylearg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package studentlist;

import java.util.*;

public class StudentList {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Integer, String> students = new HashMap<>();
String name;
int number;

for(int i = 1;i<=3;i++){
System.out.print("\nEnter Student ID Number "+i+": ");
number = sc.nextInt();
sc.nextLine();
System.out.print("Enter first name "+i+": ");
name = sc.nextLine();
students.put(number, name);
}
System.out.println("\n---STUDENT LIST---");
for (Map.Entry e : students.entrySet()) {
System.out.println(e.getKey()+" | "+e.getValue());
}

while(true){
System.out.println("\nChoose the number you want to execute:");
System.out.println("[1] Enter new record");
System.out.println("[2] Replace record");
System.out.println("[3] Delete a record");
System.out.println("[4] Display a record");
System.out.println("[5] Display all records");
System.out.println("[6] Delete all records");
System.out.println("[7] Exit");

System.out.print("\nEnter the number you want to execute: ");


int choose = sc.nextInt();

switch (choose) {
case 1:
System.out.print("\nEnter Student ID Number: ");
number = sc.nextInt();
sc.nextLine();

if(students.containsKey(number)){
System.out.println("\nRECORD ALREADY EXIST.");
}
else{
System.out.print("Enter first name: ");
name = sc.nextLine();
students.put(number, name);
System.out.println("\nNEW RECORD HAS BEEN ADDED.");
}
break;
case 2:
System.out.print("\nEnter the ID you want to replace: ");
number = sc.nextInt();
sc.nextLine();
if(students.containsKey(number)){
System.out.print("Enter new name: ");
name = sc.nextLine();
students.replace(number, name);
System.out.println("\nNAME HAS BEEN REPLACED.");
}
else{
System.out.println("\nNO RECORD FOUND.");
}
break;
case 3:
System.out.print("\nEnter the Student ID Number to be deleted:
");
number = sc.nextInt();

if(students.containsKey(number)){
students.remove(number);
System.out.println("\nTHE RECORD HAS BEEN DELETED.");
}
else{
System.out.println("\nNO RECORD FOUND.");
}

break;
case 4:
System.out.print("\nEnter the ID number you want to check: ");
number = sc.nextInt();
sc.nextLine();

if(students.containsKey(number)){
System.out.println("\nRECORD FOUND:
"+students.get(number));
}else{
System.out.println("\nNO RECORD FOUND.");
}
break;
case 5:
System.out.println("\n---STUDENTS LIST---");

if(!students.isEmpty()){
for (Map.Entry e : students.entrySet()) {
System.out.println(e.getKey()+" | "+e.getValue());
}
}
else{
System.out.println("[-]");
}
break;
case 6:
System.out.println("\nDo you want to delete all records?");
System.out.print("\"Y/N\": ");
char let = sc.next().toLowerCase().charAt(0);

if (let == 'y'){
students.clear();
System.out.println("\nALL RECORDS HAS BEEN DELETED.");
}
break;
case 7:
System.exit(0);
break;
default:
break;
}
}
}
}

You might also like