[ジャワ] エクササイズのJavaライブラリ管理

スレッド:

ドキュメントを管理するためのライブラリが含まれて, ブック, ジャーナル, 新聞
+ 各材料は、特性を有しています: ドキュメントコード, インプリント, 第リリース.
+ 本を管理する必要性: 著者名, ページ数
+ ジャーナルは、管理する必要があります: 問題, 解放することができます
+ 経営陣は、報告する必要があります: リリース日.
1. ほとんどの再利用を取得する方法についてのドキュメントタイプに建設管理クラス.
2. 建物のインストールQLThuVienクラスのメソッドは、次のタスクを実行します:
+ n個の文書のリストの情報を入力します
+ インポートされた文書に関する情報を表示します

congdongjava.comでジョーのModコード

 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 特定のオブジェクトではありません (それは政策i番目の場合は、文書の作成者に名前を付けるVD出力) そして彼のポストを交換した後、持っていました 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();
	}
}