Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by NguyenVietNamSon ( 5 years ago )
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;



public class main {

	public static void XuatDanhSach(List<info> list)
	{
		for(int i = 0; i < list.size(); ++i)
		{
			list.get(i).Print();
			System.out.print("\n");
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/* Tạo sẵn dữ liệu cho list để từ đó đưa vào file book */
		List<info> list = new ArrayList<info>();
		
		info info1 = new info("aaa", "a", "a", 1, 1);
		info info2 = new info("bbb", "b", "b", 2, 2);
		info info3 = new info("ccc", "c", "c", 3, 3);
		info info4 = new info("ddd", "d", "d", 4, 4);
		info info5 = new info("eee", "e", "e", 5, 5);
		
		list.add(info1);
		list.add(info2);
		list.add(info3);
		list.add(info4);
		list.add(info5);
		/* ====================================================== */
		
		/* Ghi dữ liệu được gán sẵn ở trên xuống file Book.dat */
		try {
			   //Bước 1: Tạo đối tượng luồng và liên kết nguồn dữ liệu
			   FileOutputStream fos = new FileOutputStream("book.dat");
			   DataOutputStream dos = new DataOutputStream(fos);
			  
			   //Bước 2: Ghi dữ liệu
			   
			   dos.writeBytes(list.get(0).getCategorybook());
			   dos.writeBytes("|");
			   dos.writeBytes(list.get(0).getTitlebook());
			   dos.writeBytes("|");
			   dos.writeBytes(list.get(0).getAuthorbook());
			   dos.writeBytes("|");
			   dos.writeBytes(String.valueOf(list.get(0).getYearRelease()));
			   dos.writeBytes("|");
			   dos.writeBytes(String.valueOf(list.get(0).getPriceOnOrder()));
			   
			   
			   for(int i = 1; i < list.size(); ++i)
			   {
				   dos.writeBytes("\n");
				   dos.writeBytes(list.get(i).getCategorybook());
				   dos.writeBytes("|");
				   dos.writeBytes(list.get(i).getTitlebook());
				   dos.writeBytes("|");
				   dos.writeBytes(list.get(i).getAuthorbook());
				   dos.writeBytes("|");
				   dos.writeBytes(String.valueOf(list.get(i).getYearRelease()));
				   dos.writeBytes("|");
				   dos.writeBytes(String.valueOf(list.get(i).getPriceOnOrder()));
			   }

			   
			   //Bước 3: Đóng luồng
			   fos.close();
			   dos.close();
			   System.out.println("Da ghi thanh cong danh sach vao file book.dat!");
			  } catch (IOException ex) {
			    ex.printStackTrace();
			  } 
		/* ========================================================================== */
		
		list.clear();
		
		/* Câu 1: Đọc lại dữ liệu từ file book.dat vào List và hiển thị lên màn hình */
		System.out.print("\nCau 1: Du lieu cac quyen sach doc tu file book.dat vao danh sach\n");
		try {
			   //Bước 1: Tạo đối tượng luồng và liên kết nguồn dữ liệu
			   FileInputStream fis = new FileInputStream("book.dat");
			   DataInputStream dis = new DataInputStream(fis);
		
			   //Bước 2: Đọc dữ liệu
			   
			   String line;
			   while((line = dis.readLine()) != null)
			   {
				   int length = line.length();
				   
				   int start = 0;
				   int end = 0;
				   for(int i = start; i < length; ++i)
				   {
					   if(line.charAt(i) == '|')
					   {
						   end = i;
						   break;
					   }
				   }
				   //System.out.print(line.substring(start, end));
				   String cate = line.substring(start, end);
				   
				   start = end + 1;
				   for(int i = start; i < length; ++i)
				   {
					   if(line.charAt(i) == '|')
					   {
						   end = i;
						   break;
					   }
				   }
				   String title = line.substring(start, end);
				   
				   start = end + 1;
				   for(int i = start; i < length; ++i)
				   {
					   if(line.charAt(i) == '|')
					   {
						   end = i;
						   break;
					   }
				   }
				   String author = line.substring(start, end);
				   
				   start = end + 1;
				   for(int i = start; i < length; ++i)
				   {
					   if(line.charAt(i) == '|')
					   {
						   end = i;
						   break;
					   }
				   }
				   int year = Integer.parseInt(line.substring(start, end));
				   
				   start = end + 1;
				   
				   double price = Double.parseDouble(line.substring(start));
				   info f = new info(cate, title, author, year, price);
				   list.add(f);
			   }
			   
			 //Bước 3: Đóng luồng
			   fis.close();
			   dis.close();
			   
			  } catch (IOException ex) {
			    ex.printStackTrace();
			  } 
		
		
		XuatDanhSach(list);
		/* ================================================================== */
	
		
		/* Câu 2: Sắp xếp các quyển sách theo năm xuất bản và hiển thị lên màn hình */
		System.out.print("\nCau 2: Sap xep cac quyen sach theo nam xuat ban tang dan\n");
		for(int i = 0; i < list.size() - 1; ++i)
		{
			for(int j = i + 1; j < list.size(); ++j)
			{
				if(list.get(i).getYearRelease() > list.get(j).getYearRelease())
				{
					Collections.swap(list, i, j);
				}
			}
		} 
	
		XuatDanhSach(list);
		/* ================================================================== */
		
		/* Câu 3: Tính tổng giá bán của các quyển sách theo loại sách */
		System.out.print("\nCau 3: Tinh tong gia ban cua cac quyen sach theo loai sach\n");
		System.out.print("Loai sach\tTong gia\n");
		for(int i = 0; i < list.size(); ++i)
		{
			Boolean check = true;
			for(int j = i - 1; j >= 0; --j)
			{
				if(list.get(i).getCategorybook().equals(list.get(j).getCategorybook()))
				{
					check = false;
					break;
				}
			}
			if(check)
			{
				double sumPrice = list.get(i).getPriceOnOrder();
				for(int j = i + 1; j < list.size(); ++j)
				{
					if(list.get(i).getCategorybook().equals(list.get(j).getCategorybook()))
					{
						sumPrice += list.get(j).getPriceOnOrder();
					}
				}
				System.out.print(list.get(i).getCategorybook() + "\t\t" + sumPrice + "\n");
			}
		}
		/* ================================================================== */
		
		/* Câu 4: Thêm/Xóa/Sửa */
		System.out.print("\nCau 4: Them sach\n");
		System.out.print("Nhap thong tin quyen sach can them");
		String cate, title, author;
		int year;
		double price;
        Scanner sc = new Scanner(System.in);
        Boolean check;
        do {
        	check = true;
        	
        	System.out.print("\nNhap title: ");
        	title = sc.nextLine();
        	
        	for(int i = 0; i < list.size(); ++i)
        	{
        		if(list.get(i).getTitlebook().equals(title))
        		{
        			check = false;
        			break;
        		}
        	}
        	
        	if(check == false)
        		System.out.print("\nTitle nay da bi trung. Xin nhap lai!");
        }while(check == false);
        
        System.out.print("\nNhap category: ");
        cate = sc.nextLine();
        
        System.out.print("\nNhap author: ");
        author = sc.nextLine();
        
        System.out.print("\nNhap year: ");
        year = sc.nextInt();
		
        System.out.print("\nNhap price: ");
        price = sc.nextDouble();
        
        info newbook = new info(cate, title, author, year, price);
        list.add(newbook);
        System.out.print("\nDa them sach thanh cong. Hien thi lai danh sach");
        XuatDanhSach(list);
        
        System.out.print("\nCau 4: Xoa sach\n");
		System.out.print("Nhap thong tin quyen sach can xoa");
		String TitleRemove;
		sc = new Scanner(System.in);
		System.out.print("\nNhap title quyen sach can xoa: ");
		TitleRemove = sc.nextLine();
		
		check = false;
		for(int i = 0; i < list.size(); ++i)
		{
			if(list.get(i).getTitlebook().equals(TitleRemove))
			{
				check = true;
				list.remove(i);
				i--;
				break;
			}
		}
		if(check == false)
			System.out.print("\nKhong tim thay title quyen sach can xoa");
		else
		{
			System.out.print("\nDa xoa quyen sach. Hien thi lai danh sach");
	        XuatDanhSach(list);
		}
		
		/* ================================================================== */
		
		/* Câu 5: Lưu lại danh sách vào file book.dat */
		System.out.print("\nCau 5: Luu lai danh sach vao file book.dat\n");
		try {
			   //Bước 1: Tạo đối tượng luồng và liên kết nguồn dữ liệu
			   FileOutputStream fos = new FileOutputStream("book.dat");
			   DataOutputStream dos = new DataOutputStream(fos);
			  
			   //Bước 2: Ghi dữ liệu
			   
			   dos.writeBytes(list.get(0).getCategorybook());
			   dos.writeBytes("|");
			   dos.writeBytes(list.get(0).getTitlebook());
			   dos.writeBytes("|");
			   dos.writeBytes(list.get(0).getAuthorbook());
			   dos.writeBytes("|");
			   dos.writeBytes(String.valueOf(list.get(0).getYearRelease()));
			   dos.writeBytes("|");
			   dos.writeBytes(String.valueOf(list.get(0).getPriceOnOrder()));
			   
			   
			   for(int i = 1; i < list.size(); ++i)
			   {
				   dos.writeBytes("\n");
				   dos.writeBytes(list.get(i).getCategorybook());
				   dos.writeBytes("|");
				   dos.writeBytes(list.get(i).getTitlebook());
				   dos.writeBytes("|");
				   dos.writeBytes(list.get(i).getAuthorbook());
				   dos.writeBytes("|");
				   dos.writeBytes(String.valueOf(list.get(i).getYearRelease()));
				   dos.writeBytes("|");
				   dos.writeBytes(String.valueOf(list.get(i).getPriceOnOrder()));
			   }

			   
			   //Bước 3: Đóng luồng
			   fos.close();
			   dos.close();
			   System.out.println("Da ghi thanh cong danh sach vao file book.dat!");
			  } catch (IOException ex) {
			    ex.printStackTrace();
			  } 
	}
}

 

Revise this Paste

Your Name: Code Language: