Write a library application using library objects that are made of employee obje
ID: 669137 • Letter: W
Question
Write a library application using library objects that are made of employee objects, patron objects, and book objects. There exists HAS_A relationship between the library object and the other objects. You will use class aggregation (and enumeration) in this exercise and you may use the Course class example from the text as a guide. The UML diagram given below shows the possible (incomplete) class design and the relationships among the classes.
-libraryName: String
-employee: Employee
-patron: Patron
-book: Book
- lastName: String
- firstName: String
- patronClass: PatronClass
- libraryCardNum:int
- title: String
- author: String
- subject: String
- accessionNo:String
Once you have written these classes and any extension that may be needed, use them in an application (such as a driver class named LibraryDriver) that asks the user to enter the employee name, patron name, and book name. Make sure that your program has identifying remarks are in each class file. Your application should have the following components:
1. Create an enumerated type PatronClass that contains the values ADULT, CHILD, SENIOR, and TEACHER. Note that this will require you to create a java source file named PatronClass.java with the declaration “public enum PatronClass {”.
2. As shown in the UML diagram, the class Library contains the fields: libraryName, employee, patron, and book.
3. The class Patron contains the following fields: lastName, firstName, patronClass (stores your enumerated type) and static libraryCardNum that starts at 1000 and increments by 20 as each patron is added. The method setLibraryCardNum is called in the constructor to set the card number after the number.is incremented.
4. The class Book contains the following fields: title, author, subject, and accessionNo.
5. Fully implement all the getters and setters for each of the classes as appropriate.
6. Create three static ArrayLists named employees, and patrons to store empoyees, patrons, and books as they are entered by the user.
7. Provide three methods addEmpoyee, addPatron, and addBook methods in the Library class in order to allow the users to enter information about employees, patrons, and books respectively which will then be stored in the appropriate ArrayList.
8. The driver class named as LibraryDriver will call addEmpoyee, addPatron, and addBook and print a list of all employees, all patrons, and inventory of all books by printing these objects.
9. Override the toString() method for all the classes and format them appropriately. Each field must be on a new line, and separated from the field name by a colon (e.g. Name: value).
-libraryName: String
-employee: Employee
-patron: Patron
-book: Book
+ Library (name: String; employee: Employee; patron: Patron; book:Book)+ getName(): String
+ getemployee: Employee
+ getPatron: Patron
+ getBook:Book
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class LibraryDriver {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter employee details: Enter employee First name : ");
String fn = sc.next();
System.out.println(" Enter employee last name : ");
String ln = sc.next();
Employee emp = new Employee(fn, ln, 1, "");
System.out.println("Enter Patron details: Enter First name : ");
fn = sc.next();
System.out.println(" Enter last name : ");
ln = sc.next();
Patron pat = new Patron(fn, ln, PatronClass.ADULT);
System.out.println("Enter Book details: Enter title : ");
String t = sc.next();
System.out.println(" Enter author name : ");
String a = sc.next();
System.out.println(" Enter subject name : ");
String subject = sc.next();
Book book = new Book(t, a, subject, "ITEM1" );
Library lib = new Library("Library1", null, null ,null);
lib.addEmployee(emp);
lib.addPatron(pat);
lib.addBook(book);
lib.displayEmployees();
lib.displayPatrons();
lib.displayBooks();
}
}
class Library {
static ArrayList<Employee> empList = new ArrayList<Employee>();
static ArrayList<Patron> patList = new ArrayList<Patron>();
static ArrayList<Book> bookList = new ArrayList<Book>();
String libraryName;
Employee employee;
Patron patron;
Book book;
public Library (String name, Employee employee, Patron patron, Book book) {
this.libraryName = name;
this.employee = employee;
this.patron = patron;
this.book = book;
}
public void displayEmployees() {
for(Employee emp: empList)
System.out.println(emp.toString());
}
public void displayPatrons() {
for(Patron obj: patList)
System.out.println(obj.toString());
}
public void displayBooks() {
for(Book obj: bookList)
System.out.println(obj.toString());
}
public void addEmployee(Employee emp) {
empList.add(emp);
}
public void addPatron(Patron pat) {
patList.add(pat);
}
public void addBook(Book book) {
bookList.add(book);
}
public String getLibraryName() {
return libraryName;
}
public void setLibraryName(String libraryName) {
this.libraryName = libraryName;
}
public Employee getEmployee() {
return employee;
}
public Patron getPatron() {
return patron;
}
public Book getBook() {
return book;
}
}
class Patron {
String lastName;
String firstName;
PatronClass patronClass;
int libraryCardNum;
public Patron(String lname, String fname, PatronClass patronClass) {
this.lastName = lname;
this.firstName = fname;
this.patronClass = patronClass;
}
public Patron(Object patron) {
Patron pat = (Patron) patron;
this.lastName = pat.lastName;
this.firstName = pat.firstName;
this.patronClass = pat.patronClass;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public PatronClass getPatronClass() {
return patronClass;
}
public void setPatronClass(PatronClass patronClass) {
this.patronClass = patronClass;
}
public int getLibraryCardNum() {
return libraryCardNum;
}
public void setLibraryCardNum(int libraryCardNum) {
this.libraryCardNum = libraryCardNum;
}
public String toString() {
return (" Patron : name : "+firstName+" "+lastName+" libraryCarNum : "+libraryCardNum+" Patron Class : "+patronClass);
}
}
enum PatronClass {
ADULT, CHILD, SENIOR, TEACHER
}
class Book {
String title;
String author;
String subject;
String accessionNo;
public Book(String title, String author, String subject, String accessionNo) {
this.title = title;
this.author = author;
this.subject = subject;
this.accessionNo = accessionNo;
}
public Book(Object book) {
Book b = (Book) book;
this.title =b.title;
this.author = b.author;
this.subject = b.subject;
this.accessionNo = b.accessionNo;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getAccessionNo() {
return accessionNo;
}
public void setAccessionNo(String accessionNo) {
this.accessionNo = accessionNo;
}
public String toString() {
return (" Book : title : "+title+" author: "+author+" subject : "+subject+" accessionNo : "+accessionNo);
}
}
class Employee {
String lname;
String fname;
String title;
int employeeNo;
public Employee(String lname, String fname, int employeeNo, String title) {
this.lname = lname;
this.fname = fname;
this.title = title;
this.employeeNo = employeeNo;
}
public Employee(Object employee) {
Employee e = (Employee) employee;
this.lname = e.lname;
this.fname = e.fname;
this.title = e.title;
this.employeeNo = e.employeeNo;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getEmployeeNo() {
return employeeNo;
}
public void setEmployeeNo(int employeeNo) {
this.employeeNo = employeeNo;
}
public String toString() {
return (" Employee : name : "+fname+" "+lname+" title : "+title+" employeeNo: "+employeeNo);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.