1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods.
2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory.
3) Write a Java program to read the "books.txt" file and display the book data contained in it on the Console with each field displayed in column, one line per book.
Book.java
public class Book {
//private attributes
private String author;
private String title;
private String year;
private double price;
//constructor that takes in all attributes and sets
them
public Book(String strAuthor, String strTitle, String
strYear, double dblPrice)
{
author = strAuthor;
title = strTitle;
year = strYear;
price = dblPrice;
}
//getter methods
//method that returns author
public String getAuthor()
{
return author;
}
//method that returns title
public String getTitle()
{
return title;
}
//method that returns year
public String getYear()
{
return year;
}
//method that returns price
public double getPrice()
{
return price;
}
//setter methods
//method that sets author name
public void setAuthor(String strAuthor)
{
author = strAuthor;
}
//method that sets title of the book
public void setTitle(String strTitle)
{
title = strTitle;
}
//method that sets year
public void setYear(String strYear)
{
year = strYear;
}
//method that sets price of the book
private void setPrice(double dblPrice)
{
price = dblPrice;
}
//override toString method
public String toString()
{
return getAuthor() + "\t\t" +
getTitle() + "\t\t" + getYear() + "\t\t$" + getPrice();
}
}
EnterBookData.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class EnterBookData {
public static void main(String[] args) throws
IOException {
//create object to the Scanner
class
Scanner input = new
Scanner(System.in);
boolean repeat = true;
String author, title, year;
double price;
String userChoice;
// open the file to write book
data
FileWriter fw = new
FileWriter("book.txt");
BufferedWriter bw = new
BufferedWriter(fw);
//repeat a loop to read
book details
while(repeat)
{
//prompt and
read author name
System.out.print("Enter the name of the Author: " );
author =
input.nextLine();
//prompt and
read book title
System.out.print("Enter the title of the book: ");
title =
input.nextLine();
//prompt and
read year of publishing
System.out.print("Enter the year of publishing: ");
year =
input.nextLine();
//prompt and
read price
System.out.print("Enter the price: $");
price =
input.nextDouble();
//create a book
object
Book bookObj =
new Book(author, title, year, price);
//write book
details to text file
//as author
names and title contain spaces we are here writing each detail in a
separate line
bw.write(bookObj.getAuthor() + "\n");
bw.write(bookObj.getTitle() + "\n");
bw.write(bookObj.getYear() + "\n");
bw.write(bookObj.getPrice() + "\n");
//prompt and
read if user wants to enter one more book detail
System.out.print("Do you want to enter another book details (y/n)?
");
input.nextLine();
userChoice =
input.nextLine();
//terminate the
loop if the user wants to exit
if(userChoice.equals("n") || userChoice.equals("N"))
repeat = false;
}
//close the writers
bw.close();
fw.close();
input.close();
}
}
DisplayBookData.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DisplayBookData {
public static void main(String[] args) throws
FileNotFoundException {
// pass the path to the file as a
parameter
File file = new
File("book.txt");
Scanner input = new
Scanner(file);
String author = "", title = "",
year = "";
double price = 0.0;
//display the header
System.out.println("Author\t\tTitle\t\t\t\tYear\t\tPrice");
//run a loop until the end of the
file is reached
while (input.hasNextLine())
{
//read author
name
author =
input.nextLine();
//read
title
title =
input.nextLine();
//read
year
year =
input.nextLine();
//read
price
price =
Double.parseDouble(input.nextLine());
//create a Book
object
Book bookObj =
new Book(author, title, year, price);
//display the
book details
System.out.println(bookObj.toString());
}
input.close();
}
}
Output
Program Screenshots
File generated
1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4...
1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...
Create a java program that asks the user for their title (String) and their health level (int). Display their title and health level with ‘:’ between them. For example if they enter Wizard and 9 then display- Wizard:9 Thank you in advance
Write a definition for a class named Book with attributes title, price and author, where author is a Contact object and title is a String, and price is a float number. You also need to define the Contact class, which has attributes name and email, where name and email are both String. Instantiate a couple of Book objects that represents books with title, price and author (You can come up with the attributes values for each object) Write a function...
High School Assignment: please keep simple, use for loops but preferably no arrays In python, Instructions Write a program to input ten book titles and author's names then save them to a file named books.txt. For each book, the user will first input the title, then input the first name, then the last name of the author, all on separate lines. In addition to saving the books in the file, please output, using the print method, the information you've gathered...
please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...
(JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...
Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)). 3.) Program will read movies from a local text file named movies.txt in the current project directory in Eclipse. 4.) Each line in the text file contains one movie, containing each field, per line.separated by commas. 5.) Read the text file and load the movies into...
Database Management ID Price 1 Author Richie Karen 10 2 4 Year 1955 1958 1959 Title с JAVA Script Java C++ prolog Perl 20 10 5 6 Schwartz Ross Maria 1959 1972 20 10 7 Wall 1987 20 The above relation has information about different books. The table information is already saved as txt format in Moodle under the name Quiz2Data.txt. Using MySql on your computer do: Create a database Create a table with name book, then load Quiz2Data.txt (check...
You need to program a simple book library system. There are three java classes Book.java // book object class Library.java //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...
please write this program in C++(Linux). Also write all the explanation of codes, the comment for your code. Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...