By GokiSoft.com| 15:39 10/07/2023|
Java Advanced

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

1. Tạo 1 lớp sinh viên gồm các thuộc tính (RollNo, Name, Sex, Age, Email, Address).

Tạo hàm tạo không đối và hàm tạo có đầy đủ tham số.

2. Tạo một HashMap quản lý danh sách sinh viên, dùng rollNo là key cho HashMap

3. Xây dựng menu chương trình

- Nhập N sinh viên

- In thông tin sv

- Tìm kiếm sinh viên (Yêu cầu nhập RollNo và hiển thị thông tin sv đó)

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

5

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

NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-03-22 19:36:51



/*
 * 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 hashmap;
import java.util.Scanner;
import java.util.HashMap;
/**
 *
 * @author abc
 */
public class Hashmap {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        HashMap<Student, String> Student = new HashMap<Student,String>();
        Scanner scan = new Scanner(System.in);
        int choice;
        do
        {
            showMenu();
             choice = Integer.parseInt(scan.nextLine());
            
            switch(choice)
            {
                case 1 :
                    int N = Integer.parseInt(scan.nextLine());
                    for ( int i = 0 ; i < N ; i++)
                    {
                        Student student = new Student();
                        student.input();
                        Student.put(student,student.RollNo );
                    }
                    break;
                    
                    
                case 2 :
                    for (Student i : Student.keySet())
                    {
                        i.display();
                    }
                    break;
                  
                    
                    
                case 3 :
                    System.out.println("Nhap rollNo");
                    String rollNo;
                    rollNo = scan.nextLine();
                    
                    for(Student i : Student.keySet())
                    {
                        if(i.getRollNo().equalsIgnoreCase(rollNo))
                        {
                            i.display();
                        }
                    }
                    break;
            }
            
            
        }while(choice!=3);
        
        
        
        
    }
    public static void showMenu()
    {
        System.out.println("1. Nhập N sinh viên");
        System.out.println("2. In thông tin sinh viên");
        System.out.println("3. Tim kien sinh vien");
    }
    
}

/*
 * 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 hashmap;
import java.util.Scanner;
/**
 *
 * @author abc
 */
public class Student {
    String RollNo;
    String Name;
    String Sex;
    Integer Age;
    String Email;
    String Address;
    
    
    public Student()
    {
        
    }

    public Student(String RollNo, String Name, String Sex, Integer Age, String Email, String Address) {
        this.RollNo = RollNo;
        this.Name = Name;
        this.Sex = Sex;
        this.Age = Age;
        this.Email = Email;
        this.Address = Address;
    }

    public String getRollNo() {
        return RollNo;
    }

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

    public String getName() {
        return Name;
    }

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

    public String getSex() {
        return Sex;
    }

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

    public Integer getAge() {
        return Age;
    }

    public void setAge(Integer 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;
    }
    
    
    public void input()
    {
        Scanner scan = new Scanner(System.in);
        RollNo = scan.nextLine();
        Name = scan.nextLine();
        Sex = scan.nextLine();
        Age = Integer.parseInt(scan.nextLine());
        Email = scan.nextLine();
        Address = scan.nextLine();
    }
    public void display()
    {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Student{" + "RollNo=" + RollNo + ", Name=" + Name + ", Sex=" + Sex + ", Age=" + Age + ", Email=" + Email + ", Address=" + Address + '}';
    }
    
    
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-03-21 07:36:04



/*
 * 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 HashMap;

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

/**
 *
 * @author DELL
 */
public class HashMap {

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

        ArrayList<Student> std = new ArrayList<>();
        int c;
        do {
            menu();
            c = Integer.parseInt(scan.nextLine());
            switch(c){
                case 1: 
                    inputStd(std);
                    break;
                case 2: 
                    PrintStd(std);
                    break;
                case 3: 
                    searchStd(std);
                    break;
                case 4:
                default :
                    System.out.println("End !");
                    break;
                
            }

        } while (c != 4);

    }

    public static void menu() {
        System.out.println("\n1.Nhập N sinh viên");
        System.out.println("2.In thông tin sv");
        System.out.println("3.Tìm kiếm sinh viên (Yêu cầu nhập RollNo)");
        System.out.println("4.thoat");
        System.out.print("\nLua chon : ");
    }

    public static void inputStd(ArrayList<Student> std) {
        Scanner scan = new Scanner(System.in);

        int n;
        System.out.println("so sinh vien can them : ");
        n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student sv = new Student();
            sv.input();
            
                    std.add(sv);
                
            
        }
    }

    public static void PrintStd(ArrayList<Student> std) {
        for (int i = 0; i < std.size(); i++) {
            std.get(i).display();
        }
    }

    public static void searchStd(ArrayList<Student> std) {
        Scanner scan = new Scanner(System.in);
        String rollno;
        System.out.println("Enter RollNo : ");
        rollno = scan.nextLine();
        for (int i = 0; i < std.size(); i++) {
            if (std.get(i).getRollNo().equalsIgnoreCase(rollno)) {
                std.get(i).display();
                break;
            }else{
                System.out.println("khong co .");
            }
        }

    }
}



/*
 * 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 HashMap;

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class Student {
    String RollNo, Name, Sex, Email, Address;
    int  Age;

    public Student() {
    }

    public Student(String RollNo, String Name, String Sex, String Email, String Address, int Age) {
        this.RollNo = RollNo;
        this.Name = Name;
        this.Sex = Sex;
        this.Email = Email;
        this.Address = Address;
        this.Age = Age;
    }

    public String getRollNo() {
        return RollNo;
    }

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

    public String getName() {
        return Name;
    }

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

    public String getSex() {
        return Sex;
    }

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

    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;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int Age) {
        this.Age = Age;
    }
    public void input(){
        Scanner scan = new Scanner(System.in);
        
        System.out.println("RollNo : ");
        RollNo = scan.nextLine();
        System.out.println("Name : ");
        Name = scan.nextLine();
        System.out.println("Age : ");
        Age = Integer.parseInt(scan.nextLine());
        System.out.println("Sex : ");
        Sex = scan.nextLine();
        
        while (true) {            
            System.out.println("Email : ");
            Email = scan.nextLine();
            if ((!Email.contains(" "))&&(Email.contains("@"))) {
                break;
            }else{
                System.err.println(" email >>> '@'&!' '");
            }
        }
    }

    @Override
    public String toString() {
        return "Student{" + "RollNo=" + RollNo + ", Name=" + Name + ", Sex=" + Sex + ", Email=" + Email + ", Address=" + Address + ", Age=" + Age + '}';
    }
    public void display(){
        System.out.println(toString());
    }   
}



Minh Nghia [T1907A]
Minh Nghia

2020-03-20 14:23:38



/*
 * 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 HashMap;

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


/**
 *
 * @author Administrator
 */
public class Test {
    public static void main(String[] args) {
        HashMap<String, Student> hashMap = new HashMap();
        
        Scanner scan = new Scanner(System.in);
        int choose,n;
        
        do{
            menu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose){
                case 1:
                    System.out.println("Nhap thong tin N sinh vien");
                    n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student student = new Student();
                        student.input();
                        hashMap.put(student.RollNo, student);
                    }
                    break;
                case 2:
                    System.out.println("Hien thi thong tin sinh vien:");
                    for (String str : hashMap.keySet()) {
                        Student student = hashMap.get(str);                                      
                        student.display(); 
                     }
                    break;
                case 3:
                    System.out.println("Nhap rollNo can tin kiem");
                    String rollNo = scan.nextLine();
                    for (String rl : hashMap.keySet()) {
                        if(hashMap.get(rl).RollNo.equalsIgnoreCase(rollNo)){
                            hashMap.get(rl).display();
                        }
                    }
                    break;
                case 4:
                    System.out.println("Thoat");
                    break;
                default:
                    System.out.println("Input fail!!");
                    break;
            }
        }while(choose != 3);
        
    }
    public static void menu(){
        System.out.println("1. Nhap N sinh vien");
        System.out.println("2. In thong tin sv");
        System.out.println("3. Tìm kiếm sinh viên (Yêu cầu nhập RollNo và hiển thị thông tin sv đó)");
        System.out.println("4. Thoat");
        System.out.println("Choose:");
    }
}



/*
 * 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 HashMap;

import java.util.Scanner;


/**
 *
 * @author Administrator
 */
public class Student {
    String RollNo, Name, Sex, Email,Address;
    int Age;
    
    
    public Student() {
    }

    public Student(String RollNo, String Name, String Sex, String Email, String Address, int Age) {
        this.RollNo = RollNo;
        this.Name = Name;
        this.Sex = Sex;
        this.Email = Email;
        this.Address = Address;
        this.Age = Age;
    }
    
    

    public String getRollNo() {
        return RollNo;
    }

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

    public String getName() {
        return Name;
    }

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

    public String getSex() {
        return Sex;
    }

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

    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;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int Age) {
        this.Age = Age;
    }
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap RollNo:");
        RollNo = scan.nextLine();
        System.out.println("Nhap ten:");
        Name = scan.nextLine();
        System.out.println("Nhap tuoi:");
        Age = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap gioi tinh:");
        Sex = scan.nextLine();
        System.out.println("Nhap email:");
        Email = scan.nextLine();
        System.out.println("Nhap dia chi:");
        Address = scan.nextLine();
        
    }
    
    public void display(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Student{" + "RollNo=" + RollNo + ", Name=" + Name + ", Sex=" + Sex + ", Email=" + Email + ", Address=" + Address + ", Age=" + Age + '}';
    }
    
    
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-20 12:56:59



package java2_Advanced.BaiTapNgay20_3_2020.QuanLySinhVienBang_hashmap;

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

public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        HashMap<String, Student> ma = new HashMap<>();
        int choose;
        do {
            showMenu();
            System.out.print("Nhap lua chon: ");
            choose = Integer.parseInt(scan.nextLine());
            switch (choose) {
                case 1:
                    System.out.println("Nhap so sinh vien can them thong tin: ");
                    int n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student student = new Student();    //tao ra 1 doi tuong tinh vien
                        student.input();                    // nhap thong tin cho sinh vien
                        ma.put(student.RollNo, student);    // them ma sinh vien lam key cua sinh vien
                    }
                    break;
                case 2:
                    for (String s : ma.keySet()) {     // for chay qua cac ma sinh vien
                        Student student = ma.get(s);   // gan ma sinh vien vao cho sinh vien
                        student.output();              // hien thi thong tin sinh vien
                    }
                    break;
                case 3:
                    System.out.println("Nhap ma sinh vien can tim: ");
                    String rollNo = scan.nextLine();
                    for (String s : ma.keySet()) {                         // for chay qua cac ma sinh vien
                        if (ma.get(s).RollNo.equalsIgnoreCase(rollNo)) {    // tim ma sinh vien giong voi ma nhap vao
                            ma.get(rollNo).output();                        // hien thi thong tin sinh vien ca ma vua tim duoc
                        }
                    }
                    break;
                case 4:
                    System.out.println("Thoat.");
                    break;
                default:
                    System.err.println("Nhap loi !!!");
                    break;
            }
        } while (choose != 4);
    }

    public static void showMenu() {
        System.out.println("1.Nhap thong tin n sinh vien");
        System.out.println("2.Hien thi thong tin n sinh vien");
        System.out.println("3.Tìm kiếm sinh viên (nhap ma sinh vien).");
        System.out.println("4.Thoat");
    }
}



package java2_Advanced.BaiTapNgay20_3_2020.QuanLySinhVienBang_hashmap;


import java.util.Scanner;

public class Student {
    String RollNo, Name, Sex, Email, Address;
    int age;
    public Student() {
    }

    public Student(String rollNo, String name, String sex, String email, String address, int age) {
        RollNo = rollNo;
        Name = name;
        Sex = sex;
        Email = email;
        Address = address;
        this.age = age;
    }

    public void input(){

        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ma sinh vien: ");
        RollNo = scan.nextLine();
        System.out.println("Nhap ten sinh  vien: ");
        Name = scan.nextLine();
        System.out.println("Nhap gioi tinh: ");
        Sex = scan.nextLine();
        System.out.println("Nhap email: ");
        Email = scan.nextLine();
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap dia chi: ");
        Address = scan.nextLine();
        System.out.println("");
    }

    public void output(){
        System.out.println(toString());
        System.out.println("");
    }

    @Override
    public String toString() {
        return "Student{" +
                "RollNo='" + RollNo + '\'' +
                ", Name='" + Name + '\'' +
                ", Sex='" + Sex + '\'' +
                ", Email='" + Email + '\'' +
                ", Address='" + Address + '\'' +
                ", age=" + age +
                '}';
    }
}



thienphu [T1907A]
thienphu

2020-03-20 09:50:32



/*
 * 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 HashMap;

import java.util.Scanner;

/**
 *
 * @author Thien Phu
 */
public class Student {

    //(RollNo, Name, Sex, Age, Email, Address).
    String name, sex, email, address;
    int age, rollNo;

    public Student() {
    }

    public Student(int rollNo, String name, String sex, String email, String address, int age) {
        this.rollNo = rollNo;
        this.name = name;
        this.sex = sex;
        this.email = email;
        this.address = address;
        this.age = age;
    }

    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap thong tin Student: ");
        System.out.println("Nhap RollNO");
        rollNo = Integer.parseInt(sc.nextLine());
        System.out.println("Nhap name: ");
        name = sc.nextLine();
        System.out.println("Nhap age: ");
        age = Integer.parseInt(sc.nextLine());
        System.out.println("Nhap sex: ");
        sex = sc.nextLine();
        System.out.println("Nhap email:");
        email = sc.nextLine();
        System.out.println("Nhap address: ");
        address = sc.nextLine();

    }

    public void display() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", sex=" + sex + ", email=" + email + ", address=" + address + ", age=" + age + ", rollNo=" + rollNo + '}';
    }

    public String getName() {
        return name;
    }

    public int getRollNo() {
        return rollNo;
    }

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

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

    public String getSex() {
        return sex;
    }

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

    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;
    }

    public int getAge() {
        return age;
    }

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

}



/*
 * 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 HashMap;

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

/**
 *
 * @author Thien Phu
 */
public class HashMapStd {

    private static Scanner sc = new Scanner(System.in);
    HashMap<Integer, Student> map;

    public HashMapStd() {
        map = new HashMap<Integer, Student>();
    }

    //them n sinh vien
    public void inputSt() {

        System.out.println("Nhap n sinh vien can them ");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            Student st = new Student();
            st.input();
            map.put(st.getRollNo(), st);
        }
    }

    public void showStd() {
        for (Integer key : map.keySet()) {
            Student st = map.get(key);
            st.display();
        }
    }

    public void fintSv() {
        System.out.println("Nhap thong tin sinh vien can tim:");
        int rollNo = sc.nextInt();
        boolean checkrollNo = false;
        for (Integer key : map.keySet()) {
            if (map.get(key).rollNo == rollNo) {
                checkrollNo = true;
                break;

            }
        }
        if (checkrollNo) {
            map.get(rollNo).display();
        } else {
            System.out.println(rollNo + " không có trong danh sach");
        }
    }
}



/*
 * 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 HashMap;

import java.util.Scanner;

/**
 *
 * @author Thien Phu
 */
public class MainTest {

    public static void main(String[] args) {
        int choose;
        Scanner sc = new Scanner(System.in);
        HashMapStd hashmap = new HashMapStd();
        do {
            showmenu();
            choose = Integer.parseInt(sc.nextLine());
            switch (choose) {
                case 1:
                    hashmap.inputSt();
                    break;
                case 2:
                    hashmap.showStd();
                    break;
                case 3:
                    hashmap.fintSv();
                    break;
                case 4:
                    System.out.println("Thoat thanh cong");
                    break;
                default:
                    System.err.println("Nhap sai roi. Chon lai");
                    break;

            }
        } while (choose != 4);

    }

    public static void showmenu() {
        System.out.println("1: Nhập N sinh viên");
        System.out.println("2: In thông tin sv");
        System.out.println("3: Tìm kiếm sinh viên (Yêu cầu nhập RollNo và hiển thị thông tin sv đó)");
        System.out.println("4: Thoat");
        System.out.println("Choose:");
    }
}