Phương thức Hiển thị thông tin sinh viên theo lớp học chưa thể chạy được

Em có làm một bài tập

  1. Su dung kien thuc ve lap trinh huong doi tuong, thiet ke cac class model chuan nhu Sinh Vien, lop hoc, …

  2. Lam duoc cac chuc nang Them, Sua, Xoa sinh vien; hem, Sua , Xoa Lop hoc

package com.nghianv.thuchanhjava.qlysinhvien;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import com.sun.org.apache.regexp.internal.recompile;

public class SinhVienManager {
	public static Scanner sc = new Scanner(System.in);
	private List<SinhVien> sinhVienLists;
	private SinhVienDao sinhVienDao;

	public SinhVienManager() {
		sinhVienDao = new SinhVienDao();
		sinhVienLists = sinhVienDao.read();
	}

	public void themSV() {
		int maSV = (sinhVienLists.size() > 0) ? (sinhVienLists.size() + 1) : 1;
		System.out.println("Nhap so phan tu cua mang: ");
		int n = sc.nextInt();
		sc.nextLine();
		for (int i = 0; i < n; i++) {
			System.out.println("Nhậ mã số sinh viên :");
			maSV = nhapMaSV();
			System.out.println("Họ và tên: ");
			String hoTen = nhapHoTen();
			System.out.println("Địa chỉ: ");
			String diaChi = nhapDiaChi();
			System.out.println("Năm sinh: ");
			int namSinh = nhapNamSinh();
			System.out.println("Nhập mã lớp");
			String maLop = nhapMaLop();
			//
			SinhVien sinhVien = new SinhVien(maLop, maSV, hoTen, namSinh, diaChi);
			sinhVienLists.add(sinhVien);
			sinhVienDao.write(sinhVienLists);
		}
	}

	//
	public void suaSV(int maSV) {
		boolean kiemtra = false;
		for (int i = 0; i < sinhVienLists.size(); i++) {
			SinhVien x = sinhVienLists.get(i);
			if (maSV == sinhVienLists.get(i).getMaSV()) {
				kiemtra = true;
				x.setHoTen(nhapHoTen());
				x.setDiaChi(nhapDiaChi());
				x.setNamSinh(nhapNamSinh());
				x.setMaLop(nhapMaLop());
				break;
			}
		}
		if (!kiemtra) {
			System.out.println("Mã sinh viên không tồn tại");
		} else {
			sinhVienDao.write(sinhVienLists);
		}
	}

	//
	public void xoaSV(int maSV) {
		SinhVien sinhVien = null;
		for (int i = 0; i < sinhVienLists.size(); i++) {
			if (maSV == sinhVienLists.get(i).getMaSV()) {
				sinhVien = sinhVienLists.get(i);
				break;
			}
		}
		if (sinhVien != null) {
			sinhVienLists.remove(sinhVien);
			sinhVienDao.write(sinhVienLists);
		} else {
			System.out.println("Mã sinh viên không tồn tại");
		}
	}

	/*
	 * Sắp xếp danh sách sinh viên theo mã số
	 */
	public void sapXep() {
		Collections.sort(sinhVienLists, new Comparator<SinhVien>() {

			@Override
			public int compare(SinhVien o1, SinhVien o2) {

				if (o1.getMaSV() > o2.getMaSV()) {
					return 1;
				} else if (o1.getMaSV() < o2.getMaSV()) {
					return -1;
				} else {
					return 0;
				}
			}

		});
	}

	public List<SinhVien> getSinhVienLists() {
		return sinhVienLists;
	}

	public void setSinhVienLists(List<SinhVien> sinhVienLists) {
		this.sinhVienLists = sinhVienLists;
	}

	/*
	 * Hiện thị thông tin toàn bộ danh sách sinh viên
	 */
	public void hienThiSV() {
		for (SinhVien sinhVienList : sinhVienLists) {
			System.out.format("%2d | ", sinhVienList.getMaSV());
			System.out.format("%3s | ", sinhVienList.getHoTen());
			System.out.format("%3s | ", sinhVienList.getDiaChi());
			System.out.format("%3s | ", sinhVienList.getNamSinh());
			System.out.format("%3s | ", sinhVienList.getMaLop());

		}
	}

	/*
	 * Hiện thị danh sách sinh viên theo lớp
	 */
	public void hienThi(List<SinhVien> sinhVienLists, String maLop) {
		SinhVienDao svDao = new SinhVienDao();
		svDao.read();
		sinhVienLists = new ArrayList<>();
		for (int i = 0; i < sinhVienLists.size(); i++) {
			if (sinhVienLists.get(i).getMaLop().equals(maLop)) {
				System.out.format("%2d | ", sinhVienLists.get(i).getMaSV());
				System.out.format("%3s | ", sinhVienLists.get(i).getHoTen());
				System.out.format("%3s | ", sinhVienLists.get(i).getDiaChi());
				System.out.format("%3s | ", sinhVienLists.get(i).getNamSinh());
			} else {
				System.out.println("Không có thông tin lớp học cần tìm kiếm");
			}

		}

	}
//
	public void hienThi1(List<SinhVien> sinhVienLists, String maLop) {
		SinhVienDao svDao = new SinhVienDao();
		svDao.read();
		sinhVienLists = new ArrayList<>();
		for (int i = 0; i < sinhVienLists.size(); i++) {
			if (sinhVienLists.contains(maLop)) {
				System.out.format("%2d | ", sinhVienLists.get(i).getMaSV());
				System.out.format("%3s | ", sinhVienLists.get(i).getHoTen());
				System.out.format("%3s | ", sinhVienLists.get(i).getDiaChi());
				System.out.format("%3s | ", sinhVienLists.get(i).getNamSinh());
			} else {
				System.out.println("Không có thông tin lớp học cần tìm kiếm");
			}

		}

	}
	//
	public int nhapMaSV() {
		System.out.println("Nhập mã sinh viên: ");
		while (true) {
			try {
				int maSV = Integer.parseInt((sc.nextLine()));
				return maSV;

			} catch (NumberFormatException e) {
				System.out.println("Error! Mời bạn nhập lại mã sinh viên là số nguyên");
			}
		}
	}

	//
	public String nhapHoTen() {
		System.out.println("Nhập họ tên sinh viên: ");
		return sc.nextLine();
	}

	//
	public String nhapDiaChi() {
		System.out.println("Nhập địa chỉ: ");
		return sc.nextLine();
	}

	//
	public int nhapNamSinh() {
		System.out.println("Nhập năm sinh: ");
		while (true) {
			int namSinh = Integer.parseInt((sc.nextLine()));
			Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			try {
				if ((year - namSinh) < 17 && (year - namSinh) > 100) {
					throw new ArithmeticException(" Xin vui lòng kiểm tra lại năm sinh bạn nhập");
				} else {
					System.out.println("ok");
				}
				return namSinh;

			} catch (Exception e) {
				System.out.println("Xin vui lòng kiểm tra lại năm sinh bạn nhập");
			}
		}
	}

	//
	public String nhapMaLop() {
		System.out.println("Nhập mã lớp: ");
		return sc.nextLine();
	}

}
public static void main(String[] args) {
		List<SinhVien> lists = new ArrayList<>();
		SinhVienManager sv = new SinhVienManager();
		LopManager lopManager = new LopManager();
		int luachon;
//		boolean thoat = false;
		showMenu();
		while (true) {
			luachon = Integer.parseInt(sc.nextLine());
			switch (luachon) {
			case 1:
				sv.themSV();
				break;
			case 2:
				System.out.println("Mời bạn mã của sinh viên cần tìm kiếm");
				int maSV = Integer.parseInt((sc.nextLine()));
				sv.suaSV(maSV);
				break;
			case 3:
				System.out.println("Mời bạn mã của sinh viên cần tìm kiếm");
				int maSV1 = Integer.parseInt((sc.nextLine()));
				sv.xoaSV(maSV1);
				break;
			case 4:
				sv.hienThiSV();
				break;
			case 5:
//				List<SinhVien> sinhVienLists = new ArrayList<>();
				System.out.println("Moi bạn nhập mã lớp học cần tìm kiếm");
				String maLop = sc.nextLine();
				sv.hienThi1(sinhVienLists, maLop);
				break;
			case 6:
				lopManager.themLop();
				break;
			case 7:
				System.out.println("Moi bạn nhập mã lớp học cần tìm kiếm");
				String maLop1 = sc.nextLine();
				lopManager.suaLop(maLop1);
				break;
			case 8:
				System.out.println("Moi bạn nhập mã lớp học cần tìm kiếm");
				String maLop2 = sc.nextLine();
				lopManager.xoaLop(maLop2);
				break;
				
			default:
				System.out.println("Mời bạn nhập lựa chọn");
				break;
			case 0:
				System.exit(2);
				break;
			}

Trong phương thức Hiện thị thông tin sinh viên theo lớp học, em mãi chưa thể chạy được. Mọi người chỉ giúp em với

merged to the main of the topic and moved by noname00

trong hàm hienThi( List<SinhVien> sinhVienLists, String maLop).
đã truyền vào listSv rồi sao còn new ArrayList() nữa?
khi này data rỗng -> sai

em cảm ơn. Ban đầu em cũng không làm thế nhưng ở trong file run.java trong lúc em gọi hàm case 5:

System.out.println(“Moi bạn nhập mã lớp học cần tìm kiếm”);
String maLop = sc.nextLine();
sv.hienThi(sinhVienLists, maLop);

thì luôn báo "sinhVienLists cannot be resolved to a variable" hoặc đề nghị creat local variable
anh có thể chỉ giúp em tại sao không ạ

Xóa thử dòng này trong hàm hienThi xem

nếu e đảm bảo dc sinhVienLists truyền vào có data, thì thử làm như sau

List newList = new ArrayList(sinhVienLists);

dùng newList này để loop vòng for.
nếu sinhVienLists truyền vào đã rỗng or null thì phải xử lí chỗ gọi hàm nhé.

Em cảm ơn tất cả mọi người
do trong phương thức sv.hienThi(sinhVienLists, maLop); em lại truyền thêm vào một sinhVienLists trong khi thực tế đọc từ file data làm cho luôn xảy ra hai trường hợp: 1 là phải khai báo, or là khi xuất ra danh sách luôn là null
Em đã xóa đi và chỉ truyền vào sv.hienThi(maLop); để tìm kiếm thì đã xuất ra ok ạ

package com.nghianv.thuchanhjava.qlysinhvien;

import java.io.Serializable;
import java.util.List;

public class Lop implements Serializable{
	protected String maLop;
	protected String tenLop;
	public Lop() {
	}

	public Lop(String tenLop, String maLop) {
		super();
		this.tenLop = tenLop;
		this.maLop = maLop;
	}

	public String getTenLop() {
		return tenLop;
	}

	public void setTenLop(String tenLop) {
		this.tenLop = tenLop;
	}

	public String getMaLop() {
		return maLop;
	}

	public void setMaLop(String maLop) {
		this.maLop = maLop;
	}
}

Đề bài của em bây giờ thêm là tạo 1 class Lop với 2 thuộc tính “1. maLop” & “2.tenLop”
hãy xuất ra thông tin sinh viên có thêm trường tên lớp với maLop từ sinhvien.java = maLop trong Lop.java
Mọi người chỉ em với
Làm cách nào em có thể truy suất từ 2 file data “lop.txt” chứa listLop và “sinhvien.txt” chứa listSinhVien rồi xuất ra sinh viên thêm trường teenLop với ạ

package com.nghianv.thuchanhjava.qlysinhvien;

import java.util.List;
import java.util.Scanner;

public class LopManager {
	public static Scanner sc = new Scanner(System.in);
	private List<Lop> lopLists;
	private LopDao lopDao;
	public static String y1;
	
	public LopManager() {
		lopDao = new LopDao();
		lopLists = lopDao.read();
	}
	//
	public void themLop() {
		String tenLop = nhapLop();
		System.out.println("Nhập mã lớp");
		String maLop = nhapMaLop();
		//
		Lop lop = new Lop(tenLop, maLop);
		lopLists.add(lop);
		lopDao.write(lopLists);
	}
	//
	public void suaLop(String maLop) {
		boolean kiemtra = false;
		for (int i = 0; i < lopLists.size(); i++) {
			Lop y = lopLists.get(i);
			if (maLop == y.getMaLop()) {
				kiemtra = true;
				y.setTenLop(nhapLop());
				break;
			}
		}
		if (!kiemtra) {
			System.out.println("Mã lớp không tồn tại");
		} else {
			lopDao.write(lopLists);
		}
	}
	//
	public void xoaLop(String maLop) {
		Lop lop = null;
		for (int i = 0; i < lopLists.size(); i++) {
			Lop y = lopLists.get(i);
			if (maLop == y.getMaLop()) {
				lop = y;
				break;
			}

		}
		if (lop != null) {
			lopLists.remove(lop);
			lopDao.write(lopLists);
		} else {
			System.out.println("Mã lớp không tồn tại");
		}

	}
	//
	public void hienThiLop() {
		int stt = 0;
		for (int j = 0; j <= lopLists.size(); j++) {
			y1 = lopLists.get(j).getMaLop();
			Lop y = lopLists.get(j);
			System.out.format("%3s | ", stt++);
			System.out.format("%3s | ", y.getMaLop());
			System.out.format("%3s | ", y.getTenLop());
		}
	}
	//
	public String nhapLop() {
		System.out.println("Nhập lớp: ");
		return sc.nextLine();
	}

	//
	public String nhapMaLop() {
		System.out.println("Nhập mã lớp: ");
		return sc.nextLine();
	}
	
}

Chưa biết cách đọc tập tin?
Thấy bạn có dùng đến Serializable.
Trong 2 tập tin .txt đó lưu dạng gì? Serializable từ đối tượng Java hay dữ liệu văn bản.

Em mới đang bắt đầu học java lên còn nhiều chỗ chưa hiểu
Em lưu tập tin từ đối tượng Sinh Vien trong list anh à

Class Sinh Vien nên implements Serializable

public class SinhVien implements Serializable

sau đó lưu xuống file mốt đọc thì ép kiểu về đúng class là dùng lại được. (Nhưng thủ thuật này không khuyến khích lắm vì file sẽ rất nặng và đọc lâu.)

Đây là bài cơ bản của java. Về sau còn cáu hình file XML or Json và lưu xuống file dạng text dễ dàng đọc và dể sử dụng hơn :slight_smile:

Demo:
https://www.mkyong.com/java/how-to-read-and-write-java-object-to-a-file/

1 Like

Lỗi là do ở hàm main là hàm static, mà sinhvienlists bạn khai báo là nonstatc nên nó k refer đc. Bạn cũng nên đổi tên biến 1 chút cho dễ đọc :))). Sinhvienlists thì mình dễ hiểu nhầm thành nhiều sinhvienlist

Vâng em cảm ơn
Có gì nhờ các bác chỉ dạy giúp em

Vâng em cảm ơn
Có gì nhờ các bác chỉ dạy giúp em!

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?