Giang vien huong dan: Thay Ngo Cong Thang

Một phần của tài liệu BÀI TẬP LỚN LẬP TRÌNH JAVA (Trang 31)

- Sinh vien thuc hien:

Nguyen Thi Linh Chi Vu Thi Mai Hoa Pham Van Huong

Le Thi Hong Nhung

Nguyen Thi Sen *- De tai:

+ Tim hieu vao/ra tep (text/binary) trong JaVa

+ Viet chuong trinh nhap vao mot danh sach sinh vien voi cac thong tin la Ma sinh vien, ho ten, ngay sinh.

Ghi danh sach sinh vien ra tep. Doc lai danh sach sinh vien tu tep,tach ho ten thanh ho dem va ten,

sap xep danh sach theo ten van ABC, neu trung ten sap xep theo ho dem. Chuong trinh co kha nang nhap du lieu

tu tep Excel va ghi du lieu ra tep Excel. **/ import java.io.*; import java.util.*; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import fita.ncthang.console.cin;

public class JavaPro {

public static void main(String[] args) throws IOException {

int choice; String filename = ""; end_while: while (true) { System.out.println();

System.out.println("1. Nhap danh sach sinh vien tu ban phim."); System.out.println("2. Hien danh sach sinh vien ra man hinh."); System.out.println("3. Sap xep danh sach sinh vien theo ho ten.");

System.out.println("4. Ghi danh sach sinh vien tu tep van ban."); System.out.println("5. Doc danh sach sinh vien ra tep van ban."); System.out.println("6. Ghi danh sach sinh vien tu tep Excel."); System.out.println("7. Doc danh sach sinh vien tu tep Excel."); System.out.println("8. Thoat chuong trinh.");

System.out.print("Nhap lua chon: "); choice = cin.getInt(); switch (choice) { case 1: stdList = inputStudentList(); break; case 2:

if (stdList != null) showStudentList(stdList);

else System.out.println("Khong co sinh vien trong danh sach.");

case 3:

stdList = sortingStudentList(stdList); break;

case 4:

System.out.print("Nhap ten tep de ghi danh sach sinh vien: ");

filename = cin.getString(); if (stdList != null)

{ fileOutput(stdList, filename);

System.out.print("\nDANH SACH SINH VIEN DA DUOC LUU VAO TEP "+filename);

System.out.println(); }

else

System.out.println("Khong co sinh vien trong danh sach.");

break; case 5:

System.out.print("Nhap ten tep de doc danh sach sinh vien: ");

filename = cin.getString(); stdList = fileInput(filename); break;

case 6: (adsbygoogle = window.adsbygoogle || []).push({});

System.out.print("Nhap ten tep Excel de ghi danh sach sinh vien (*.xls): ");

filename = cin.getString(); if (stdList != null)

{

excelOutput(stdList, filename);

System.out.print("\nDANH SACH SINH VIEN DA DUOC LUU VAO TEP "+filename);

System.out.println(); }

else

System.out.println("Khong co sinh vien trong danh sach.");

break; case 7:

System.out.print("Nhap ten tep Excel de doc danh sach sinh vien (*.xls): ");

filename = cin.getString(); stdList = excelInput(filename); break; case 8: break end_while; default:

System.out.println("Chon sai, hay chon lai."); }

} }

public static Student[] inputStudentList() {

Student[] std = null; int std_num;

String id = "", name = "", birth = ""; do

{

System.out.print("Nhap so sinh vien: "); std_num = cin.getInt();

} while (std_num <= 0); std = new Student[std_num];

System.out.println("Nhap danh sach sinh vien:"); for (int i = 0; i < std_num; i++)

{

System.out.printf("Nhap sinh vien thu %d:\n", i + 1); System.out.print("Nhap ma sinh vien: ");

id = cin.getString();

System.out.print("Nhap ho ten: "); name = cin.getString();

System.out.print("Nhap ngay sinh (xx/yy/zzzz): "); birth = cin.getString();

std[i] = new Student(id, name, birth); }

return std; }

public static void showStudentList(Student[] std) {

int std_num = std.length; System.out.println(); System.out.println();

System.out.println("Danh sach sinh vien:"); (adsbygoogle = window.adsbygoogle || []).push({});

System.out.printf("%-15s %-20s %-15s\n", "Ma sinh vien", "Ho ten", "Ngay sinh");

for (int i = 0; i < std_num; i++)

System.out.printf("%-15s %-20s %-15s\n", std[i].getStudentID(), std[i].getFullName(), std[i].getDayOfBirth());

}

public static Student[] fileInput(String filename) throws IOException {

Student[] std = null; int std_num;

String id = "", name = "", birth = ""; String std_info = "";

try {

FileReader freader = new FileReader(filename);

BufferedReader buffered = new BufferedReader(freader); std_num = Integer.parseInt(buffered.readLine());

std = new Student[std_num]; for (int i = 0; i < std_num; i++) {

int index = std_info.indexOf(" "); int l_index = std_info.lastIndexOf(" "); id = std_info.substring(0, index).trim();

name = std_info.substring(index + 1, l_index).trim(); birth = std_info.substring(l_index + 1).trim();

std[i] = new Student(id, name, birth); }

System.out.print("Noi dung co trong tep van ban la:\n"); showStudentList(std);

buffered.close(); }

catch (IOException exc) {

System.out.println("Khong the mo tep"); }

return std; }

public static void fileOutput(Student[] std, String filename) throws IOException

{

String std_info;

int std_num = std.length; try {

FileWriter fwriter = new FileWriter(filename);

BufferedWriter buffered = new BufferedWriter(fwriter); buffered.write("" + std_num);

buffered.newLine();

for (int i = 0; i < std_num; i++) { std_info = "" + std[i].getStudentID() + " " + std[i].getFullName() + " " + std[i].getDayOfBirth(); buffered.write(std_info); buffered.newLine(); } buffered.flush(); buffered.close(); }

catch (IOException exc) {

System.out.println("Khong the tao tep"); }

} (adsbygoogle = window.adsbygoogle || []).push({});

public static Student[] sortingStudentList(Student[] std) {

Student tmp = null; int std_num = std.length;

for (int i = 0; i < std_num - 1; i++)

for (int j = i + 1; j < std_num; j++) if

(std[i].getName().compareToIgnoreCase(std[j].getName()) == 0) {

if

{ tmp = std[i]; std[i] = std[j]; std[j] = tmp; showStudentList(std); } } else if (std[i].getName().compareToIgnoreCase(std[j].getName()) > 0) { tmp = std[i]; std[i] = std[j]; std[j] = tmp; showStudentList(std); } else showStudentList(std); return std; }

public static Student[] excelInput(String filename) {

Student[] std = null; int std_num = 0;

try {

FileInputStream fIn = new FileInputStream(filename); HSSFWorkbook workbook = new HSSFWorkbook(fIn); HSSFSheet sheet = workbook.getSheet("Sheet0"); HSSFRow row = null;

HSSFCell cell = null; row = sheet.getRow(0); cell = row.getCell(0);

std_num = Integer.parseInt(cell.getStringCellValue()); std = new Student[std_num];

for (int i = 1; i <= std_num; i++) { row = sheet.getRow(i); for (int j = 0; j < 3; j++) { cell = row.getCell(j); switch (j) { case 0: id = cell.getStringCellValue(); break; case 1: name = cell.getStringCellValue(); break; case 2: birth = cell.getStringCellValue();

break; }

}

std[i-1] = new Student(id, name, birth); }

System.out.print("Noi dung co trong tep Excel la:\n"); showStudentList(std);

}

catch (Exception exc) {

System.out.println(exc.getMessage()); }

return std; }

public static void excelOutput(Student[] std, String filename) {

try {

HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet();

HSSFRow row = null; HSSFCell cell = null; int std_num = std.length; row = sheet.createRow(0); cell = row.createCell(0);

cell.setCellValue("" + std_num);

for(int i = 1; i <= std_num; i++) { row = sheet.createRow(i); cell = row.createCell(0); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(std[i-1].getStudentID()); cell = row.createCell(1); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(std[i-1].getFullName()); cell = row.createCell(2); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(std[i-1].getDayOfBirth()); }

FileOutputStream fOut = new FileOutputStream(filename); wb.write(fOut); fOut.flush(); fOut.close(); } catch(Exception exc) { System.out.println(exc.getMessage()); } } }

class Student { (adsbygoogle = window.adsbygoogle || []).push({});

private String studentID; private String fullname; private String dayofbirth; public Student() { studentID = ""; fullname = ""; dayofbirth = ""; }

public Student(String id, String name, String birth) { studentID = id; fullname = name; dayofbirth = birth; } public Student(Student std) { studentID = std.studentID; fullname = std.fullname; dayofbirth = std.dayofbirth; }

public void setStudentID(String id) {

studentID = id; }

public String getStudentID() {

return studentID; }

public void setFullName(String name) {

fullname = name; }

public String getFullName() {

return fullname; }

public String getName() {

return fullname.substring(fullname.lastIndexOf(" ") + 1).trim(); }

{

dayofbirth = birth; }

public String getDayOfBirth() {

return dayofbirth; }

Một phần của tài liệu BÀI TẬP LỚN LẬP TRÌNH JAVA (Trang 31)