Bài tập java CODE quản lý sách trong thư viện

11 1.2K 1
Bài tập java CODE quản lý sách trong thư viện

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Thư viện X quản lý danh sách loại sách Thông tin loại sách: Sách giáo khoa: Mã sách, đơn giá, số lượng, nhà xuất bản, tình trạng (mới, cũ) Nếu tình trạng sách thành tiền = số lượng * đơn giá Nếu tình trạng sách cũ thành tiền = số lượng * đơn giá * 50% Sách tham khảo: Mã sách, đơn giá, số lượng, nhà xuất bản, thuế, thành tiền = số lượng * đơn giá + thuế Viết chương trình thực yêu cầu sau: Xây dựng lớp với chức thừa kế Nhập xuất danh sách loại sách Tính tổng thành tiền cho loại Tính trung bình cộng đơn giá sách tham khảo Xuất sách giáo khoa nhà xuất X Sach.java package baitapquanlysach; import java.text.SimpleDateFormat; import java.util.Scanner; public class Sach { private String maSach, nhaXuatBan; private double donGia; private int soLuong; Scanner scanner = new Scanner(System.in); public Sach() { super(); } public Sach(String maSach, String nhaXuatBan, double donGia, int soLuong) { super(); this.maSach = maSach; this.nhaXuatBan = nhaXuatBan; this.donGia = donGia; this.soLuong = soLuong; } public String getMaSach() { return maSach; } public void setMaSach(String maSach) { this.maSach = maSach; } public String getNhaXuatBan() { return nhaXuatBan; } public void setNhaXuatBan(String nhaXuatBan) { this.nhaXuatBan = nhaXuatBan; } public double getDonGia() { return donGia; } public void setDonGia(double donGia) { this.donGia = donGia; } public int getSoLuong() { return soLuong; } public void setSoLuong(int soLuong) { this.soLuong = soLuong; } public void nhapSach() { System.out.print("Nhập mã sách: "); maSach = scanner.nextLine(); System.out.print("Nhập tên nhà xuất bản: "); nhaXuatBan = scanner.nextLine(); System.out.print("Nhập đơn giá: "); donGia = scanner.nextDouble(); System.out.print("Nhập số lượng: "); soLuong = scanner.nextInt(); } @Override public String toString() { return "Mã sách: " + this.maSach + ", tên nhà xuất bản: " + this.nhaXuatBan + ", đơn giá: " + this.donGia + ", số lượng: " + this.soLuong; } } SachGiaoKhoa.java package baitapquanlysach; public class SachGiaoKhoa extends Sach { private String tinhTrang; private int number; private double thanhTien; public SachGiaoKhoa() { super(); } public SachGiaoKhoa(String tinhTrang, int number) { super(); this.tinhTrang = tinhTrang; this.number = number; } public String getTinhTrang() { return tinhTrang; } public void setTinhTrang(String tinhTrang) { this.tinhTrang = tinhTrang; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String tinhTrangSach(int x) { switch (number) { case 0: tinhTrang = "cũ"; break; case 1: tinhTrang = "mới"; break; default: break; } return tinhTrang; } public void nhapSach() { super.nhapSach(); System.out.print("Nhập tình trạng sách (0 - cũ/ mới): "); number = scanner.nextInt(); } public String toString() { return super.toString() + ", tình trạng sách: " + this.tinhTrangSach(number); } } SachThamKhao.java package baitapquanlysach; public class SachThamKhao extends Sach { private double thue, thanhTien; public SachThamKhao() { super(); } public SachThamKhao(double thue) { super(); this.thue = thue; } public double getThue() { return thue; } public void setThue(double thue) { this.thue = thue; } public void nhapSach() { super.nhapSach(); System.out.print("Nhập thuế: "); thue = scanner.nextDouble(); } @Override public String toString() { return super.toString() + ", thuế: " + this.thue; } } Main.java package baitapquanlysach; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList arrSachGiaoKhoa = new ArrayList(); ArrayList arrSachThamKhao = new ArrayList(); int soSachGiaoKhoa, soSachThamKhao; double tongTienSachGiaoKhoa = 0, tongTienSachThamKhao = 0, tongDonGiaSachThamKhao = 0, trungBinhCongDonGia = 0; Scanner scanner = new Scanner(System.in); System.out.print("Nhập số sách giáo khoa: "); soSachGiaoKhoa = scanner.nextInt(); System.out.print("Nhập số sách tham khảo: "); soSachThamKhao = scanner.nextInt(); System.out.println("Nhập thông tin sách giáo khoa:"); for (int i = 0; i < soSachGiaoKhoa; i++) { System.out.println("Nhập thông tin sách thứ " + (i + 1) + ":"); SachGiaoKhoa sachGiaoKhoa = new SachGiaoKhoa(); sachGiaoKhoa.nhapSach(); arrSachGiaoKhoa.add(sachGiaoKhoa); } System.out.println("Nhập thông tin sách tham khảo:"); for (int i = 0; i < soSachThamKhao; i++) { System.out.println("Nhập thông tin sách thứ " + (i + 1) + ":"); SachThamKhao sachThamKhao = new SachThamKhao(); sachThamKhao.nhapSach(); arrSachThamKhao.add(sachThamKhao); } for (int i = 0; i < arrSachGiaoKhoa.size(); i++) { if (arrSachGiaoKhoa.get(i).getNumber() == 0) { tongTienSachGiaoKhoa += arrSachGiaoKhoa.get(i).getSoLuong() * arrSachGiaoKhoa.get(i).getDonGia() * 50 / 100; } else if (arrSachGiaoKhoa.get(i).getNumber() == 1) { tongTienSachGiaoKhoa += arrSachGiaoKhoa.get(i).getSoLuong() * arrSachGiaoKhoa.get(i).getDonGia(); } } System.out.println("Tổng tiền sách giáo khoa = " + tongTienSachGiaoKhoa); for (int i = 0; i < arrSachThamKhao.size(); i++) { tongTienSachThamKhao += arrSachThamKhao.get(i).getSoLuong() * arrSachThamKhao.get(i).getDonGia() + arrSachThamKhao.get(i).getThue(); } System.out.println("Tổng tiền sách tham khảo = " + tongTienSachThamKhao); System.out.println(" -Thông tin sách giáo khoa -"); for (int i = 0; i < arrSachGiaoKhoa.size(); i++) { System.out.println(arrSachGiaoKhoa.get(i).toStrin g()); } System.out.println(" -Thông tin sách tham khảo -"); for (int i = 0; i < arrSachThamKhao.size(); i++) { System.out.println(arrSachThamKhao.get(i).toStri ng()); } System.out.println(" -Trung bình cộng đơn giá sách tham khảo -"); for (int i = 0; i < arrSachThamKhao.size(); i++) { tongDonGiaSachThamKhao += arrSachThamKhao.get(i).getDonGia(); trungBinhCongDonGia = tongDonGiaSachThamKhao / arrSachThamKhao.size(); } System.out.println("Trung bình cộng đơn giá sách tham khảo = " + trungBinhCongDonGia); System.out.println(" -Các sách giáo khoa nhà xuất X -"); for (int i = 0; i < arrSachGiaoKhoa.size(); i++) { if (arrSachGiaoKhoa.get(i).getNhaXuatBan().equalsIgnoreC ase("X")) { System.out.println(arrSachGiaoKhoa.get(i).toSt ring()); } } } } ... tình trạng sách (0 - cũ/ mới): "); number = scanner.nextInt(); } public String toString() { return super.toString() + ", tình trạng sách: " + this.tinhTrangSach(number); } } SachThamKhao .java package... toString() { return super.toString() + ", thuế: " + this.thue; } } Main .java package baitapquanlysach; import java. util.ArrayList; import java. util.Scanner; public class Main { public static void main(String[]... System.out.print("Nhập số sách giáo khoa: "); soSachGiaoKhoa = scanner.nextInt(); System.out.print("Nhập số sách tham khảo: "); soSachThamKhao = scanner.nextInt(); System.out.println("Nhập thông tin sách giáo

Ngày đăng: 03/10/2019, 09:33

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan