Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am currently wondering how a certain java program would be written properly.



 [Sample Console Output/File Output][1]
A department store manages their sales in the form of files.  A sale is recorded on each line of a sales file in this format:

22349999990603193150

    The first 4 digits are the department code(2234)
    The next 6 digits are the item number (999999)
    The next 6 digits are the date(060319)
    The final digits are the price in pennies(3150)

Write a program that will parse a sales file and print a report for each sale

Your program should:

    Prompt a user for the input sales file name
    Prompt a user for the output report file name
    Create your own input file with at least 4 items
    Read each line of the sales file
        Print the following to the console and output report file 
            The department store name: MCC Department Store
            The date in format  full month name, day, 4 digit year
            The department name
            The item number
            The price, tax and total
    The department ranges are:
        1000-1999 Clothing
        2000-2999 Hardware
        3000-3999 Food
        4000-4999 Toys
        5000-5999 Sporting Goods
        6000-9999 – Misc.


  [1]: https://i.stack.imgur.com/RMAYp.jpg


What I have tried:

My current code is as follows:

    try {
             in = new FileInputStream("input.txt");
             out = new FileOutputStream("output.txt");
             
             int c;
             while ((c = in.read()) != -1) {
                out.write(c);
             }
          }finally {
             if (in != null) {
                in.close();
             }
             if (out != null) {
                out.close();
Posted
Updated 10-Nov-20 6:50am

Given that your code doesn't even come close to doing what your homework requires, there isn't a lot of point in either of us wasting our time "describing how it works" so you can understand your own code ...

Throw that lot away, read the question again carefully, and start thinking about what you need to do instead of leaping into code and hoping it will all work!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Ryan Pflum 10-Nov-20 12:31pm    
How does this answer anything? Take your time and answer the question.
OriginalGriff 10-Nov-20 12:36pm    
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you want to understand what the code is doing, there's a tool called the DEBUGGER that does that. It's there to debug YOU, not the code. It's there to help you understand what the code is doing. Get to know how to use it, and fast. It'll make your life so much easier from this point on.

I think it's pretty obvious what that code does. It's a very simple operation, but you would already know what that if it was you who wrote the code.

You copied the code from TutorialsPoint.com, here[^] and they already explained what that code does.
 
Share this answer
 
The first thing you need to do with your input is read each line and split it into the different fields: department, item, date, price. So you could start by writing the code to do that part. Just reading it one character at a time and writing that to the output serves no purpose.

So start by creating a text file with a few records and process them simply. Read each record, split the string and print the four separated values. Once you have that then you can think about what extra processing you need for each field. for example you will need a method to process the date into a displayable form. Another method to calculate the tax based on whatever rate you have been told to use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900