Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package School;
- import java.util.Scanner;
- class TaiLieu {
- private String maTaiLieu, tenNhaXuatBan;
- private int soBanPhatHanh;
- Scanner scan = new Scanner(System.in);
- public void input() {
- System.out.print("Nhap ma tai lieu: ");
- maTaiLieu = scan.nextLine();
- System.out.print("Nhap ten nha xuat ban: ");
- tenNhaXuatBan = scan.nextLine();
- System.out.print("Nhap so ban phat hanh: ");
- soBanPhatHanh = Integer.parseInt(scan.nextLine());
- }
- public void ouputTitle() {
- System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s\n",
- "Ma Tai Lieu", "Nha Xuat Ban", "So Ban Phat Hanh",
- "Tac Gia Sach", "So Trang Sach", "So Phat Hanh",
- "Thang Phat Hanh", "Ngay Phat Hanh");
- }
- public void output() {
- System.out.printf("%-20s %-20s %-20d", maTaiLieu, tenNhaXuatBan,
- soBanPhatHanh);
- }
- }
- class Sach extends TaiLieu {
- private String tenTacGia;
- private int soTrang;
- public String getTenTacGia() {
- return tenTacGia;
- }
- public void setTenTacGia(String tenTacGia) {
- this.tenTacGia = tenTacGia;
- }
- public int getSoTrang() {
- return soTrang;
- }
- public void setSoTrang(int soTrang) {
- this.soTrang = soTrang;
- }
- @Override
- public void input() {
- Scanner scan = new Scanner(System.in);
- super.input();
- System.out.print("Nhap ten tac gia cuon sach: ");
- tenTacGia = scan.nextLine();
- System.out.print("Nhap so trang cuon sach: ");
- soTrang = Integer.parseInt(scan.nextLine());
- }
- public void output() {
- super.output();
- System.out.printf("%-20s %-20d %-20s %-20s %-20s\n", tenTacGia,
- soTrang, " ", " ", " ");
- }
- }
- class TapChi extends TaiLieu {
- private int soPhatHanh;
- private int thangPhatHanh;
- public int getSoPhatHanh() {
- return soPhatHanh;
- }
- public void setSoPhatHanh(int soPhatHanh) {
- this.soPhatHanh = soPhatHanh;
- }
- public int getThangPhatHanh() {
- return thangPhatHanh;
- }
- public void setThangPhatHanh(int thangPhatHanh) {
- this.thangPhatHanh = thangPhatHanh;
- }
- @Override
- public void input() {
- Scanner scan = new Scanner(System.in);
- super.input();
- System.out.print("Nhap so phat hanh: ");
- soPhatHanh = Integer.parseInt(scan.nextLine());
- System.out.print("Nhap thang phat hanh: ");
- thangPhatHanh = Integer.parseInt(scan.nextLine());
- }
- public void output() {
- super.output();
- System.out.printf("%-20s %-20s %-20s %-20d %-20s\n", " ", " ",
- soPhatHanh, thangPhatHanh, " ");
- }
- }
- class Bao extends TaiLieu {
- private int ngayPhatHanh;
- public int getNgayPhatHanh() {
- return ngayPhatHanh;
- }
- public void setNgayPhatHanh(int ngayPhatHanh) {
- this.ngayPhatHanh = ngayPhatHanh;
- }
- @Override
- public void input() {
- Scanner scan = new Scanner(System.in);
- super.input();
- System.out.print("Nhap ngay phat hanh: ");
- ngayPhatHanh = Integer.parseInt(scan.nextLine());
- }
- public void output() {
- super.output();
- System.out.printf("%-20s %-20s %-20s %-20d %-20s\n", " ", " ", " ",
- ngayPhatHanh, " ");
- }
- }
- public class QuanLyThuVien {
- int n;
- TaiLieu taiLieu[];
- public void input() {
- Scanner scan = new Scanner(System.in);
- System.out.print("Nhap so tai lieu: ");
- n = Integer.parseInt(scan.nextLine());
- taiLieu = new TaiLieu[n];
- for (int i = 0; i < n; i++) {
- System.out.print("Nhap loai tai lieu: (sach, tapchi, bao) ");
- String loai = scan.nextLine();
- if (loai.equals("sach"))
- taiLieu[i] = new Sach();
- else if (loai.equals("tapchi"))
- taiLieu[i] = new TapChi();
- else if (loai.equals("bao"))
- taiLieu[i] = new Bao();
- else {
- --i;
- continue;
- }
- taiLieu[i].input();
- }
- scan.close();
- }
- public void output() {
- taiLieu[0].ouputTitle();
- for (int i = 0; i < n; i++) {
- taiLieu[i].output();
- }
- }
- // Neu muon lay du lieu tu cac lop con
- public void outputTenTacGia() {
- for (int i = 0; i < n; i++) {
- if (taiLieu[i] instanceof Sach)
- System.out.println("Tac gia cuon sach thu " + (i + 1)
- + " la : " + ((Sach) taiLieu[i]).getTenTacGia());
- }
- }
- public static void main(String[] args) {
- QuanLyThuVien ql = new QuanLyThuVien();
- ql.input();
- ql.output();
- ql.outputTenTacGia();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement