+1 678 648 4277 

Books Quality and prices data using Java Homework Solution


Books Series, Price, Condition and Buyer Database

Problem Description

You are going to write a program that reads book price data from a file, performs some computations, and displays a table of cost data.

Input: The file contains, on the first line, the title of the book. On the second line, it contains the base price for a new copy of the book.

The rest of the file consists of lines in this format:

• Name of seller

• Shipping cost

• Quality of book (1 = new, 2 = like new, 3 = very good, 4 = good, 5 = acceptable)

You may not assume that the file exists. You may not assume that it contains any text. If it exists and contains text, it might contain the title and base price of a new copy of the book (the header information) only, or it might contain the header information and any number of complete records. You may assume that the header information, if present, is complete. You may assume that each record, if present, is complete. You may assume the correct data type for all data in the file.

Output: You will display the title of the book and a table that includes a header and an output line for each record from the file (seller, shipping, quality) which is the seller, the quality of the book, the cost of the book, and the cost of the book plus shipping.

Interface: Your program should prompt for the name of the input file using the following prompt:

• What is the name of the file?

Your program should use the following error message if the file is not found.

• The file was not found.

Your program should use the following message if the file is empty. (Substitute the name of the file provided by the user.)

• empty.txt is empty.

If the file contains the header record only, the program should produce the following output (header information read from the file is underlined).

There are no copies of Turing's Vision: The Birth of Computer Science for sale.

For a file that contains a header plus seller records, the output should be formatted as follows. Cost and Cost + Shipping will be shown in two decimal places. The seller column should have a width of 30 characters. All other columns have a width of 20 characters.

Books Quality

Design specifications: Use console and text file input and console output.

Do not, under any circumstances, use System.exit. Design your methods to have one return statement. The main method should have no return statements. Use break only in switch statements. Do not submit code that uses these constructs (or other code that breaks out of a logical structure) inappropriately. Submit whatever you can write that does not include these constructs.

For this program, you must write (and use) the following two methods:

• calculateCost

calculateCost takes two parameters – the base cost of the book (double), and the quality (int)

calculateCost returns the adjusted cost of the book (double) according to this formula:

A “new” book’s cost is the base cost. A “like new” book’s cost is base cost – 10%. A “very good” book’s cost is base cost – 20%. A “good” book’s cost is base cost – 25%. An acceptable book’s cost is base cost – 30%. So, for example, if a book’s base cost is $25.99 and it’s in good condition, its cost is $25.99 – 25% of 25.99, or $19.49.

• getQualityString

getQualityString takes one parameter – the quality of the book (int) getQualityString returns the appropriate string (String) based on the quality (e.g. for a quality of 1, getQualityString should return “New”)

Testing

Test Case 1

Purpose

Test a case with a file with header information and complete records with books of all quality types.

Input (file)

Turing's Vision: The Birth of Computer Science 20.99

BestBookDeals 3.99

2

UsedTextbooksRUs 0

5

JohnsScienceBooks 3.99

3

ValueBookorama 0

1

A1BookSellers 3.99

4

AlreadyUnderlined

3.99

5

QualityBooks

3.99

1

BooksBooksBooks

3.99

2

Expected output

Books 1

Test Case 2

Purpose

An invalid file name.

Input (console)

doesnotexist.txt

Expected output

The file was not found.

Test Case 3

Purpose

A file with header information but no records.

Input (file)

Turing's Vision: The Birth of Computer Science 20.99

Expected output

There are no copies of Turing's Vision: The Birth of Computer Science for sale.

Test Case 4

Purpose

An empty file.

Input (console)

empty.txt

Input (file)

Expected output

empty.txt is empty.

Solution:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BookPrice { /** * returns the adjusted cost of the book (double) according to this formula: A * "new" book’s cost is the base cost. A "like new" book’s cost is base cost – * 10%. A "very good" book’s cost is base cost – 20%. A "good" book’s cost is * base cost – 25%. An acceptable book’s cost is base cost – 30%. So, for * example, if a book’s base cost is $25.99 and it’s in good condition, its cost * is $25.99 – 25% of 25.99, or $19.49. * * @param base * @param quantity * @return */ public static double calculateCost(double base, int quantity) { double finalCost = 0; switch (quantity) { case 1: // new - book’s cost is the base cost finalCost = base; break; case 2: // like new - book’s cost is base cost – 10% finalCost = base - (0.1 * base); break; case 3: // very good - book's cost is base cost - 20% finalCost = base - (0.2 * base); break; case 4: // good - book's cost is base cost - 25% finalCost = base - (0.25 * base); break; case 5: // acceptable - book's cost is base cost - 30% finalCost = base - (0.3 * base); break; default: break; } return finalCost; } public static String getQualityString(int quality) { String qualityStr = ""; switch (quality) { case 1: // new qualityStr = "New"; break; case 2: // like new qualityStr = "Like new"; break; case 3: // very good qualityStr = "Very good"; break; case 4: // good qualityStr = "Good"; break; case 5: // acceptable qualityStr = "Acceptable"; break; default: break; } return qualityStr; } public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(System.in); // Your program should prompt for the name of the input file using the following // prompt System.out.print("What is the name of the file? "); String filename = sc.nextLine(); // Open file File inputFile = new File(filename); // Check file exist if (inputFile.exists()) { // Open the file Scanner inputFileReader = new Scanner(inputFile); // Check if empty if (inputFileReader.hasNext() == false) { // Your program should use the following message if the file is empty System.out.println(inputFile + " is empty."); return; } else { double bookCost = 0.0; String title = inputFileReader.nextLine(); if (inputFileReader.hasNextDouble()) { bookCost = inputFileReader.nextDouble(); if (inputFileReader.hasNext()) { inputFileReader.nextLine(); System.out.println("Title: " + title); System.out.printf("%-30s%-20s%-20s%-20s\n", "Seller", "Quality", "Cost", "Cost + Shipping"); while (inputFileReader.hasNext()) { // Reading the seller name and print out String sellerName = inputFileReader.nextLine(); //System.out.println("DEBUG: sellerName: " + sellerName); if (inputFileReader.hasNextDouble()) { double shippingCost = inputFileReader.nextDouble(); int quantity = inputFileReader.nextInt(); //System.out.println("DEBUG: shippingCost: " + shippingCost + " - quantity: " + quantity); double finalCost = calculateCost(bookCost, quantity); System.out.printf("%-30s%-20s%-20.2f%-20.2f\n", sellerName, getQualityString(quantity), finalCost, (shippingCost + finalCost)); } inputFileReader.nextLine(); } } else { System.out.println("There are no copies of " + title + " for sale."); } } else { System.out.println("Missing shipping cost."); return; } } } else { // Your program should use the following error message if the file is not found. System.out.println("The file was not found."); } // j o h n n y t u o t - g m a i l - c o m } }