CSV Parsing

// CashRegister.java
/*
CashRegister Class
Write a CashRegister class that can be used with the RetailItem class. The CashRegister class should simulate the sale of a retail item.  In addition, the class should have the following methods:
addItem which accepts an argument of a RetailItem and adds that item to the current sale.
The getSubtotal method should return the subtotal of the sale, which is the quantity multiplied by the price. This method must get the price from the RetailItem object that was passed as an argument to the constructor.
The getTax method should return the amount of sales tax on the purchase. The sales tax rate is 6 percent of a retail sale.
The getTotal method should return the total of the sale, which is the subtotal plus the sales tax.
*/

package lab6;

import java.util.ArrayList;

/**
 *
 * @author ncc
 */
public class CashRegister {
    private ArrayList<RetailItem> sale;
    
    public CashRegister() {
        sale = new ArrayList();
    }
    
    public void addItem(RetailItem item) {
        sale.add(item);
    }
    
    public double getSubtotal() {
        double subtotal = 0.0;
        for (RetailItem item: sale) {
            subtotal += item.getPrice();
        }
        return subtotal;
    }
    
    public double getSalesTax() {
        return this.getSubtotal() * 0.06;
    }
    
    public double getTotal() {
        return this.getSubtotal() + this.getSalesTax();
    }
    
/* Sales Receipt
Output a sales receipt showing the output of all of the items entered into the cash register.
Sales Receipt:
Items: 3
Subtotal: $15.00
Sales-Tax: $5.00
Total: $20.00
*/ 
    @Override
    public String toString() {
        StringBuilder returnSB = new StringBuilder();
        returnSB
            .append("Sales Receipt:\n")
            .append("Items: ")
            .append(this.sale.size())
            .append("\n");

        for (RetailItem item : sale) { 
            returnSB.append(item.toString()).append("\n");
        }
        
        returnSB.append("Subtotal: ")
                .append(this.getSubtotal())
                .append("\n")
                .append("Sales-Tax: ")
                .append(this.getSalesTax())
                .append("\n")
                .append("Total: ")
                .append(this.getTotal())
                .append("\n");
        return returnSB.toString();
    }
    
    public String toCSV() {
        StringBuilder sb = new StringBuilder();
        sb.append("description,price,units on hand\n");
        for (RetailItem item : sale) {
            sb.append(item.toCSV());
        }
        return sb.toString();
    }
}

/* 
RetailItem.java
Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields:
description. The description field references a String object that holds a brief description of the item.
unitsOnHand. The unitsOnHand field is an int variable that holds the number of units currently in inventory.
price. The price field is a double that holds the item’s retail price.
Write a constructor that accepts arguments for each field, appropriate mutator methods that store values in these fields, and accessor methods that return the values in these fields. 
*/

package lab6;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ncc
 */
public class RetailItem {
    private String description;
    private double unitsOnHand;
    private double price;
    
    public RetailItem(String description, double unitsOnHand, double price) {
        this.description = description;
        this.unitsOnHand = unitsOnHand;
        this.price = price;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @return the unitsOnHand
     */
    public double getUnitsOnHand() throws NotEnoughUnitsException {
        if (unitsOnHand < 0) {
            throw new NotEnoughUnitsException();
        }
        return unitsOnHand;
    }

    /**
     * @param unitsOnHand the unitsOnHand to set
     */
    public void setUnitsOnHand(double unitsOnHand) {
        this.unitsOnHand = unitsOnHand;
    }

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

    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
    
    @Override
    public String toString() {
        String returnString = "Description: " + this.getDescription() + "\n";
        returnString += "Price: " + this.getPrice() + "\n";
        try {
            returnString += "Quantity On Hand: " + this.getUnitsOnHand() + "\n";
        } catch (NotEnoughUnitsException ex) {
            Logger.getLogger(RetailItem.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnString;
    }
    
    public String toCSV() {
        return this.description + "," + this.price + "," + this.unitsOnHand + "\n";
    }
}

/* 
Main.java
*/
package lab6;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author ncc
 */
public class Lab6 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here
        RetailItem shoes = new RetailItem("Converse Sneakers", 12.0, 20.00);
        System.out.println(shoes);
        CashRegister register = new CashRegister();        
//// UNcomment this for CSV Reading
//        File importRegister = new File("receipt.csv");
//        Scanner fileIn = new Scanner(importRegister);
//        while (fileIn.hasNextLine()) {
//            String line = fileIn.nextLine();
//            String[] data = line.split(",");
//            for (String s : data) {
////                System.out.println(s);                
//            }
//            RetailItem item = new RetailItem(data[0], Double.parseDouble(data[2]), Double.parseDouble(data[1]));
//            register.addItem(item);
//        }
//        
//        System.out.println(register);
///// END CSV READ SECTION
 
        register.addItem(shoes);
        register.addItem(shoes);
        System.out.println(register.toCSV());
        try {
            PrintWriter csvWriter = new PrintWriter("salesReceipt.csv");
            csvWriter.println(register.toCSV());
            csvWriter.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    
}