Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A class to represent a generic information carrier
- class Carrier {
- // Private instance variables to store the common attributes of all carriers
- private String type; // The type of the carrier (book, magazine, etc.)
- private String author; // The author of the carrier
- private String title; // The title of the carrier
- private int year; // The year of publication of the carrier
- private boolean status; // The status of the carrier (free or busy)
- // A constructor that takes all the parameters and assigns them to the instance variables
- public Carrier(String type, String author, String title, int year, boolean status) {
- this.type = type;
- this.author = author;
- this.title = title;
- this.year = year;
- this.status = status;
- }
- // A constructor that takes no parameters and assigns default values to the instance variables
- public Carrier() {
- this.type = "Unknown";
- this.author = "Unknown";
- this.title = "Unknown";
- this.year = 0;
- this.status = true;
- }
- // Public getters and setters for the private instance variables
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- // Input validation: the year should be positive and not greater than the current year
- if (year > 0 && year <= java.time.Year.now().getValue()) {
- this.year = year;
- } else {
- System.out.println("Invalid year");
- }
- }
- public boolean getStatus() {
- return status;
- }
- public void setStatus(boolean status) {
- this.status = status;
- }
- // A method to display the details of the carrier
- public void display() {
- System.out.println("Type: " + type);
- System.out.println("Author: " + author);
- System.out.println("Title: " + title);
- System.out.println("Year: " + year);
- System.out.println("Status: " + (status ? "Free" : "Busy"));
- System.out.println();
- }
- }
- // A class to represent a book as a subclass of Carrier
- class Book extends Carrier {
- // Private instance variable to store the number of pages of the book
- private int pages;
- // A constructor that takes all the parameters and passes them to the superclass constructor
- // and assigns the pages to the instance variable
- public Book(String author, String title, int year, boolean status, int pages) {
- super("Book", author, title, year, status);
- this.pages = pages;
- }
- // A constructor that takes no parameters and passes default values to the superclass constructor
- // and assigns a default value to the pages
- public Book() {
- super("Book", "Unknown", "Unknown", 0, true);
- this.pages = 0;
- }
- // A public getter and setter for the private instance variable
- public int getPages() {
- return pages;
- }
- public void setPages(int pages) {
- // Input validation: the pages should be positive
- if (pages > 0) {
- this.pages = pages;
- } else {
- System.out.println("Invalid pages");
- }
- //continue with the code
- }
- // A method to override the display method of the superclass and add the pages
- @Override
- public void display() {
- super.display();
- System.out.println("Pages: " + pages);
- System.out.println();
- }
- }
- // A class to represent a virtual library that contains an array of carriers
- class Library {
- // Private instance variable to store the array of carriers
- private Carrier[] carriers;
- // A constructor that takes the size of the array and creates a new array of carriers
- public Library(int size) {
- carriers = new Carrier[size];
- }
- // A method to add a carrier to the array at a given index
- public void addCarrier(int index, Carrier carrier) {
- // Input validation: the index should be within the bounds of the array
- if (index >= 0 && index < carriers.length) {
- carriers[index] = carrier;
- } else {
- System.out.println("Invalid index");
- }
- }
- // A method to search for free carriers and display their details
- public void searchFreeCarriers() {
- System.out.println("Free carriers:");
- for (Carrier carrier : carriers) {
- // Check if the carrier is not null and its status is true (free)
- if (carrier != null && carrier.getStatus()) {
- carrier.display();
- }
- }
- }
- // A method to search for busy carriers and display their details
- public void searchBusyCarriers() {
- System.out.println("Busy carriers:");
- for (Carrier carrier : carriers) {
- // Check if the carrier is not null and its status is false (busy)
- if (carrier != null && !carrier.getStatus()) {
- carrier.display();
- }
- }
- }
- }
- // A class to test the application
- class Test {
- public static void main(String[] args) {
- // Create a new library with a size of 5
- Library library = new Library(5);
- // Create some carriers and add them to the library
- Carrier c1 = new Book("J.K. Rowling", "Harry Potter and the Philosopher's Stone", 1997, true, 223);
- library.addCarrier(0, c1);
- Carrier c2 = new Book("George Orwell", "1984", 1949, false, 328);HarryPotter
- library.addCarrier(1, c2);
- Carrier c3 = new Carrier("Magazine", "National Geographic", "The Future of Medicine", 2020, true);
- library.addCarrier(2, c3);
- Carrier c4 = new Carrier("CD-ROM", "Microsoft", "Windows XP", 2001, false);
- library.addCarrier(3, c4);
- Carrier c5 = new Carrier("Audio-CD", "Queen", "Bohemian Rhapsody", 1975, true);
- library.addCarrier(4, c5);
- // Search for free carriers and display their details
- library.searchFreeCarriers();
- // Search for busy carriers and display their details
- library.searchBusyCarriers();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement