In: Computer Science
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below:
Part B: Create a Book class containing the following:
public Book()
public Book(string bookId, string bookTitle, int numPages, double price)
Information of all Books
Part C:
Extend the application in Part A (i.e., adding code in the Program class) to become a BookStore Application by making use of the Book class and completing the following tasks:
private static void GetBookData(int num, Book[] books),
to fill an array of books. The method must fill the array with Books which are constructed from user input information (which must be prompted for). Along with the prompt for a book id, it should display a list of valid book categories and call method in Part A.2 to make sure the inputted book id is a valid format. If not the user is prompted to re-enter a valid book id.
public static void DisplayAllBooks(Book[] books),
to display information of all books that have been entered including book id, title, number of pages and price. This method should call the ToString method that has been created in Book class.
private static void GetLists(int num, Book[] books),
to display the valid book categories, and then continuously prompts the user for category codes and displays the information of all books in the category as well as the number of books in this category.
Appropriate messages are displayed if the entered category code is not a valid code.
Then call method in Part C.1 to create an array of books.
Then call method in Part C.2 to display all books in the array.
Then call method in Part C.3 to allow the user to input a category code and see information of the category.
Program.java:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Program {
public static int InputValue(int min,int max) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of books:\n");
int n=s.nextInt();
while((n<min || n>max)) {
System.out.println("Enter number between "+min+" and "+max);
n=s.nextInt();
}
return n;
}
public static boolean IsValid(String id) {
if(id.length()!=5) {
return false;
}
else {
String pattern="[A-Z]{2}[0-9]{3}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(id);
if(m.find()) {
return true;
}
else {
return false;
}
}
}
private static void GetBookData(int n,Book[] books) {
Scanner s=new Scanner(System.in);
for(int i=0;i<n;i++) {
System.out.println("Enter book"+(i+1)+" id:");
String bookId=s.nextLine();
boolean a = IsValid(bookId);
while(!a) {
System.out.println("Enter book"+(i+1)+" id in correct format:");
bookId=s.nextLine();
a=IsValid(bookId);
}
if(a) {
System.out.println("Enter the title of book"+(i+1));
String title=s.nextLine();
System.out.println("Enter the number of pages of book"+(i+1));
int numPages=Integer.parseInt(s.nextLine());
System.out.println("Enter the price of book"+(i+1));
double price=Double.parseDouble(s.nextLine());
Book b=new Book(bookId,title,numPages,price);
b.setBookId(bookId);
b.bookId=b.getBookId();
b.setBookCategory(bookId);
books[i]=b;
}
}
System.out.println("\n");
}
public static void DisplayAllBooks(Book[] books) {
System.out.println("Total books:");
for(int i=0;i<books.length;i++) {
System.out.println(books[i]);
}
System.out.println("\n");
}
private static void GetLists(int num,Book[] books) {
Scanner s1=new Scanner(System.in);
Book b=new Book();
System.out.println("The valid book categories are:");
String[] cn = Book.categoryNames;
for(int u=0;u<books.length;u++) {
System.out.println(books[u].bookCategory);
}
for(int i=0;i<num;i++) {
System.out.println("Enter the Category code");
String ccode=s1.next();
int count=0;
if(b.checkCode(ccode)) {
for(int j=0;j<books.length;j++) {
int m=books[j].getCategoryCodeNumber(ccode);
String m1=cn[m];
if(books[j].bookCategory.equals(m1)) {
count++;
System.out.println(books[j]);
}
}
if(count>0) {
System.out.println("The number of books with the category code "+ccode+" are "+count);
}
else {
System.out.println("No books in this category");
}
System.out.println("\n");
}
else {
System.out.println("Invalid code");
}
}
}
public static void main(String[] args) {
int n=InputValue(1,30);
Book[] b=new Book[n];
GetBookData(n,b);
DisplayAllBooks(b);
GetLists(5,b);
}
}
Screenshot:
Book.java:
public class Book {
public static String[] categoryCode= {"CS","IS","SE","SO","MI"};
public static String[] categoryNames= {"Computer Science","Information System","Security","Society","Miscellaneous"};
String bookId;
String bookCategory;
String bookTitle;
int numPages;
double price;
public Book() {
}
public Book(String bookId, String bookTitle, int numPages, double price) {
this.bookId=bookId;
this.bookTitle=bookTitle;
this.numPages=numPages;
this.price=price;
}
public void setBookId(String bookId) {
String s=bookId.substring(0,2);
int flag=0;
for(int i=0;i<categoryCode.length;i++) {
if(s.equals(categoryCode[i])) {
flag=1;
break;
}
}
if(flag==0) {
bookId="MI"+bookId.substring(2,5);
}
this.bookId=bookId;
}
public String getBookId() {
return bookId;
}
public String getBookCategory() {
return bookCategory;
}
public void setBookCategory(String bookId) {
String s=bookId.substring(0,2);
bookCategory=categoryNames[4];
for(int i=0;i<categoryCode.length;i++) {
if(categoryCode[i].equals(s)) {
bookCategory=categoryNames[i];
}
}
}
public boolean checkCode(String c) {
for(int i=0;i<categoryCode.length;i++) {
if(categoryCode[i].equals(c)) {
return true;
}
}
return false;
}
public int getCategoryCodeNumber(String c) {
for(int i=0;i<categoryCode.length;i++) {
if(categoryCode[i].equals(c)) {
return i;
}
}
return 0;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public int getNumPages() {
return numPages;
}
public void setNumPages(int numPages) {
this.numPages = numPages;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookCategory=" + bookCategory + ", bookTitle=" + bookTitle + ", numPages="
+ numPages + ", price=" + price + "]";
}
}
Screenshot:
Screenshot of the output: