Write a Java program for an inventory report using objects created from a text file

user3518113 picture user3518113 · Sep 19, 2014 · Viewed 8k times · Source

I had a project for school that I could just not get to compile correctly.

The instructions can be found at this link.

I believe I have the class created correctly in Product.java. My code is below:

import java.util.*;

public class Product {

// Private member variables go here - you will need to add them yourself.
private String name;
private String code;
private int quantity;
private double price;
private String type;
private ArrayList<Integer> userRatings;


/*
 * Product constructor
 */
public Product() {
    name = "";
    code = "";
    quantity = 0;
    price = 0;
    type = "";
    userRatings = new ArrayList<Integer>();
}

/*
 * setName
 *  @param name - new name for the product
 */
public void setName(String name) {
    this.name = name;
}

/*
 * getName
 *  @return the name of the product
 */
public String getName() {
    return name;
}

/*
 * setType
 *  @param type - the type of the product
 */
public void setType(String type) {
    this.type = type;
}

/*
 * getType
 * @return - the product type
 */
public String getType() {
    return type;
}

/*
 * setPrice
 * @param price - the price of the product
 */
public void setPrice(double price) {
    this.price = price;
}

/*
 * getPrice
 * @return the price of the product
 */
public double getPrice() {
    return price;
}

/*
 * setQuantity
 * @param quantity - the number of this product in inventory
 */
public void setQuantity(int quantity) {
    this.quantity = quantity;
}

/*
 * getQuantity
 * @return the number of this product in inventory
 */
public int getQuantity() {
    return quantity;
}

/*
 * setInventoryCode
 * @param code - the new inventory code for the product
 */
public void setInventoryCode(String code) {
    this.code = code;
}

/*
 * getInventoryCode
 * @return the inventory code of the product
 */
public String getInventoryCode() {
    return code;
}

/*
 * addUserRating
 * NOTE: Each individual rating is stored with the product, so you need to maintain a list
 * of user ratings.  This method should append a new rating to the end of that list
 * @param rating - the new rating to add to this product
 */
public void addUserRating(int rating) {
    userRatings.add(rating);
}

/*
 * getUserRating
 *  NOTE:  See note on addUserRating above.  This method should be written to allow you
 *  to access an individual value from the list of user ratings 
 * @param index - the index of the rating we want to see
 * @return the rating indexed by the value index
 */
public int getUserRating(int index) {
    return userRatings.get(index);
}

/*
 * getUserRatingCount
 *  NOTE: See note on addUserRating above.  This method should be written to return
 *  the total number of ratings this product has associated with it
 * @return the number of ratings associated with this product
 */
public int getUserRatingCount() {
    return userRatings.size();
}

/*
 * getAvgUserRating
 *  NOTE: see note on addUserRating above.  This method should be written to compute
 *  the average user rating on demand from a stored list of ratings.
 * @return the average rating for this product as a whole integer value (use integer math)
 */
public int getAvgUserRating() {
    int sum = 0;
    if(userRatings.size() > 0){
        for (int i = 0; i < userRatings.size(); i++){
            sum += userRatings.get(i);
        }
        return sum / userRatings.size();
    }

    else return 0;
}
}

But the problem lies within the test code. I have tried multiple ways and keep getting the same InputMismatchException error. I will need an ArrayList of Product objects for part 2 so I tried to incorporate that into the test code. Below is ProductTest.java:

import java.util.*;
import java.io.*;

public class ProductTest {

/*
 * A simple main loop to load product objects from a file
 * and then display them to the console
 */
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter an inventory filename: ");
    String fname = keyboard.nextLine();
    ArrayList<Product> products = loadProducts (fname);
    displayProducts(products);

}

/*
 * loadProducts
 * Given a filename, opens the file and reads Products from
 * the file into an ArrayList of Product objects. Returns the
 * Arraylist.
 * 
 * @param fname - String containing the input file name
 * @return - An ArrayList of Product objects
 */

public static ArrayList<Product> loadProducts(String fname) {
    ArrayList<Product> products = new ArrayList<Product>();
    try {
        Scanner inFile = new Scanner(new File(fname));
        while (inFile.hasNext()) {
            Product pr = new Product();
            pr.setName(inFile.next());
            pr.setInventoryCode(inFile.next());
            pr.setQuantity(inFile.nextInt());
            pr.setPrice(inFile.nextDouble());
            pr.setType(inFile.next());
            while(inFile.nextInt() != -1){
                pr.addUserRating(inFile.nextInt());
            }
            products.add(pr);
        }
        inFile.close();
    }
    catch(IOException e) {
        System.out.println("ERROR: "+e);
    }
    return products;
}

/*
 * displayProducts
 *  Given an ArrayList of Product objects, outputs them
 *  to the console in the specified format.
 *  
 *  The format for this method is:
 *  NAME, INVENTORY_CODE, QUANTITY, PRICE, TYPE, RATING
 *  
 *  @param products - ArrayList of Product objects
 */

public static void displayProducts(ArrayList<Product> products) {
    for(int i = 0; i<products.size(); i++) {
        Product tmpProduct = products.get(i);
        System.out.println(tmpProduct.getName());
        System.out.println(tmpProduct.getInventoryCode());
        System.out.println(tmpProduct.getQuantity());
        System.out.println(tmpProduct.getPrice());
        System.out.println(tmpProduct.getType());
        System.out.println(tmpProduct.getAvgUserRating());
        System.out.println();
    }

}

}

This is the error message that results from running the current ProductTest:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at osu.cse2123.ProductTest.loadProducts(ProductTest.java:46)
    at osu.cse2123.ProductTest.main(ProductTest.java:23)

The text file I'm using contains the following:

The Shawshank Redemption
C0000001
100
19.95
DVD
4
5
3
1
-1
The Dark Knight
C0000003
50
19.95
DVD
5
2
3
-1
Casablanca
C0000007
137
9.95
DVD
5
4
5
3
-1
The Girl With The Dragon Tattoo
C0000015
150
14.95
Book
4
4
2
-1
Vertigo
C0000023
55
9.95
DVD
5
5
3
5
2
4
-1
A Game of Thrones
C0000019
100
8.95
Book
-1

Any help on the issues would be much appreciated.

Answer

shinjw picture shinjw · Sep 19, 2014

You are reading one word at a time. Currently your file is being read by the Scanner like this:

inFile.next() = The
inFile.next() = Shawshank

Tip: inFile.nextLine() = The Shawshank Redeption