Question: public static Product createProduct(Scanner fileScanner) { String name = ; int sku = -1; double price = -1; double weight = -1; int destination =

public static Product createProduct(Scanner fileScanner) { String name = ""; int sku = -1; double price = -1; double weight = -1; int destination = -1; int quantity = -1; //TODO Student return new Product(); } 

public static Product createProduct(Scanner fileScanner)

This method loops through the fileScanner parameter provided, using the Scanner.hasNextLine() method and creates a Product object from the data being read. Each file provided will have multiple lines specifying the NAME, SKU, PRICE, WEIGHT, DESTINATION, and QUANTITY.

Each file will contain each of these fields, but lines will be provided in different orders, as they may be when provided by different suppliers. Such as:

 NAME: Berk's Salt Lamps SKU: 90435765 PRICE: 12.99 WEIGHT: 22.5 DESTINATION: 83128 QUANTITY: 22 

Each of these fields will exist in a given "Product.txt" file. Using if statements and the String.contains() method, determine if the newest line in the loop has one of these fields, and if it does, set the appropriate variable. For setting each variable you will need to use String.substring(). Integer.parseInt(int) and Double.parseDouble(double) will also come in handy for parsing and setting each variable.

If the fields is not NAME, SKU, PRICE, WEIGHT, DESTINATION, or QUANTITY print an error, but still create the object.

For your loop and reading a line from the Scanner object, the Scanner.hasNextLine() and Scanner.nextLine() methods will help!

After reading through the entire file and setting the appropriate variables, create and return the new Product object.

Parameters:

fileScanner - Scanner set to a File object and ready to be read from.

Returns:

Product that is created from file contents.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!