By GokiSoft.com|
18:55 20/07/2022|
Java Advanced
[Source Code] Chương trình quản lý sinh viên bằng HashMap - Java Advanced - C2108L
Chương trình quản lý sinh viên bằng HashMap - Java Advanced
#C2108L.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 bt1058;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class C2108L {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
HashMap<String, Student> studentList = new HashMap<>();
Scanner scan = new Scanner(System.in);
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();
//Them phan tu nay vao day???
//Su dung rollno lam key trong mang HashMap
studentList.put(std.getRollno(), std);
}
//Duyet qua cac phan tu trong HashMap
for (Map.Entry<String, Student> entry : studentList.entrySet()) {
String key = entry.getKey();
Student std = entry.getValue();
System.out.println("KEY: " + key);
std.display();
}
System.out.println("Nhap MSV can tim kiem: ");
String rollno = scan.nextLine();
Student std = studentList.get(rollno);
if(std != null) {
std.display();
} else {
System.out.println("Ko tim thay sinh vien co msv: " + rollno);
}
System.out.println("Nhap MSV can tim kiem: ");
rollno = scan.nextLine();
std = studentList.get(rollno);
if(std != null) {
std.display();
} else {
System.out.println("Ko tim thay sinh vien co msv: " + rollno);
}
}
}
#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 bt1058;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Student {
String rollno, name, gender, email, address;
int age;
public Student() {
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap MSV: ");
rollno = scan.nextLine();
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap gioi tinh: ");
gender = scan.nextLine();
System.out.println("Nhap email: ");
email = scan.nextLine();
System.out.println("Nhap dia chi: ");
address = scan.nextLine();
System.out.println("Nhap tuoi: ");
age = Integer.parseInt(scan.nextLine());
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "rollno=" + rollno + ", name=" + name + ", gender=" + gender + ", email=" + email + ", address=" + address + ", 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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)