[Java] Exercise Java library management
Threads:
A library to manage documents include, Book, Journal, Newspaper
+ Each material properties: Code document, Imprint, Number release.
+ The type of management needed: Author Name, number of pages
+ The magazine should manage: Issue, May release
+ The report should manage: release date.
1. Construction of class to manage documents on how to re-use the most.
2. Construction class QLThuVien installation methods perform the following tasks:
+ Enter the information for a list of n documents
+ Displays information about the document entered
Code of Mod Joe in 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(); } }
But to do so if you want to get any information from a 1 specific object is not (VD output name of the author of the document if it is of the i-th) and after all I had to exchange 1 The code is as follows satisfied that.
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(); } }
Recent Comments