[learn Java notes from scratch] student management system

Keywords: Java

You can pay attention to the author's account and the Java notebook from scratch. You can also go to the author's blog Garden to learn from the catalog. This film will be based on the black horse programmer job class video for learning and data sharing, and take notes and their own views. Welcome to study and discuss together.

[learn Java notes from scratch] directory

Student management system has always been a link that can not be skipped in the process of university learning, because this system includes a lot of knowledge, which is a good starting project for beginners.

Demand:
1. Student attributes include student number, name, age and address
2. There should be a main interface
3. Students can be added. The student number is unique and cannot be added repeatedly
4. All students can be viewed
5. Students can be deleted according to student number
6. Students can be found by student number

Realization:

package com.tyust.studentmanagement;

public class Student {
	
	//Member variables
	private String id;
	private String name;
	private String age;
	private String address;
		
	
	//get, set method
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	//Non parametric structure
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	//Parametric structure
	public Student(String id, String name, String age, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
}
package com.tyust.studentmanagement;

import java.util.ArrayList;
import java.util.Scanner;

/*
 * 
 */
public class StudentAction {
	public static void main(String[] args) {

		// Create a collection
		ArrayList<Student> stu = new ArrayList<Student>();

		// Main interface, while(true) is a dead cycle
		while (true) {
			System.out.println("**********Welcome to student management system**********");
			System.out.println("To add student information, press 1");
			System.out.println("To view student information, press 2");
			System.out.println("To delete student information, press 3");
			System.out.println("To change student information, press 4");
			System.out.println("To exit, press 5");
			System.out.println("Please enter the action you need:");

			// Input from keyboard
			Scanner sc = new Scanner(System.in);
			// Receive
			int choice = sc.nextInt();

			// switch selection
			switch (choice) {
			case 1:
				addStudent(stu);
				break;
			case 2:
				travelStudent(stu);
				break;
			case 3:
				deleteStudent(stu);
				break;
			case 4:
				modifyStudent(stu);
				break;
			// This place uses case penetration, i.e. case 5 has no content and directly penetrates to the next case
			case 5:
			default:
				System.out.println("Thank you for your use!");
				// Shut down the virtual machine and end the program
				System.exit(0);

			}
		}
	}

	// Increase student
	public static void addStudent(ArrayList<Student> stu) {

		// Keyboard entry
		Scanner sc = new Scanner(System.in);
		// Pull, pull the declaration of id out of the loop so that it can be used after the loop. If it is declared in the loop, the variables will disappear after the loop ends
		String id;

		// while loop to determine whether the student ID is repeated
		while (true) {
			System.out.println("Please enter student ID:");
			id = sc.nextLine();
			// Judgment flag initialization
			boolean index = false;
			// Loop judgment. If the same is found, change the judgment flag and jump out of the loop
			for (int i = 0; i < stu.size(); i++) {
				if (id.equals(stu.get(i).getId())) {
					index = true;
					break;
				}
			}

			// judge
			if (index) {
				System.out.println("The student number you entered already exists! Please re-enter.");
				System.out.println("To re-enter student information, press 1");
				System.out.println("To return to the main menu, press 2");
				System.out.println("Please enter the action you need:");
				Scanner sc1 = new Scanner(System.in);
				// Receive
				int flag = sc1.nextInt();

				// switch selection
				switch (flag) {
				case 1:
					// Jump out of judgment and continue the cycle
					break;

				case 2:
				default:
					// return direct end method
					return;
				}
			} else
				break;
		}

		// Enter additional information and add to the collection
		System.out.println("Please enter your name:");
		String name = sc.nextLine();
		System.out.println("Please enter age:");
		String age = sc.nextLine();
		System.out.println("Please enter the address:");
		String address = sc.nextLine();
		Student s = new Student(id, name, age, address);

		stu.add(s);

		System.out.println("Add success");

	}

//View all narratives
	public static void travelStudent(ArrayList<Student> stu) {
		//If there is no student, no student, if there is student information
		if (stu.size() == 0) {
			System.out.println("No student");
		} else {
			System.out.println("Student ID\t\t Full name\t Age\t address\t");
			for (int i = 0; i < stu.size(); i++) {
				Student s = stu.get(i);
				System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
			}
		}

	}
//Modify student information
	public static void modifyStudent(ArrayList<Student> stu) {
		System.out.println("Please enter the student number you want to modify:");
		//Receiving student ID
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		//Flag initialization
		boolean judge = false;
		int index = -1;
		//Traverse to find students and find modification marks
		for (int i = 0; i < stu.size(); i++) {
			if (id.equals(stu.get(i).getId())) {
				
				judge = true;
				index = i;
				break;
			}
		}

		if (judge) {

			
			System.out.println("Please re-enter your name:");
			String name = sc.nextLine();
			System.out.println("Please re-enter age:");
			String age = sc.nextLine();
			System.out.println("Please re-enter the address:");
			String address = sc.nextLine();
			//Create a new object
			Student s = new Student(id, name, age, address);
			//modify
			stu.set(index,s);

			System.out.println("Modified success");
		} else {
			System.out.println("There is no such student!");
		}
	}
//Delete student information
	public static void deleteStudent(ArrayList<Student> stu) {
		System.out.println("Please enter the student number you want to delete:");
		//Receiving student ID
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		//Dimension initialization
		boolean judge = false;
		//Ergodic search
		for (int i = 0; i < stu.size(); i++) {
			if (id.equals(stu.get(i).getId())) {
				//remove
				stu.remove(i);

				judge = true;
				break;
			}
		}
		//judge
		if (judge) {
			System.out.println("Delete successful!");
		} else {
			System.out.println("There is no such student!");
		}
	}

}

Posted by venkychinnu on Tue, 07 Apr 2020 10:55:33 -0700