0% found this document useful (0 votes)
16 views6 pages

Student

Code program management student

Uploaded by

dothhe182087
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)
16 views6 pages

Student

Code program management student

Uploaded by

dothhe182087
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/ 6

package studentmanage;

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Student manager = new Student();
Scanner sc = new Scanner(System.in);

while (true) {
int choice = manager.displayMenu();
switch (choice) {
case 1:
manager.createStudent();
break;
case 2:
manager.findAndSortStudent();
break;
case 3:
manager.updateOrDeleteStudent();
break;
case 4:
manager.report();
break;
case 5:
System.out.println("Exiting...");
sc.close();
return;
}
}
}
}

package studentmanage;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;

class CheckValid {
public static int checkInputInt(Scanner sc, int min, int max) {
while (true) {
try {
int num = Integer.parseInt(sc.nextLine().trim());
if (num >= min && num <= max) return num;
System.out.print("Invalid input, enter number between " + min + "
and " + max + ": ");
} catch (NumberFormatException e) {
System.out.print("Invalid input, please enter a number: ");
}
}
}

public static String checkInputString(Scanner sc) {


return sc.nextLine().trim();
}

public static char checkUorD(Scanner sc) {


while (true) {
System.out.print("Enter 'U' to update or 'D' to delete: ");
String input = sc.nextLine().trim().toUpperCase();
if (input.equals("U") || input.equals("D")) return input.charAt(0);
System.out.println("Invalid input. Please enter 'U' or 'D'.");
}
}

public static String checkId(Scanner sc, ArrayList<Student> students) {


while (true) {
System.out.print("Enter student ID: ");
String id = sc.nextLine().trim();
if (Pattern.matches("^[A-Za-z0-9]+$", id)) {
boolean exists = students.stream().anyMatch(s ->
s.getId().equals(id));
if (!exists) return id;
System.out.println("ID already exists. Please enter a unique ID.");
} else {
System.out.println("Invalid ID format. Only alphanumeric characters
are allowed.");
}
}
}

public static String checkName(Scanner sc) {


while (true) {
System.out.print("Enter student name: ");
String name = sc.nextLine().trim();
if (Pattern.matches("^[A-Z][a-z]+( [A-Z][a-z]+)*$", name)) {
return name;
}
System.out.println("Invalid name format. Please enter in 'Nguyen Van A'
format.");
}
}

public static String checkCourse(Scanner sc) {


while (true) {
System.out.print("Enter course (Java, .Net, C/C++): ");
String course = sc.nextLine().trim();
if (course.equals("Java") || course.equals(".Net") ||
course.equals("C/C++")) {
return course;
}
System.out.println("Invalid course. Please enter Java, .Net, or C/C+
+.");
}
}

public static boolean checkYesNo(Scanner sc) {


while (true) {
System.out.print("Do you want to add more students? (Y/N): ");
String input = sc.nextLine().trim().toUpperCase();
if (input.equals("Y")) return true;
if (input.equals("N")) return false;
System.out.println("Invalid input, please enter Y or N.");
}
}
}

package studentmanage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

// Tao lop person de chua du lieu ve sinh vien


class Person {
protected String id;
protected String name;
public Person() {
}

public Person(String id, String name) {


this.id = id;
this.name = name;
}

public String getId() {


return id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}

// Doi tuong Student ke thua lop person


public class Student extends Person {
private int semester;
private String course;
private ArrayList<Student> students = new ArrayList<>();
private static final String[] COURSES = {"Java", ".Net", "C/C++"};
private Scanner sc = new Scanner(System.in);

public Student() {
super();
}

public Student(String id, String name, int semester, String course) {


super(id, name); // Gọi constructor của lớp Person
this.semester = semester;
this.course = course;
}
public int getSemester() {
return semester;
}

public String getCourse() {


return course;
}

public void setSemester(int semester) {


this.semester = semester;
}

public void setCourse(String course) {


this.course = course;
}

public void createStudent() {


while (true) {
System.out.println("Enter student details (or type 'done' to stop):");

String id = CheckValid.checkId(sc, students);


if (id.equalsIgnoreCase("done"))
break;

String name = CheckValid.checkName(sc);


if (name.equalsIgnoreCase("done"))
break;

System.out.print("Enter semester: ");


int semester = CheckValid.checkInputInt(sc, 1, 10);

String course = CheckValid.checkCourse(sc);


if (course.equalsIgnoreCase("done"))
break;

students.add(new Student(id, name, semester, course));


System.out.println("Student added successfully!");

if (students.size() >= 10) {


if (!CheckValid.checkYesNo(sc))
break;
}
}
}

public void findAndSortStudent() {


if (students.isEmpty()) {
System.out.println("The student list is empty.");
return;
}
System.out.print("Enter student ID or name to search: ");
String input = sc.nextLine().trim();
ArrayList<Student> foundStudents = new ArrayList<>();
boolean isIdSearch = input.matches("[a-zA-Z0-9]+");
for (Student s : students) {
if (isIdSearch) {
if (s.getId().equalsIgnoreCase(input)) {
foundStudents.add(s);
break;
}
} else {
if (s.getName().toLowerCase().contains(input.toLowerCase())) {
foundStudents.add(s);
}
}
}
if (foundStudents.isEmpty()) {
System.out.println("No students found.");
return;
}
Collections.sort(foundStudents, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
System.out.println("List of students found: ");
for (Student s : foundStudents) {
System.out.println(s);
}
}

public void updateOrDeleteStudent() {


System.out.print("Enter student ID to update or delete: ");
String id = sc.nextLine().trim();
Student student = null;
for (Student s : students) {
if (s.getId().equals(id)) {
student = s;
break;
}
}
if (student == null) {
System.out.println("Student not found.");
return;
}
char choice = CheckValid.checkUorD(sc);
if (choice == 'U') {
System.out.println("Updating student details...");
student.setName(CheckValid.checkName(sc));
student.setSemester(CheckValid.checkInputInt(sc, 1, 10));
student.setCourse(CheckValid.checkCourse(sc));
System.out.println("Student updated successfully!");
} else if (choice == 'D') {
students.remove(student);
System.out.println("Student deleted successfully!");
}
}

public int displayMenu() {


System.out.println("WELCOME TO STUDENT MANAGEMENT");
System.out.println("1. Create");
System.out.println("2. Find and Sort");
System.out.println("3. Update/Delete");
System.out.println("4. Report");
System.out.println("5. Exit");
System.out.print("Choose an option: ");

return CheckValid.checkInputInt(sc, 1, 5);


}

public void report() {


ArrayList<String> reportList = new ArrayList<>();
for (Student s : students) {
String reportLine = s.getName() + " | " + s.getCourse();
if (!reportList.contains(reportLine)) {
long count = students.stream()
.filter(st -> st.getName().equals(s.getName())
&& st.getCourse().equals(s.getCourse())
&& st.getSemester() == s.getSemester())
.count();
System.out.println(reportLine + " | " + count);
reportList.add(reportLine);
}
}
}

@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Semester: " + semester + ",
Course: " + course;
}
}

You might also like