I am working on a receipt sorting program, and I am pretty much finished with it, however, there is one more thing that it has to be able to do and i'm not entirely sure how to go about implementing it.
Here is the assignment (I highlighted the part that I do not understand.):
Overview
Anchovisoft StoreFront® is a popular web store application with one major flaw: it creates
receipts for its transactions as a loose collection of files, with no real organization. You’ve been
hired to write a program to clean up the collection of files, organizing them by customer name
and date.
You’ll also need to include the ability to read in all the transactions again from the organized file
hierarchy and sort them into an ordered collection (Array, ArrayList, Vector, etc.) to be
processed. They should be sorted by transaction id.
The Files
The files are named as follows:
[transaction_id].receipt
Inside the file is the content of the receipt. Here is a sample file, 111115.receipt:
ID: 111115
Date: 20100104
Customer Name: Aaron Johnson
Product ID: 1228
Product Name: Rotafork(tm) Deluxe
Amount: 9
Price per unit: 10.99
Subtotal: 98.91
Shipping: 8.00
Tax: 0.08
Total: 115.46
At present, all the files are in the same directory. Your job is to organize them into folders, first by
customer name then by year and month. The above file should become:
./Johnson, Aaron/2010/01/111115.receipt
Classes
How you implement this program is largely up to you. However, the following classes and
methods must be available:
Class: ReceiptOrganizer
This is the main class from which the program will be launched. This class should include the
main() method that (a) warns the user that organizing files is somewhat dangerous, (b) prompts
the user for the directory path where the files are currently stored, and (c) prompts the user for
the root path in which the files should be organized. (So, the files might be stored in
C:\Users\JSU\Desktop and need to be placed under C:\Users\JSU\Documents\Transaction
Receipts.)
Class: Receipt
This class should represent a single receipt. It should be able to store all data related to a
receipt, including transaction id, date, customer id, etc. It should provide accessor and mutator
methods for manipulating data. It should implement Comparable<Receipt> in order to be sorted
by transaction id.
One of the constructors of Receipt should take a file name as a String and load the receipt
information from the file.
Here is my current code:
The ReceiptOrganizer class:
package receiptorganizer; import java.io.*; import java.util.*; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * * @author Jared */ public class ReceiptOrganizer { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { //Variables Scanner s = new Scanner(System.in); String warningMessage = "WARNING!: Organizing files can be somewhat dangerous." + "\n" + "Do you wish to continue? (y/n) : "; String userConfirmation; String currentDirectoryPath; String rootPath; String goAgain = "y"; while (goAgain.equals("y") || goAgain.equals("Y")){ //Warning message System.out.println(warningMessage); userConfirmation = s.nextLine(); //If the user does want to go ahead and run the program. switch (userConfirmation) { case "y": case "Y": System.out.println("What is the directory path where the files are currently stored? : "); currentDirectoryPath = s.nextLine(); System.out.println("What is the root path in which the files should be organized? : "); rootPath = s.nextLine(); //This will be where we do everything at. //This currently just moves the files from one folder to another. Path p = Paths.get(currentDirectoryPath); DirectoryStream<Path> dir = Files.newDirectoryStream(p); Path newDir = Paths.get(rootPath); Files.createDirectories(newDir); for (Path file : dir) { String name = file.getFileName().toString(); if (name.endsWith(".receipt")) { //String representation of the files directory. String fileDir = currentDirectoryPath.toString() + "\\" + name; //Making new Receipt object and getting the info from the methods in order to sort into the directories. Receipt newReceipt = new Receipt(fileDir); String firstName = newReceipt.getCustomerFirstName(); String lastName = newReceipt.getCustomerLastName(); String year = Integer.toString(newReceipt.getYear()); String month = Integer.toString(newReceipt.getMonth()); String wholeName = lastName + ", " + firstName; //Creates the new path for the file. Path tempDir = Paths.get(rootPath, wholeName, year, month); Files.createDirectories(tempDir); //Writes the file to the new directories. System.out.println("Adding " + name + "..."); Path newPath = Paths.get(tempDir.toString(), name); if (Files.notExists(newPath)) Files.copy(file, newPath); } } break; case "n": case "N": System.out.println("Try again if you change your mind. Have a great day."); break; default: System.out.println("Sorry, your choice did not match our available options."); break; } System.out.println("Do you wish to organize more files? (y/n) : "); goAgain = s.nextLine(); } } }
Here is the Receipt class:
package receiptorganizer; import java.io.*; import java.util.*; /** * * @author Jared */ public class Receipt implements Comparable<Receipt>{ //Variables: public int id, year, month, day, productID, amount; public float pricePerUnit, subtotal, shipping, tax, total; public String customerFirstName, customerLastName, productName; //Constructor for Receipt: public Receipt(String fileName) throws FileNotFoundException, IOException{ BufferedReader inputFile = new BufferedReader(new FileReader(fileName)); String[] line; line = inputFile.readLine().split(" "); id = Integer.parseInt(line[1]); line = inputFile.readLine().split(" "); String newDate = line[1]; String[]newDateArray = newDate.split("-"); year = Integer.parseInt(newDateArray[0]); month = Integer.parseInt(newDateArray[1]); day = Integer.parseInt(newDateArray[2]); line = inputFile.readLine().split(" "); customerFirstName = line[2]; customerLastName = line[3]; line = inputFile.readLine().split(" "); productID = Integer.parseInt(line[2]); line = inputFile.readLine().split(" "); productName = line[2] + " " + line[3]; line = inputFile.readLine().split(" "); amount = Integer.parseInt(line[1]); line = inputFile.readLine().split(" "); pricePerUnit = Float.parseFloat(line[3]); line = inputFile.readLine().split(" "); subtotal = Float.parseFloat(line[1]); line = inputFile.readLine().split(" "); shipping = Float.parseFloat(line[1]); line = inputFile.readLine().split(" "); tax = Float.parseFloat(line[1]); line = inputFile.readLine().split(" "); total = Float.parseFloat(line[1]); } //Getters: public int getID (){ return id; } public int getYear (){ return year; } public int getMonth (){ return month; } public int getDay (){ return day; } public String getCustomerFirstName (){ return customerFirstName; } public String getCustomerLastName (){ return customerLastName; } public int getProductID (){ return productID; } public String getProductName (){ return productName; } public int getAmount (){ return amount; } public float getPricePerUnit (){ return pricePerUnit; } public float getSubtotal (){ return subtotal; } public float getShipping (){ return shipping; } public float getTax (){ return tax; } public float getTotal (){ return total; } //Setters: public void setID (int id){ this.id = id; } public void setYear (int year){ this.year = year; } public void setMonth (int month){ this.month = month; } public void setDay (int day){ this.day = day; } public void setCustomerFirstName (String customerFirstName){ this.customerFirstName = customerFirstName; } public void setCustomerLastName (String customerLastName){ this.customerLastName = customerLastName; } public void setProductID (int productID){ this.productID = productID; } public void setProductName (String productName){ this.productName = productName; } public void setAmount (int amount){ this.amount = amount; } public void setPricePerUnit (int pricePerUnit){ this.pricePerUnit = pricePerUnit; } public void setSubtotal (int subtotal){ this.subtotal = subtotal; } public void setShipping (int shipping){ this.shipping = shipping; } public void setTax (int tax){ this.tax = tax; } public void setTotal (int total){ this.total = total; } /* * Implements a compareTo method to compare if two transaction ids are the same. * Returns 0 if they are the same. * Returns -1 if otherID is less than id and therefore should be sorted after. * Returns 1 if otherID is greater than id and therefore should be sorted before. */ @Override public int compareTo(Receipt otherReceipt){ int otherID = otherReceipt.getID(); if (id == otherID){ return 0; } else if (otherID < id){ return -1; } else if (otherID > id){ return 1; } return 0; } }