[Java的] 锻炼Java库管理

主题:

库来管理文件包括:, 书, 刊物, 报纸
+ 每个文件都有属性: 文档代码, 版本说明, 版本号.
+ 这些书需要管理: 作者名称, 页数
+ 期刊需要管理: 发行数量, 可能释放
+ 报纸应管理: 发布日期.
1. 施工层来管理如何得到最重用的文档类型.
2. 建设QLThuVien类安装方法执行以下任务:
+ 输入信息的N个文档的列表
+ 显示有关导入文档信息

乔国防部代码congdongjava.com

 import java.util.Scanner;

class TaiLieu {
    String maTaiLieu, tenNhaXuatBan;
    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 = scan.nextInt();
        //scan.nextLine();
        //scan.close();
    }

    public void output( ) {
        System.out.printf("%20s %20s %20s %20s %20s %20s %20s %20sn",
                "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");
        System.out.printf("%20s %20s %20d", maTaiLieu,
                tenNhaXuatBan, soBanPhatHanh);
    }
    public void close() {
            scan.close();
    }
}

class Sach extends TaiLieu implements Type {
    String tenTacGia;
    int soTrang;

    @Override
    public void input( ) {
        super.input( );
        System.out.print("Nhap ten tac gia cuon sach: ");
        tenTacGia = scan.nextLine();
        System.out.print("Nhap so trang cuon sach: ");
        soTrang = scan.nextInt();
        //scan.nextLine();
        //scan.close();
    }

    public void output( ) {
        super.output();
        System.out.printf("%20s %20d %20s %20s %20s", tenTacGia,
                soTrang, " ", " ", " ");
    }
}

class TapChi extends TaiLieu implements Type {
    String soPhatHanh;
    int thangPhatHanh;

    @Override
    public void input( ) {

    }

    public void output( ) {
        System.out.printf("%20s %20s %20s %20d %20s", " ", " ",
                soPhatHanh, thangPhatHanh, " ");
    }
}

class Bao extends TaiLieu implements Type {
    int ngayPhatHanh;

    @Override
    public void input( ) {

    }

    public void output( ) {
        System.out.printf("%20s %20s %20s %20d %20s", " ", " ", " ",
                ngayPhatHanh, " ");
    }
}

interface Type {
    public void input( );

    public void output( );
}

public class QuanLyThuVien {
    int n;
    TaiLieu taiLieu[];

    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Nhap so tai lieu: ");
        n = scan.nextInt();
        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 continue;
            taiLieu[i].input();
        }
        scan.close();
    }

    public void output() {
        for (int i = 0; i < n; i++) {
            taiLieu[i].output();
        }
    }

    public static void main(String[] args) {
        QuanLyThuVien ql = new QuanLyThuVien();
        ql.input();
        ql.output();
    }
} 

但是这样做,如果想从任何信息 1 具体的目标是不 (VD输出命名文档的作者,如果它是第i个) 毕竟我不得不换 1 代码如下所示纳.

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 %-20sn",
				"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 %-20sn", 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 %-20sn", " ", " ",
				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 %-20sn", " ", " ", " ",
				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();
	}
}