Advertisement
CR7CR7

Virtual-Library

Apr 8th, 2023
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. // A class to represent a generic information carrier
  2. class Carrier {
  3.   // Private instance variables to store the common attributes of all carriers
  4.   private String type; // The type of the carrier (book, magazine, etc.)
  5.   private String author; // The author of the carrier
  6.   private String title; // The title of the carrier
  7.   private int year; // The year of publication of the carrier
  8.   private boolean status; // The status of the carrier (free or busy)
  9.  
  10.   // A constructor that takes all the parameters and assigns them to the instance variables
  11.   public Carrier(String type, String author, String title, int year, boolean status) {
  12.     this.type = type;
  13.     this.author = author;
  14.     this.title = title;
  15.     this.year = year;
  16.     this.status = status;
  17.   }
  18.  
  19.   // A constructor that takes no parameters and assigns default values to the instance variables
  20.   public Carrier() {
  21.     this.type = "Unknown";
  22.     this.author = "Unknown";
  23.     this.title = "Unknown";
  24.     this.year = 0;
  25.     this.status = true;
  26.   }
  27.  
  28.   // Public getters and setters for the private instance variables
  29.   public String getType() {
  30.     return type;
  31.   }
  32.  
  33.   public void setType(String type) {
  34.     this.type = type;
  35.   }
  36.  
  37.   public String getAuthor() {
  38.     return author;
  39.   }
  40.  
  41.   public void setAuthor(String author) {
  42.     this.author = author;
  43.   }
  44.  
  45.   public String getTitle() {
  46.     return title;
  47.   }
  48.  
  49.   public void setTitle(String title) {
  50.     this.title = title;
  51.   }
  52.  
  53.   public int getYear() {
  54.     return year;
  55.   }
  56.  
  57.   public void setYear(int year) {
  58.     // Input validation: the year should be positive and not greater than the current year
  59.     if (year > 0 && year <= java.time.Year.now().getValue()) {
  60.       this.year = year;
  61.     } else {
  62.       System.out.println("Invalid year");
  63.     }
  64.   }
  65.  
  66.   public boolean getStatus() {
  67.     return status;
  68.   }
  69.  
  70.   public void setStatus(boolean status) {
  71.     this.status = status;
  72.   }
  73.  
  74.   // A method to display the details of the carrier
  75.   public void display() {
  76.     System.out.println("Type: " + type);
  77.     System.out.println("Author: " + author);
  78.     System.out.println("Title: " + title);
  79.     System.out.println("Year: " + year);
  80.     System.out.println("Status: " + (status ? "Free" : "Busy"));
  81.     System.out.println();
  82.   }
  83. }
  84.  
  85. // A class to represent a book as a subclass of Carrier
  86. class Book extends Carrier {
  87.   // Private instance variable to store the number of pages of the book
  88.   private int pages;
  89.  
  90.   // A constructor that takes all the parameters and passes them to the superclass constructor
  91.   // and assigns the pages to the instance variable
  92.   public Book(String author, String title, int year, boolean status, int pages) {
  93.     super("Book", author, title, year, status);
  94.     this.pages = pages;
  95.   }
  96.  
  97.   // A constructor that takes no parameters and passes default values to the superclass constructor
  98.   // and assigns a default value to the pages
  99.   public Book() {
  100.     super("Book", "Unknown", "Unknown", 0, true);
  101.     this.pages = 0;
  102.   }
  103.  
  104.   // A public getter and setter for the private instance variable
  105.   public int getPages() {
  106.     return pages;
  107.   }
  108.  
  109.   public void setPages(int pages) {
  110.     // Input validation: the pages should be positive
  111.     if (pages > 0) {
  112.       this.pages = pages;
  113.     } else {
  114.       System.out.println("Invalid pages");
  115.     }
  116. //continue with the code
  117.  }
  118.  
  119.   // A method to override the display method of the superclass and add the pages
  120.   @Override
  121.   public void display() {
  122.     super.display();
  123.     System.out.println("Pages: " + pages);
  124.     System.out.println();
  125.   }
  126. }
  127.  
  128. // A class to represent a virtual library that contains an array of carriers
  129. class Library {
  130.   // Private instance variable to store the array of carriers
  131.   private Carrier[] carriers;
  132.  
  133.   // A constructor that takes the size of the array and creates a new array of carriers
  134.   public Library(int size) {
  135.     carriers = new Carrier[size];
  136.   }
  137.  
  138.   // A method to add a carrier to the array at a given index
  139.   public void addCarrier(int index, Carrier carrier) {
  140.     // Input validation: the index should be within the bounds of the array
  141.     if (index >= 0 && index < carriers.length) {
  142.       carriers[index] = carrier;
  143.     } else {
  144.       System.out.println("Invalid index");
  145.     }
  146.   }
  147.  
  148.   // A method to search for free carriers and display their details
  149.   public void searchFreeCarriers() {
  150.     System.out.println("Free carriers:");
  151.     for (Carrier carrier : carriers) {
  152.       // Check if the carrier is not null and its status is true (free)
  153.       if (carrier != null && carrier.getStatus()) {
  154.         carrier.display();
  155.       }
  156.     }
  157.   }
  158.  
  159.   // A method to search for busy carriers and display their details
  160.   public void searchBusyCarriers() {
  161.     System.out.println("Busy carriers:");
  162.     for (Carrier carrier : carriers) {
  163.       // Check if the carrier is not null and its status is false (busy)
  164.       if (carrier != null && !carrier.getStatus()) {
  165.         carrier.display();
  166.       }
  167.     }
  168.   }
  169. }
  170.  
  171. // A class to test the application
  172. class Test {
  173.   public static void main(String[] args) {
  174.     // Create a new library with a size of 5
  175.     Library library = new Library(5);
  176.  
  177.     // Create some carriers and add them to the library
  178.     Carrier c1 = new Book("J.K. Rowling", "Harry Potter and the Philosopher's Stone", 1997, true, 223);
  179.     library.addCarrier(0, c1);
  180.  
  181.     Carrier c2 = new Book("George Orwell", "1984", 1949, false, 328);HarryPotter
  182.     library.addCarrier(1, c2);
  183.  
  184.     Carrier c3 = new Carrier("Magazine", "National Geographic", "The Future of Medicine", 2020, true);
  185.     library.addCarrier(2, c3);
  186.  
  187.     Carrier c4 = new Carrier("CD-ROM", "Microsoft", "Windows XP", 2001, false);
  188.     library.addCarrier(3, c4);
  189.  
  190.     Carrier c5 = new Carrier("Audio-CD", "Queen", "Bohemian Rhapsody", 1975, true);
  191.     library.addCarrier(4, c5);
  192.  // Search for free carriers and display their details
  193.     library.searchFreeCarriers();
  194.  
  195.     // Search for busy carriers and display their details
  196.     library.searchBusyCarriers();
  197.   }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement