Advertisement
nguyenvanquan7826

Quan ly thu vien

Sep 23rd, 2013
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package School;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class TaiLieu {
  6.     private String maTaiLieu, tenNhaXuatBan;
  7.     private int soBanPhatHanh;
  8.     Scanner scan = new Scanner(System.in);
  9.  
  10.     public void input() {
  11.         System.out.print("Nhap ma tai lieu: ");
  12.         maTaiLieu = scan.nextLine();
  13.         System.out.print("Nhap ten nha xuat ban: ");
  14.         tenNhaXuatBan = scan.nextLine();
  15.         System.out.print("Nhap so ban phat hanh: ");
  16.         soBanPhatHanh = Integer.parseInt(scan.nextLine());
  17.     }
  18.  
  19.     public void ouputTitle() {
  20.         System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s\n",
  21.                 "Ma Tai Lieu", "Nha Xuat Ban", "So Ban Phat Hanh",
  22.                 "Tac Gia Sach", "So Trang Sach", "So Phat Hanh",
  23.                 "Thang Phat Hanh", "Ngay Phat Hanh");
  24.     }
  25.  
  26.     public void output() {
  27.         System.out.printf("%-20s %-20s %-20d", maTaiLieu, tenNhaXuatBan,
  28.                 soBanPhatHanh);
  29.     }
  30. }
  31.  
  32. class Sach extends TaiLieu {
  33.     private String tenTacGia;
  34.     private int soTrang;
  35.  
  36.     public String getTenTacGia() {
  37.         return tenTacGia;
  38.     }
  39.  
  40.     public void setTenTacGia(String tenTacGia) {
  41.         this.tenTacGia = tenTacGia;
  42.     }
  43.  
  44.     public int getSoTrang() {
  45.         return soTrang;
  46.     }
  47.  
  48.     public void setSoTrang(int soTrang) {
  49.         this.soTrang = soTrang;
  50.     }
  51.  
  52.     @Override
  53.     public void input() {
  54.         Scanner scan = new Scanner(System.in);
  55.         super.input();
  56.         System.out.print("Nhap ten tac gia cuon sach: ");
  57.         tenTacGia = scan.nextLine();
  58.         System.out.print("Nhap so trang cuon sach: ");
  59.         soTrang = Integer.parseInt(scan.nextLine());
  60.     }
  61.  
  62.     public void output() {
  63.         super.output();
  64.         System.out.printf("%-20s %-20d %-20s %-20s %-20s\n", tenTacGia,
  65.                 soTrang, " ", " ", " ");
  66.     }
  67. }
  68.  
  69. class TapChi extends TaiLieu {
  70.     private int soPhatHanh;
  71.     private int thangPhatHanh;
  72.  
  73.     public int getSoPhatHanh() {
  74.         return soPhatHanh;
  75.     }
  76.  
  77.     public void setSoPhatHanh(int soPhatHanh) {
  78.         this.soPhatHanh = soPhatHanh;
  79.     }
  80.  
  81.     public int getThangPhatHanh() {
  82.         return thangPhatHanh;
  83.     }
  84.  
  85.     public void setThangPhatHanh(int thangPhatHanh) {
  86.         this.thangPhatHanh = thangPhatHanh;
  87.     }
  88.  
  89.     @Override
  90.     public void input() {
  91.         Scanner scan = new Scanner(System.in);
  92.         super.input();
  93.         System.out.print("Nhap so phat hanh: ");
  94.         soPhatHanh = Integer.parseInt(scan.nextLine());
  95.         System.out.print("Nhap thang phat hanh: ");
  96.         thangPhatHanh = Integer.parseInt(scan.nextLine());
  97.     }
  98.  
  99.     public void output() {
  100.         super.output();
  101.         System.out.printf("%-20s %-20s %-20s %-20d %-20s\n", " ", " ",
  102.                 soPhatHanh, thangPhatHanh, " ");
  103.     }
  104. }
  105.  
  106. class Bao extends TaiLieu {
  107.     private int ngayPhatHanh;
  108.  
  109.     public int getNgayPhatHanh() {
  110.         return ngayPhatHanh;
  111.     }
  112.  
  113.     public void setNgayPhatHanh(int ngayPhatHanh) {
  114.         this.ngayPhatHanh = ngayPhatHanh;
  115.     }
  116.  
  117.     @Override
  118.     public void input() {
  119.         Scanner scan = new Scanner(System.in);
  120.         super.input();
  121.         System.out.print("Nhap ngay phat hanh: ");
  122.         ngayPhatHanh = Integer.parseInt(scan.nextLine());
  123.     }
  124.  
  125.     public void output() {
  126.         super.output();
  127.         System.out.printf("%-20s %-20s %-20s %-20d %-20s\n", " ", " ", " ",
  128.                 ngayPhatHanh, " ");
  129.     }
  130. }
  131.  
  132. public class QuanLyThuVien {
  133.     int n;
  134.     TaiLieu taiLieu[];
  135.  
  136.     public void input() {
  137.         Scanner scan = new Scanner(System.in);
  138.         System.out.print("Nhap so tai lieu: ");
  139.         n = Integer.parseInt(scan.nextLine());
  140.         taiLieu = new TaiLieu[n];
  141.  
  142.         for (int i = 0; i < n; i++) {
  143.             System.out.print("Nhap loai tai lieu: (sach, tapchi, bao) ");
  144.             String loai = scan.nextLine();
  145.             if (loai.equals("sach"))
  146.                 taiLieu[i] = new Sach();
  147.             else if (loai.equals("tapchi"))
  148.                 taiLieu[i] = new TapChi();
  149.             else if (loai.equals("bao"))
  150.                 taiLieu[i] = new Bao();
  151.             else {
  152.                 --i;
  153.                 continue;
  154.             }
  155.             taiLieu[i].input();
  156.         }
  157.         scan.close();
  158.     }
  159.  
  160.     public void output() {
  161.         taiLieu[0].ouputTitle();
  162.         for (int i = 0; i < n; i++) {
  163.             taiLieu[i].output();
  164.  
  165.         }
  166.     }
  167.  
  168.     // Neu muon lay du lieu tu cac lop con
  169.     public void outputTenTacGia() {
  170.         for (int i = 0; i < n; i++) {
  171.             if (taiLieu[i] instanceof Sach)
  172.                 System.out.println("Tac gia cuon sach thu " + (i + 1)
  173.                         + " la : " + ((Sach) taiLieu[i]).getTenTacGia());
  174.         }
  175.     }
  176.  
  177.     public static void main(String[] args) {
  178.         QuanLyThuVien ql = new QuanLyThuVien();
  179.         ql.input();
  180.         ql.output();
  181.         ql.outputTenTacGia();
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement