Đáp Án Đề 3 : Quản Lý Tài Liệu




Quản Lý Tài Liệu

 Một thư viện cần quản lý các tài liệu bao gồm, Sách, Tạp chí, Báo
+ Mỗi tài liệu có các thuộc tính: Mã tài liệu, Tên nhà xuất bản, Số bản phát hành.
+ Các loại sách cần quản lý: Tên tác giả, số trang
+ Các tạp chí cần quản lý: Số phát hành, tháng phát hành
+ Các báo cần quản lý: ngày phát hành.
 1. Xây dựng các lớp để quản lý các loại tài liệu trên sao cho việc sử dụng lại được nhiều nhất
 2. Xây dựng lớp QuanLySach cài đặt các phương thức thực hiện các công việc sau:
- Nhập thông tin về các tài liệu
- Tìm kiếm tài liệu theo loại (sách, báo, tạp chí) hoặc theo mã tài liệu
- Hiển thị thông tin tài liệu tìm kiếm được

  

//Lớp Tài Liệu

public class TaiLieu
{
 protected String mTL;
 protected String nXB;
 protected String sBPH;
 
 public TaiLieu()
 {
  
 }
 public TaiLieu(String mTL,String nXB,int sBPH)
 {
  this.mTL = mTL;
  this.nXB = nXB;
  this.sBPH = sBPH;
 }
 public String thongTin()
 {
  String tt="Ma Tai Lieu: "+mTL+"\nNha Xuat Ban: "+nXB+"\nSo Ban Phat Hanh: "+sBPH;
  return tt;
 }
 public void setmTL(String mTL)
 {
  this.mTL = mTL;
 }
 public String getmTL()
 {
  return mTL;
 }
 public void setnXB(String nXB)
 {
  this.nXB = nXB;
 }
 public String getnXB()
 {
  return nXB;
 }
 public void setsBPH(int sBPH)
 {
  this.sBPH = sBPH;
 }
 public String getsBPH()
 {
  return sBPH;
 } 
 public String toString()
 {
  String thongTin = "Ma Tai Lieu: "+mTL+"\n";
  String thongTin+ = "Nha Xuat Ban: "+nXB+"\n";
  String thongTin+ = "So Ban Phat Hanh: "+sBPH+"\n";
  return thongTin;
 }
 public void nhapThongTin(Scanner sc)
 {
  System.out.print("Nhap ma tai lieu: ");
  mTL = sc.nextLine();
  System.out.print("Nhap nha xuat ban: ");
  nXB = sc.nextLine();
  System.out.print("Nhap so ban phat hanh: ");
  sBPH = sc.nextLine();
 }
}

//Lớp Báo

import java.util.Date
public class Bao extends TaiLieu
{
 private Date nPH;
 
 public Bao()
 {
 
 }
 public Bao(String mTL,String nXB,int sBPH,Date nPH)
 {
  super(mTL,nXB,sBPH);
  this.nPH = nPH;
 }
 public String toString()
 {
  String thongTin = super.toString()+"\n"+"Ngay Phat Hanh: "+nPH;
  return thongTin;
 }
 public void nhapThongTin(Scanner sc)
 {
  super.nhapThongTin(sc);
  System.out.print("Ngay phat hanh(dd-mm-yyyy: )");
  String nPH str = sc.nextLine();
  Simple DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
  
  try
  {
   this.nPH = sdf.parse(nPH str);
  }catch (Exception e)
  {
   System.out.println;
  }
 }
 public Date get nPH()
 {
  return nPH;
 }
 public void set nPH(Date nPH)
 {
  this.nPH = nPH;
 }
}

//Lớp Sách

public class Sach extends TaiLieu
{
 private String tg;
 println int st;
 
 public Sach(String mTL, String nXB, int sBPH, String tg, int st)
 {
  super(mTL,nXB,sBPH);
  this.tg = tg;
  this.st = st;
 }
 public String thongTin()
 {
  String tt = super.thongTin()+"\nTac Gia: "+tg+"\nSo Trang: "+st;
  return tt;
 }
 public String gettg()
 {
  return tg;
 }
 public int getst()
 {
  return st;
 }
 public void settg(String tg)
 {
  this.tg = tg;
 }
 public void setst(int st)
 {
  this.st = st;
 }
}

//Lớp Tạp Chí

public class TapChi extends TaiLieu
{
 private int sph;
 private String tph;
 
 public TapChi(String mTL, String nXB, int sBPH, int sph, String tph)
 {
  super(mTL, nXB, sBPH);
  this.sph = sph;
  this.tph = tph;
 }
 public String thongTin()
 {
  String tt = super.thongTin()+"\nSo Phat Hanh: "+sph+"\nThang Phat Hanh: "+tph;
  return tt;
 }
 public String getsph()
 {
  return sph;
 }
 public int gettph()
 {
  return tph;
 }
 public void setsph(int sph)
 {
  this.sph = sph;
 }
 public void settph(String tph)
 {
  this.tph = tph;
 } 
}

//Lớp Quản Lý Tài Liệu

import java.util.ArrayList;
import java.util.Date;
import java.util.DateFormat;
import java.util.Scanner;
import java.text.SimpleDateFormat;

public class QLTL
{
    private ArrayList<TaiLieu> dstl;
    
    public QLTL(){
        dstl = new ArrayList<TaiLieu>(100);
    }
    
    public void themTaiLieu(TaiLieu tl){
        dstl.add(tl);
    }
    
    public void nhapTaiLieu(Scanner sc){
        int chon;
        String traLoi;
        TaiLieu tl;
        do{
            System.out.print("Loại Tài Liệu Bạn Muốn Nhập(Sach: S, Bao: B, TapChi: T): ");
            c = sc.nextInt(); sc.nextLine();
            
  switch(c)
  {
   case "S": case 'S':
   {
    ds.add(Nhap Sach(sc));
    break;
   }
   case "B" case 'B':
   {
    ds.add(Nhap Bao(sc));
    break;
   }
   case "T" case 'T':
   {
    ds.add(Nhap Tap Chi(sc));
    break;
   }
  }
            tl.nhapThongTin(sc);//Tinh da hinh
            themTaiLieu(tl);
            
            System.out.print("Ban muon nhap nua khong? (Y/N): ");
            traLoi = sc.nextLine();
        }while("Y".equalsIgnoreCase(traLoi));
    }
    
    public void inTaiLieu(){
        for(TaiLieu tl : dstl){
            tl.inThongTin();
            System.out.println("------------");
        }
    }
    
    public void timKiemTaiLieu(Scanner sc){
        System.out.print("Nhap loai tai lieu can tim (Sach,Bao,TapChi): ");
        String loai = sc.nextLine();
        
        if(loai.equalsIgnoreCase("S")){
            for(TaiLieu tl : dstl){
                if(tl instanceof Sach){
                    tl.inThongTin();
                }
            }
        }else if(loai.equalsIgnoreCase("B")){
            for(TaiLieu tl : dstl){
                if(tl instanceof Bao){
                    tl.inThongTin();
                }
            }
        }else if(loai.equalsIgnoreCase("T")){
            for(TaiLieu tl : dstl){
                if(tl instanceof TapChi){
                    tl.inThongTin();
                }
            }
        }
    }
    
    public void timKiemTaiLieu(String mtl){
        for(TaiLieu tl : dstl){
            if(mtl.equals(tl.mtl)){
                tl.inThongTin();
            }
        }
    }
    
    public static void main(String[] args){
        QLTL qltl = new QLTL();
        
        Scanner sc = new Scanner(System.in);
        
        qltl.nhapTaiLieu(sc);
        System.out.println("----------------------");
        
        qltl.inTaiLieu();
        System.out.println("----------------------");
        
        System.out.println("Tim kiem tai lieu theo loai:");
        qltl.timKiemTaiLieu(sc);
        System.out.println("----------------------");
        
        System.out.println("Tai lieu co ma la Bao01 la:");
        qltl.timKiemTaiLieu("Bao01");
    }
}
}

//Lớp Test

public class QuanlyTaiLieu
{
 public static void main(String[]args)
 {
  Bao b1 = new Bao("B001","Hong Duc",328,newDate());
  System.out.println(b1.toString());
  Scanner sc = new Scanner(System.in);
  Bao b2 = newBao();
  b2.nhapThongTin(sc);
  System.out.println(b2);
 }
}





Đăng nhận xét

Cảm Ơn Bạn Đã Để Lại Bình Luận

 

Quản Trị Viên

Lượt xem

Lên Trên