Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language: Java 1. Create a Book class with the following attributes and methods:

ID: 3866999 • Letter: L

Question

Language: Java

1. Create a Book class with the following attributes and methods:

- int: price

- String: title

- Constructor, accessors, mutators, and toString.

2. Create a BookStore class as a driver with the following functionalities

- A text file named “books.txt” for storing the books.

- Each line contains information of one book with the following format:

- bookTitle : price

The program shows the following menu to the user:

1: add books

- Ask the user to enter the books. Once the user entered all books, write the books into the “books.txt” with the given format.

2: show books

- Read the book information from the “books.txt” file into an array of books and print the book information on the screen.

0: exit

Explanation / Answer


Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

____________________

Book.java

public class Book {
// Declaring variables
private int price;
private String title;

// parameterized Constructor
public Book(int price, String title) {
super();
this.price = price;
this.title = title;
}

// Getters and setters
public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "Book [price=" + price + ", title=" + title + "]";
}

}

___________________

BookStore.java

package org.students;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Scanner;

public class BookStore {

public static void main(String[] args) {

//Declaring variables

int price;

String title;

int choice;

// Creating the reference of File and FileWrite classes

File f;

FileWriter fw = null;

// Scanner object is used to get the inputs entered by the user

Scanner sc = new Scanner(System.in);

while (true) {

//Displaying menu

System.out.println(":: Menu ::");

System.out.println("1. Add Books ");

System.out.println("2. Show Books ");

System.out.println("3. Exit ");

//Getting the choice entered by the user

System.out.println("Enter Choice :");

choice = sc.nextInt();

//Based on the user choice the corresponding case will be executed

switch (choice) {

case 1:

{

//Creating an ArrayList which holds Book class Objects

ArrayList < Book > arl = new ArrayList < Book > ();

while (true) {

sc.nextLine();

System.out.print("Enter Book name (-1 to exit ):");

title = sc.nextLine();

if (title.equals("-1")) {

break;

} else {

System.out.print("Enter the price :$");

price = sc.nextInt();

Book b = new Book(price, title);

arl.add(b);

}

}

// Writing the data to the file

try {

f = new File("books.txt");

// If file already exists then write data to the existing

// file

if (f.exists()) {

fw = new FileWriter(f, true);

System.out.println("Data Written to File");

} else {

// If no file already exists.Create new File

f.createNewFile();

fw = new FileWriter(f);

System.out.println("Data Written to File");

}

Iterator itr = arl.iterator();

while (itr.hasNext()) {

Book b = (Book) itr.next();

fw.write(b.getTitle() + ":");

fw.write(b.getPrice() + " ");

}

fw.close();

} catch (Exception e) {

System.out.println("Exception " + e);

}

arl.clear();

continue;

}

case 2:

{

Scanner sc1 = null;

try {

sc1 = new Scanner(new File("books.txt"));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

String str;

String arr[];

//Code which will display the data from the file

while (sc1.hasNext()) {

str = sc1.nextLine();

arr = str.split(":");

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

System.out.println("_________________");

}

continue;

}

case 3:

{

System.out.println("** PROGRAM EXIT **");

break;

}

default:

{

System.out.println("** Invalid Input **");

continue;

}

}

break;

}

}

}

___________________

Output:

:: Menu ::

1. Add Books

2. Show Books

3. Exit

Enter Choice :

1

Enter Book name (-1 to exit ):Spring in Action

Enter the price :$10

Enter Book name (-1 to exit ):Hansel and Gretel

Enter the price :$2

Enter Book name (-1 to exit ):-1

Data Written to File

:: Menu ::

1. Add Books

2. Show Books

3. Exit

Enter Choice :

1

Enter Book name (-1 to exit ):Fundamentals of English Grammar

Enter the price :$2

Enter Book name (-1 to exit ):SG Musical Notation Music Book Stand

Enter the price :$14

Enter Book name (-1 to exit ):-1

Data Written to File

:: Menu ::

1. Add Books

2. Show Books

3. Exit

Enter Choice :

2

_________________

Spring in Action

10

_________________

Hansel and Gretel

2

_________________

Fundamentals of English Grammar

2

_________________

SG Musical Notation Music Book Stand

14

_________________

:: Menu ::

1. Add Books

2. Show Books

3. Exit

Enter Choice :

3

** PROGRAM EXIT **

___________________

Output file:


Spring in Action:10
Hansel and Gretel:2
Fundamentals of English Grammar:2
SG Musical Notation Music Book Stand:14


_____________Could you rate me well.Plz .Thank You