Lab 7 Hoh Jia Da: //librarybook Class
Lab 7 Hoh Jia Da: //librarybook Class
//LibraryBook Class
public LibraryBook () {
}
public LibraryBook(String title, String author, int pageCount,
double price) {
if (title.compareTo(title)==0) {
throw new IllegalArgumentException("Invlaid parameters
in constructor.");
}
else {
this.title = title;
this.author=author;
this.pageCount=pageCount;
this.price=price;
}
}
public void setTitle (String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setAuthor (String author) {
this.author = author;
}
public String getAuthor() {
return author;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPageCount () {
return pageCount;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice () {
return price;
}
public String toString() {
return "Book title: " +title+"\nAuthor: " +author+ "\nPage Count:"
+pageCount+"\nPrice: "+price;
}
}
//LibraryDriver
import java.util.Scanner;
import java.util.ArrayList;
public class LibraryDriver {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
ArrayList <LibraryBook> list = new ArrayList<>();
LibraryBook libBook = new LibraryBook();
while (true) {
try {
System.out.println("Enter 0 to terminate program, 1 to
start program:");
int n = scan.nextInt();
if (n==0) {
System.exit(0);
}
else {
System.out.println("Enter the name of the book title:");
String title = scan.next();
libBook.setTitle(title);
list.add(libBook);
}System.out.println(libBook.toString());
}
}
}
Output:
Enter 0 to terminate program, 1 to start program:
1
Enter the name of the book title:
hoh
Enter the name of the author:
jia
Enter the page count:
10
Enter the price:
10
Book title: hoh
Author: jia
Page Count:10
Price: 10.0
Enter 0 to terminate program, 1 to start program:
1
Enter the name of the book title:
hoh
Enter the name of the author:
jia
Enter the page count:
10
Enter the price:
10
Book title: hoh
Author: jia
Page Count:10
Price: 10.0
Enter 0 to terminate program, 1 to start program: