Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Could someone explain this to me?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Could someone explain this to me?

    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;
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Could someone explain this to me?

    If you need clarification about your assignment who should you ask: strangers on the internet or the person who set the assignment?

    Please refrain from whining about your teacher and that they never explain things or help anyone.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    I am not whining. I'm saying that I do not understand how to do what it is asking me to do and if any one could point me the right direction.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Could someone explain this to me?

    I didn't say you were whining but many people do. I just wanted to stop that before it starts.

    If you want better help then you need to ask a specific question. "I don't know what to do" is very open ended and difficult for anyone to answer.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    I am just having trouble figuring out how to go about reading from each individual sub folder, do you get what i'm saying?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Could someone explain this to me?

    Nope.

    What subfolders?
    What are you trying to read?
    Where are you trying to read?
    What are you using to read?
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    Okay, the program reads in a whole bunch of text file (or receipts) that contain the receipt information. The program then makes a new Receipt object for each one of them and then through invoking the Receipt class methods, it obtains all of the information stored within the text file and assigns them variables. The program makes sub folders and sorts them by the persons name, then year of purchase and then day. So the directory is not, for example, C:\Users\John\Desktop\Receipts\Smith, John\2012\11. Once all of the files are organized in this manner, I need to go back into the new folders and read in all of the individual files and sort them into an array or something of that sort in order by the transaction id.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,107
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Could someone explain this to me?

    Start at the top folder and use one of the File class's list methods to get its contents. The File class also has methods to distinguish a file from a directory. When a directory is found, use it as the new root and continue getting its contents until at the end of the directories.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone explain this to me?

    Oh okay, that makes since. Thanks.

Similar Threads

  1. Could someone please better explain this to me?
    By blobman23 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:51 PM
  2. [SOLVED] Can someone explain to me?
    By unleashed-my-freedom in forum Java Theory & Questions
    Replies: 5
    Last Post: July 3rd, 2012, 04:10 AM
  3. i need an explain please !
    By keep smiling in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 10:22 AM
  4. Replies: 1
    Last Post: December 13th, 2010, 04:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM