0% found this document useful (0 votes)
63 views3 pages

Lab 7 Hoh Jia Da: //librarybook Class

This document contains code for a LibraryBook class with methods to set and get the title, author, page count, and price of books. It also includes a LibraryDriver class with a main method that uses a Scanner to prompt a user to input book details, creates a LibraryBook object, and adds it to an ArrayList to print the book details. The output shows the program running twice to accept book details from the user and print the LibraryBook details.

Uploaded by

Hoh Jia Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views3 pages

Lab 7 Hoh Jia Da: //librarybook Class

This document contains code for a LibraryBook class with methods to set and get the title, author, page count, and price of books. It also includes a LibraryDriver class with a main method that uses a Scanner to prompt a user to input book details, creates a LibraryBook object, and adds it to an ArrayList to print the book details. The output shows the program running twice to accept book details from the user and print the LibraryBook details.

Uploaded by

Hoh Jia Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 7 Hoh Jia Da

Notebook: First Notebook


Created: 6/5/2020 12:52 PM Updated: 6/5/2020 12:57 PM
Author: [email protected]

//LibraryBook Class

public class LibraryBook {


private String title;
private String author;
private int pageCount;
private double price;

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("Enter the name of the author:");


String author = scan.next();
libBook.setAuthor(author);
list.add(libBook);

System.out.println("Enter the page count:");


int pageCount =scan.nextInt();
libBook.setPageCount(pageCount);
list.add(libBook);

System.out.println("Enter the price:");


double price = scan.nextDouble();
libBook.setPrice(price);
list.add(libBook);
}
}
catch (IllegalArgumentException e){
System.out.println(e);

}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:

You might also like