• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inputfile and getting information out

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to read from a file that is set up like called (filename.txt)
Linda Lou 45 10.50 3
Sam Martin 40 15.00 1
Mike darvin 56 12.75 2
jeff corbin 42 13.65 4
Joseph smith 37 15.25 5
peter thomas 52 11.35 0

but I have to do it from a pane because I cannot figure out how do it easier without having to type it in from a window.

But right now my Output file is:

null null
Basic Pay $0.0
Overtime Pay $0.0
Gross Pay $0.0
State Tax Amt $0.0
Fed Tax Amt $0.0
Total Taxes $0.0
Net Pay $0.0
Parking Ticket $0.0 Total Pay $0.0

which should output all the files





 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike statham wrote:I need to read from a file that is set up like called (filename.txt)
. . .
but I have to do it from a pane because I cannot figure out how do it easier without having to type it in from a window. . . .

What does that mean? It seems contradictory to me.

If you are reading from a file, you ought not to do it in the main method. Your main method contains 49 lines. That is 48 too many. You ought to have a method which reads from the file. Then you can create an object of a class which encapsulates the pay, tax allowance, parking tickets, etc. You are inappropriately using the static keyword for things which ought not to be static in your example.
 
mike statham
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

mike statham wrote:I need to read from a file that is set up like called (filename.txt)
. . .
but I have to do it from a pane because I cannot figure out how do it easier without having to type it in from a window. . . .

What does that mean? It seems contradictory to me.

If you are reading from a file, you ought not to do it in the main method. Your main method contains 49 lines. That is 48 too many. You ought to have a method which reads from the file. Then you can create an object of a class which encapsulates the pay, tax allowance, parking tickets, etc. You are inappropriately using the static keyword for things which ought not to be static in your example.




You see the program I was given was:
Re-write programming assignment # 3 with the following exceptions:
1. Your program must read input from a file.
Input file definitions:
First Name, Last Name, Hours Worked, Pay Rate, Parking Tickets
Example:
Linda Lou 45 10.50 3
Sam Martin 40 15.00 1
2. Modularize your program by sending parameters too and receiving data from at least the following 4 methods that must be invoked:
a. Calculate your gross pay.
b. Calculate overtime if it exists.
c. Calculate your net pay.
d. Pay Parking tickets if needed.
3. Write results to an output file in the following pay stub format for each employee :

Sam Martin
Basic Pay $600.00
Overtime Pay 0
Gross Pay $600.00
State Tax Amt $54.00
Fed. Tax Amt. $120.00
Total Taxes $174.00
Net Pay $426.00
1 Parking ticket $20
Total Pay $406.00
 
Campbell Ritchie
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had written it according to the principles and customs of object-oriented programming, that would now be such an easy assignment. You would have an Employee class, which you might have to add getGrossPay() or getOvertimePay() methods to. In real life, you would have a List of weeks worked in the Employee class, but here you are only working out one week’s wages. So your class might well be called WeekWorked, or similar. As I said, with getGrossPay() and getOvertimePay() methods. You can get that stub from a toString() method.

I think you have gone about this exercise quite the wrong way, I am afraid. I think you need a class, like this:-Now a method to test it . . . which I have written so you must call it like this

java PayDemo Campbell Ritchie 42.5 1000000.00 99

. . . at the command line, otherwise you will suffer Exceptions. The correct working of that method depends on the arguments going in the correct order; you might need to change the order of the constructor parameters, and also on WeekWorked having a suitably-overridden toString() method.
I am not sure that gross pay is actually hours worked × pay rate. It is (hours worked up to 40) × pay rate + (hours worked over 40) × 1.5 × pay rate. I think you need a getBasicPay() method instead of getGrossPay; gross pay = basic pay + overtime pay.
Now you can work out how to write that WeekWorked object to a text file. And also how to get the data from a text file. When you have several records in a text file, you will need a loop to read them in turn. Loop, read from the file, create your WeekWorked object, write it to the output file, then back to the start of the loop.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic