import java.util.Scanner;
import java.util.Date;
import java.text.SimpleDateFormat;
public class QuanLy {
private DanhSachSinhVien danhSach;
private Scanner reader;
//Ham tao
public QuanLy(){
danhSach=new DanhSachSinhVien();
reader=new Scanner(System.in);
}
private SinhVien taoDoiTuongSinhVien(String ma){
SinhVien sv=new SinhVien();
System.out.print("Nhap ma sinh vien: ");sv.setMa(reader.nextLine());
System.out.print("Nhap ten sinh vien: ");sv.setTen(reader.nextLine());
System.out.print("Nhap lop: ");sv.setLop(reader.nextLine());
System.out.print("Nhap dia chi: ");sv.setDiaChi(reader.nextLine());
System.out.print("Nhap gioi tinh: ");
sv.setGioiTinh(chuyenChuoiThanhGioiTinh(reader.nextLine()));
System.out.print("Nhap ngay sinh: ");
sv.setNgaySinh(chuyenChuoiThanhNgaySinh(reader.nextLine()));
return sv;
}
private void them(){
if(danhSach.themSinhVien(taoDoiTuongSinhVien(" "))){
System.out.println("Them moi thanh cong");
}
else{
System.out.println("Them moi that bai");
}
}
private void xoaSinhVien(){
System.out.print("Nhap ma sinh vien can xoa: ");
if(danhSach.xoaSinhVienTheoMa(reader.nextLine())){
System.out.println("Xoa thanh cong!!!");
}
else{
System.out.println("Xoa that bai!!!");
}
}
private void xuatThongTinSinhVien(SinhVien sv){
System.out.println("Ma SV: "+sv.getMa());
System.out.println("Ten SV: "+sv.getTen());
System.out.println("Lop: "+sv.getLop());
System.out.println("Dia chi: "+sv.getDiaChi());
System.out.println("Ngay sinh: "+chuyenNgaySinhThanhChuoi(sv.getNgaySinh()));
System.out.println("Gioi tinh: "+chuyenGioiTinhThanhChuoi(sv.isGioiTinh()));
}
private String chuyenGioiTinhThanhChuoi(boolean gt){
if(gt)
return "Nam";
return "Nu";
}
private boolean chuyenChuoiThanhGioiTinh(String strgt){
if(strgt.equalsIgnoreCase("nam")){
return true;}
return false;
}
private Date chuyenChuoiThanhNgaySinh(String strNgaySinh){
try{
return new SimpleDateFormat("dd/MM/yyyy").parse(strNgaySinh);
}catch (Exception ex){
System.out.println("Loi: "+ex.getMessage());
}
return null;
}
private String chuyenNgaySinhThanhChuoi(Date d){
return new SimpleDateFormat("dd/MM/yyyy").format(d);
}
private void timSinhVien(){
System.out.print("Nhap ma sinh vien can tim: ");
SinhVien sv=danhSach.timSinhVienTheoMa(reader.nextLine());
if(sv!=null){
xuatThongTinSinhVien(sv);
}else{
System.out.println("Khong tim thay!!!");
}
}
private void hienThiDanhSachSinhVien(){
for(SinhVien sv:danhSach.layDanhSach())
xuatThongTinSinhVien(sv);
}
private void hienThiDanhSachSinhVienTheoLop(){
String lop;
System.out.print("Nhap lop can hien thi: ");
lop=reader.nextLine();
for(SinhVien sv:danhSach.layDanhSachSinhVienTheoLop(lop))
xuatThongTinSinhVien(sv);
}
private void hienThiDanhSachSinhVienTheoDiaChi(){
String diaChi;
System.out.print("Nhap dia chi can hien thi: ");
diaChi=reader.nextLine();
for(SinhVien sv:danhSach.layDanhSachTheoDiaChiGanDung(diaChi))
xuatThongTinSinhVien(sv);
}
private void capNhat(){
System.out.print("Nhap ma sinh vien can cap nhat: ");
String ma=reader.nextLine();
SinhVien sv=danhSach.timSinhVienTheoMa(ma);
if (sv==null) {
System.out.println("Khong ton tai ma sinh vien");
}else{
System.out.print("Cap nhat Ho ten: ");sv.setTen(reader.nextLine());
System.out.print("Cap nhat Lop: ");sv.setLop(reader.nextLine());
System.out.print("Cap nhat Dia chi: ");sv.setDiaChi(reader.nextLine());
System.out.print("Cap nhat gioi tinh: ");
sv.setGioiTinh(chuyenChuoiThanhGioiTinh(reader.nextLine()));
System.out.print("Cap nhat ngay sinh: ");
sv.setNgaySinh(chuyenChuoiThanhNgaySinh(reader.nextLine()));
}
}
public void menu(){
System.out.println("1. Them moi");
System.out.println("2. In danh sach sinh vien");
System.out.println("3. In danh sach sinh vien theo lop");
System.out.println("4. In danh sach sinh vien theo dia chi gan dung");
System.out.println("5. Tim kiem sinh vien theo ma");
System.out.println("6. Xoa sinh vien theo ma chi dinh");
System.out.println("7. Cap nhat sinh vien theo ma chi dinh");
System.out.println("0. Thoat chuong trinh");
int chon;
do{
System.out.print("Moi ban chon chuc nang chuong trinh ");
chon=Integer.parseInt(reader.nextLine());
switch(chon){
case 1:{them();
break;}
case 2: {hienThiDanhSachSinhVien();
break;}
case 3:{
hienThiDanhSachSinhVienTheoLop();
break;} }
case 4: {hienThiDanhSachSinhVienTheoDiaChi();
break;}
case 5:{timSinhVien();
break;}
case 6:{xoaSinhVien();
break;}
case 7: {capNhat();
break;}
}
}while(chon!=0);
}