Java programming for beginner. Using boolean, loops, switch, array, arraylist, a
ID: 3724463 • Letter: J
Question
Java programming for beginner.
Using boolean, loops, switch, array, arraylist, and/or getter,setter please help me coding the following in JAVA eclipse.
This is a coding for Library Lab. This will create simple library check out system.
Java Project is called LibraryExample.
There are four classes and one Enumerated type. First is called Patron class. Second is Book class. Third is BookGenre enumerated type. Forth is Transaction class. Fifth is Library class that uses Patron and Book class.
1. The Patron Class should have the user's name, and library card number (firstName, lastName, and cardNumber are String). Use both Public and Private.
2. The Book Class should have ISBN, title, author as strings. Create getter and setter.
The BookGenre should have genres as enumerous type. (Art, Fantasy, Drama, and Horror)
3. Create Transaction class.
4. Create a Library class that uses the Book and Patron Class.
========================================================================
You can code anyway you want, but in simple way for beginners to understand.
Thank you!
Explanation / Answer
package libraryapp;
public class Book {
private String ISBN;
private String title;
private String author ;
public Book() {
}
//getters and setters
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
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;
}
@Override
public String toString() {
return "Book [ISBN=" + ISBN + ", title=" + title + ", author=" + author + "]";
}
}
package libraryapp;
public enum BookGenre {
Art, Fantasy, Drama,Horror;
}
package libraryapp;
public class Library {
private Book book;
private Patron patron;
public Library() {
}
// Getters and Setters
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Patron getPatron() {
return patron;
}
public void setPatron(Patron patron) {
this.patron = patron;
}
@Override
public String toString() {
return "Library [book=" + book + ", patron=" + patron + "]";
}
}
package libraryapp;
public class Patron {
//use of public
public String firstName;
public String lastName;
//use of private
private String cardNumber ;
//Default Constructor
public Patron() {
}
//Parameterized Constructor
public Patron(String firstName, String lastName, String cardNumber) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.cardNumber = cardNumber;
}
//Getters and Setters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
//Will be used to print the object
@Override
public String toString() {
return "Patron [firstName=" + firstName + ", lastName=" + lastName + ", cardNumber=" + cardNumber + "]";
}
}
package libraryapp;
public class Transaction {
//Let us use BookGenre for Transaction
//Assuming transaction amount ,
//Book are the variables here.
private double amount;
private Book book;
private BookGenre bookGenre;
public Transaction() {
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public BookGenre getBookGenre() {
return bookGenre;
}
public void setBookGenre(BookGenre bookGenre) {
this.bookGenre = bookGenre;
}
@Override
public String toString() {
return "Transaction [amount=" + amount + ", book=" + book + ", bookGenre=" + bookGenre + "]";
}
}
package libraryapp;
//This class is used to test the methods that are created in different classes.
//We will try to use some basics of java here
public class TestLibraryApp {
//Main method , program will start from here
public static void main(String[] args) {
//Let's create some Books and add them to Library.
//First Create a library
Library library=new Library();
//create a book
Book book=new Book();
//use of setters
book.setAuthor("XYXXXXX");
book.setISBN("iiiiiiiiii");
book.setTitle("Title of the Book");
//create Patron
//Using Constructor
Patron patron=new Patron("aaaa", "bbbb", "12233333");
//set Book in Library
library.setBook(book);
//set Patron to library
library.setPatron(patron);
//printing Book,Patron,Library
//use of toString Method
System.out.println(book);
System.out.println(patron);
System.out.println(library);
//Using Transaction
Transaction transaction=new Transaction();
transaction.setAmount(100);
transaction.setBook(book);
//use of Enum
transaction.setBookGenre(BookGenre.Drama);
//print transaction
System.out.println(transaction);
}
}
Output
Book [ISBN=iiiiiiiiii, title=Title of the Book, author=XYXXXXX]
Patron [firstName=aaaa, lastName=bbbb, cardNumber=12233333]
Library [book=Book [ISBN=iiiiiiiiii, title=Title of the Book, author=XYXXXXX], patron=Patron [firstName=aaaa, lastName=bbbb, cardNumber=12233333]]
Transaction [amount=100.0, book=Book [ISBN=iiiiiiiiii, title=Title of the Book, author=XYXXXXX], bookGenre=Drama]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.