-
Notifications
You must be signed in to change notification settings - Fork 0
Feature qlsv save txt #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daboard
wants to merge
11
commits into
develop
Choose a base branch
from
feature-QLSV-saveTxt
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
df10041
test
daboard c269de9
Em vua lam xong phan save data a.
daboard 753209a
sinhvien
daboard 7b7694f
fixed overwrite problems
daboard 3c75a10
Em Demo ghi file bang cach khac
daboard 2574e16
load files successful
daboard e9306aa
fixed space problems with fields name & address
daboard f0bcb3e
directory fixed
daboard c060db9
fixed all comment
daboard 5f46fe1
fixed duplicate when write file
daboard 5fcaf23
fixed update file
daboard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| package com.oop.qlsv; | ||
|
|
||
| import java.io.DataOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.io.Serializable; | ||
| import java.util.Scanner; | ||
|
|
||
| public class QuanLySinhVien { | ||
| private static SinhVien[] data; | ||
| private static int size; | ||
| private static String filename = "studentdata.txt"; | ||
| private static Scanner keyboard = new Scanner(System.in); | ||
|
|
||
| public static void main(String[] args) throws IOException { // hàm thực thi công việc chính | ||
| data = new SinhVien[100]; | ||
| loadData(); | ||
| String repeat = null; | ||
| int option; | ||
| do { | ||
| System.out.println(" Mời bạn lựa chọn:"); | ||
| System.out.println(" 1. Thêm sinh viên"); | ||
| System.out.println(" 2. Sửa sinh viên"); | ||
| System.out.println(" 3. Xóa sinh viên"); | ||
| System.out.println(" 4. show all sinh viên"); | ||
| System.out.println(" 5. Sắp xếp Sinh Viên theo điểm giảm dần"); | ||
| System.out.println(" 6. Sắp xếp Sinh viên theo tuổi tăng dần"); | ||
| option = keyboard.nextInt(); | ||
| keyboard.nextLine(); | ||
| switch (option) { | ||
| case (1): | ||
| themSinhVien(); | ||
| System.out.println("Số lượng sinh viên hiện tại:\t " + size); | ||
| // save file | ||
| break; | ||
| case (2): | ||
| editSinhVien(); | ||
| // save file | ||
| break; | ||
| case (3): | ||
| xoaSinhVien(); | ||
| showAll(); | ||
| System.out.println("Số lượng sinh viên hiện tại:\t" + size); | ||
| // save file | ||
| break; | ||
| case (4): | ||
| showAll(); | ||
| break; | ||
| case (5): | ||
| sapXepSvTheoDiemGiamDan(); | ||
| // save file | ||
| showAll(); | ||
| break; | ||
| case (6): | ||
| sapXepSvTheoTuoiTangDan(); | ||
| showAll(); | ||
| break; | ||
|
|
||
| } | ||
| // k phai ở đây | ||
| System.out.println("Bạn có muốn thực hiện lại không? Yes/ No?"); | ||
| repeat = keyboard.nextLine(); | ||
|
|
||
| } while (repeat.equals("Yes")); | ||
|
|
||
| } | ||
|
|
||
| private static void loadData() { | ||
| int docfile = 0; | ||
| Scanner scanData = null; | ||
| System.out.println(" Loading files..."); | ||
| try { | ||
| File input = new File(filename); | ||
| scanData = new Scanner(input).useDelimiter("\\s*;\\s*"); | ||
| while (scanData.hasNext()) { | ||
| // gan' tung field trong file *txt sang bien tuong uong. | ||
| String name = scanData.next(); | ||
| String address = scanData.next(); | ||
| int age = scanData.nextInt(); | ||
| int id = scanData.nextInt(); | ||
| float diem = scanData.nextFloat(); | ||
| // tao bien sv de gan' vao mang | ||
| SinhVien sv = new SinhVien(); | ||
| sv.setTen(name); | ||
| sv.setDiaChi(address); | ||
| sv.setTuoi(age); | ||
| sv.setSoCmt(id); | ||
| sv.setDiemIelts(diem); | ||
| // gan vao mang | ||
| data[docfile] = sv; | ||
| // tang kich thuoc mang | ||
| docfile++; | ||
| } | ||
| } catch (Exception ex) { | ||
| System.out.println("File not found :D"); | ||
| } finally { | ||
| if (scanData != null) { | ||
| scanData.close(); | ||
| } | ||
| } | ||
|
|
||
| System.out.println(" files sucessfully loaded"); | ||
| System.out.println(" Số lượng sinh viên hiện tại: " + docfile); | ||
| size = docfile; | ||
| } | ||
|
|
||
| // Check an element is null or not | ||
| private static boolean isNotNull(SinhVien x) { | ||
| return x == null ? false : true; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename |
||
| } | ||
|
|
||
| private static void sapXepSvTheoTuoiTangDan() throws IOException { | ||
| for (int i = 0; i < data.length; i++) { | ||
| final boolean notNull = isNotNull(data[i]); | ||
| for (int j = i + 1; j < data.length; j++) { | ||
| if (notNull && isNotNull(data[j]) && (data[i].getTuoi() > data[j].getTuoi())) { | ||
| SinhVien temp = data[i]; | ||
| data[i] = data[j]; | ||
| data[j] = temp; | ||
| } | ||
| } | ||
| } | ||
| clearData(); | ||
| saveData(); | ||
| } | ||
|
|
||
| private static void sapXepSvTheoDiemGiamDan() throws IOException { | ||
| for (int i = 0; i < data.length; i++) { | ||
| final boolean notNull = isNotNull(data[i]); | ||
| for (int j = i + 1; j < data.length; j++) { | ||
| if (notNull && isNotNull(data[j]) && (data[i].getDiemIelts() < data[j].getDiemIelts())) { | ||
| SinhVien temp = data[i]; | ||
| data[i] = data[j]; | ||
| data[j] = temp; | ||
| } | ||
| } | ||
| } | ||
| clearData(); | ||
| saveData(); | ||
| } | ||
|
|
||
| private static void showAll() { | ||
| // TODO Auto-generated method stub | ||
| for (int i = 0; i < data.length; i++) { | ||
| if (data[i] != null) { | ||
| System.out.println(data[i]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void themSinhVien() throws IOException { | ||
| SinhVien sv = new SinhVien(); | ||
| System.out.println("Nhập tên:"); | ||
| String ten = keyboard.nextLine(); | ||
| System.out.println("Nhập địa chỉ:"); | ||
| String diaChi = keyboard.nextLine(); | ||
| System.out.println("Nhập tuổi:"); | ||
| int tuoi = Integer.parseInt(keyboard.nextLine()); | ||
| System.out.println("Nhập số chứng minh thư"); | ||
| int soCmt = Integer.parseInt(keyboard.nextLine()); | ||
| System.out.println("Nhập số điểm Ielts"); | ||
| float diemIelts = Float.parseFloat(keyboard.nextLine()); | ||
| keyboard.nextLine(); | ||
| sv.setTen(ten); | ||
| sv.setDiaChi(diaChi); | ||
| sv.setDiemIelts(diemIelts); | ||
| sv.setSoCmt(soCmt); | ||
| sv.setTuoi(tuoi); | ||
| data[size] = sv; | ||
| size++; | ||
| clearData(); | ||
| saveData(); | ||
| } | ||
|
|
||
| private static void saveData() throws IOException { | ||
| // save data | ||
| FileOutputStream fos = new FileOutputStream(filename, true); | ||
| DataOutputStream dos = new DataOutputStream(fos); | ||
| try { | ||
| for (SinhVien sinhVien : data) { | ||
| if (sinhVien != null) { | ||
| dos.writeUTF(sinhVien.toString() + "\n"); | ||
|
|
||
| } | ||
| } | ||
| System.out.println("Thông tin sinh viên đã được lưu !"); | ||
| fos.close(); | ||
| dos.close(); | ||
| } catch (IOException e) { | ||
| // TODO Auto-generated catch block | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public static void clearData() throws IOException { | ||
| FileWriter fwOb = new FileWriter(filename, false); | ||
| PrintWriter pwOb = new PrintWriter(fwOb, false); | ||
| pwOb.flush(); | ||
| pwOb.close(); | ||
| fwOb.close(); | ||
| } | ||
|
|
||
| private static void editSinhVien() throws IOException { | ||
| System.out.println(" Mời bạn nhập số cmt của sinh viên cần update thông tin:"); | ||
| int cmts = Integer.parseInt(keyboard.nextLine()); | ||
| keyboard.nextLine(); | ||
| for (int i = 0; i < data.length; i++) { | ||
| if (data[i] != null && (data[i].getSoCmt() == cmts)) { | ||
| System.out.println("Địa chỉ mới"); | ||
| data[i].setDiaChi(keyboard.nextLine()); | ||
| System.out.println("Tên mới"); | ||
| data[i].setTen(keyboard.nextLine()); | ||
| System.out.println("Điểm Ielts mới"); | ||
| data[i].setDiemIelts(Float.parseFloat(keyboard.nextLine())); | ||
| System.out.println("Tuổi mới"); | ||
| data[i].setTuoi(Integer.parseInt(keyboard.nextLine())); | ||
| } | ||
| } | ||
| clearData(); | ||
| saveData(); | ||
| } | ||
|
|
||
| private static void xoaSinhVien() throws IOException { | ||
| System.out.println("Nhập số CMT của sinh viên bạn muốn xóa:"); | ||
| int cmts = Integer.parseInt(keyboard.nextLine()); | ||
| for (int i = 0; i < data.length; i++) { | ||
| if ((data[i].getSoCmt() == cmts)) { | ||
| data[i] = null; | ||
| System.out.println("Đã xóa Sinh Viên " + cmts + " ra khỏi hệ thống"); | ||
| break; | ||
| } | ||
| } | ||
| size--; | ||
| clearData(); | ||
| saveData(); | ||
| } | ||
|
|
||
| public int size() { | ||
| return this.size(); | ||
| } | ||
| } | ||
|
|
||
| class SinhVien implements Serializable { // Chỗ này để lưu trữ thông tin sinh viên | ||
| private String ten; | ||
| private String diaChi; | ||
| private int tuoi; | ||
| private int soCmt; | ||
| private float diemIelts; | ||
|
|
||
| public void setTen(String ten) { | ||
| this.ten = ten; | ||
| } | ||
|
|
||
| public String getTen() { | ||
| return ten; | ||
| } | ||
|
|
||
| public String getDiaChi() { | ||
| return diaChi; | ||
| } | ||
|
|
||
| public void setDiaChi(String diaChi) { | ||
| this.diaChi = diaChi; | ||
| } | ||
|
|
||
| public int getTuoi() { | ||
| return tuoi; | ||
| } | ||
|
|
||
| public void setTuoi(int tuoi) { | ||
| this.tuoi = tuoi; | ||
| } | ||
|
|
||
| public int getSoCmt() { | ||
| return soCmt; | ||
| } | ||
|
|
||
| public void setSoCmt(int soCmt) { | ||
| this.soCmt = soCmt; | ||
| } | ||
|
|
||
| public float getDiemIelts() { | ||
| return diemIelts; | ||
| } | ||
|
|
||
| public void setDiemIelts(float diemIelts) { | ||
| this.diemIelts = diemIelts; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ten + ";" + diaChi + ";" + tuoi + ";" + soCmt + ";" + diemIelts + ";"; | ||
| } | ||
|
|
||
| } | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
có thể ngắn gọn dòng code
return sv == null ? false : true;đặt tên biến có nghĩa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vâng ạ