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 4 of 4

Thread: Class company, class Employee and tester (Having trouble with my computepayroll() met

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default An assignment with Filereader and Bufferedreader having trouble with one ofmyclasses

    So I had to define all the method for class company and implement the main method as well. I'm reading a file from my C: drive, I am using filereader and bufferedreader to read the file and then read it line by line. I need to output what's on the file. I was also wondering if I could print out the info w/out the use of a toString().
    (this is what's in the text file in my c drive)
    123
    45000.00
    456
    60000.00
    321
    55000.0
    567
    80000.0
    111
    37000.0

     
    import java.io.*;
     
    class Test  
    { 
     
    	public static void main (String[] args)
    	{
     
    			//Create comp company object
    			Company comp = new Company(5);
     
     
    			//Read the total payroll
    			try {
    				comp.readPayrollData();
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    			//Compute the payroll data
    			comp.computePayroll();
     
     
     
    	}//End main
    }//End class Test
     
     
    public class Employee 
    {
    private String empID;
    private double pay;
     
    public Employee(){}
     
    public Employee(String id, double pa)
    {
    	empID = id;
    	pay = pa;
    }//End Employee
    double getPay()
    {
    	return pay;
    }//end getpay
     
    }//End class Employee
     
    /*Author: JCont
     * Class company provides the following constructor methods
     * for processing the data stored in the array and for 
     * computing the company payroll.
     */
    import java.io.*;
     
     
    public class Company 
    {
    //Declare data, array of employees and payroll for company.
    private Employee[] employees;
    private double payroll;
     
    public Company(int size)
    {
     employees = new Employee[size];
    }
     
    // Method setPayRoll will store argument in variable payroll.
    public void setPayRoll(double p)
    {
    	payroll = p;
    }
     
    //validate range of index #'s. 
    private boolean Range(int i)
    {
    	return i >= 0 && i < employees.length;
    }
     
    //If valid range then will store employees in a certain index. 
    public void setemployees(Employee e, int i)
    {
    	if (Range(i))
    	{
    		employees[i] = e;
    	}	
    }
    //If there is a valid range if indices then will return that employees, otherwise null.
    public Employee getemployees(int i)
    {
    	if(Range(i))
    	{
    		return employees[i];	
    	}
    	else
    	{
    		return null;
    	}
    }//End Employee getemployees
     
     
    //Read data from a file called employees.txt and store them in array "employees"
    public void readPayrollData() throws Exception
    {
    try{
    	FileReader fr = new FileReader("c:\\employees.txt");
    	BufferedReader br = new BufferedReader(fr);
    	String id;
    	//id = br.readLine();
    	//while((id  != null))
    	for(int i = 0;(id = br.readLine()) != null;i++)
    	{
     
    			String tempsalary = br.readLine();
    			double salary = Double.parseDouble(tempsalary);
     
    			Employee emp = new Employee(id, salary);
    			employees[i] = emp;
     
    	}//end for	
    	fr.close();
      }//end try block
     
    	catch (FileNotFoundException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    	} catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    	}
    }//end readPayrollData	
     
     
    public void computePayroll()
    {
     
    	for(int i = 0; i < employees.length;i ++)
    	{
     
    		payroll = (employees[i].getPay() );	
     
    	}
    }//end Compute payroll
     
    }//End class
    Last edited by jeskoston; April 12th, 2011 at 10:35 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Class company, class Employee and tester (Having trouble with my computepayroll()

    What's your question? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class company, class Employee and tester (Having trouble with my computepayroll()

    Read a file and print, using BufferedReader and System.out : File ReaderFile Input OutputJava

    so, no you wouldn't have to use toString, this example outputs to the console. Even if you are retrieving a string, provided that contains a numeric value you are able to use Integer.parseInt() or similar function... if you were having trouble figuring out how to carry out computations on values retrieved as strings?

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Class company, class Employee and tester (Having trouble with my computepayroll()

    Thanks for the responses! Well the only trouble I'm having at this point is outputting what's on the file. Although this may seem pretty trivial it's just a matter of system.out.print. I added a system.out.print to readPayrolldata() to print out id and another on computepayroll() to display the "salary" and although it prints out the values it doesn't in id|pay... order. It'll display as so
    123
    456
    321
    567
    111
    45000.00
    60000.00
    55000.0
    80000.0
    37000.0
    which makes sense since that's the way I am calling them and printing them out, I'm having an off week and I/O is killing me in all of my classes.
    Last edited by jeskoston; April 13th, 2011 at 03:32 PM.

Similar Threads

  1. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  2. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  3. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  4. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM