By GokiSoft.com| 18:50 15/03/2023|
Java Advanced

[Source Code] Chương trình quản lý sinh viên bằng HashMap - Java Advanced - C2206L

Chương trình quản lý sinh viên bằng HashMap - Java Advanced

#Main.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft.bt1058;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author teacher
 */
public class Main {

    public static void main(String[] args) {
        //rollNo -> key -> String, key => value, key = rollNo, value = Student
        HashMap<String, Student> maps = new HashMap<>();
        Scanner scan = new Scanner(System.in);
        int choose;

        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1: {
                    System.out.println("Nhap so sinh vien can them: ");
                    int n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student std = new Student();
                        std.input();
                        
                        maps.put(std.getRollNo(), std);
                    }
                    break;
                }
                case 2:
                    System.out.println("Danh sach sinh vien");
                    for (Map.Entry<String, Student> entry : maps.entrySet()) {
                        Student std = entry.getValue();
                        std.display();
                    }
                    break;
                case 3:
                    System.out.println("Nhap MSV can tim kiem: ");
                    String rollNo = scan.nextLine();
                    if(maps.containsKey(rollNo)) {
                        Student std = maps.get(rollNo);
                        std.display();
                    }
                    break;
                case 4:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while (choose != 4);
    }

    static void showMenu() {
        System.out.println("1. Nhap N sinh vien");
        System.out.println("2. In thong tin sv");
        System.out.println("3. Tim kiem");
        System.out.println("4. Thoat");
        System.out.println("Chon: ");
    }
}

#Student.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft.bt1058;

import java.util.Scanner;

/**
 *
 * @author teacher
 */
public class Student {
    String rollNo;
    String sex;
    String name;
    int age;
    String email;
    String address;

    public Student() {
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "rollNo=" + rollNo + ", sex=" + sex + ", name=" + name + ", age=" + age + ", email=" + email + ", address=" + address;
    }
    
    public void display() {
        System.out.println(this);
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap MSV: ");
        rollNo = scan.nextLine();
        System.out.println("Nhap gioi tinh: ");
        sex = scan.nextLine();
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap email: ");
        email = scan.nextLine();
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }
}
Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)