Results 1 to 5 of 5
  1. #1
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Post Class employee,company,test: Read data from text file.

    So I've been working on an assignment for a while where I create a class "company" that has data members employees that references an array of Employee objects and a data member payroll that stores the total payroll for the company. The thing I'm having trouble with is the test class. I don't know how to reference the Company class and print out the contents within a text file. Can anyone please help me with this. Here is the code I have so far.

    Java Code:
    /* Project 1
     * 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;
    
    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
    {
    	FileReader fr = new FileReader("C:\\employees.txt");
    	BufferedReader br = new BufferedReader(fr);
    	Employee emp;
    	String id;
    	for(int i = 0;(id = br.readLine()) != null;i++)
    	{
    		double salary = Double.parseDouble(br.readLine());
    	
    			emp = new Employee(id, salary);
    			employees[i] = emp;
    
    	
    	}
    	fr.close();
    }//end readPayrollData	
    	
    
    public void computePayroll()
    {
    	payroll = 0;
    	for(int i = 0; i< employees.length;i ++)
    	{
    		payroll = (employees[i].getPay());
    	}
    }//end Compute payroll
    	
    	public String toString()
    {
    	String newString = "Employee Information: \n";
    	for(int countEmployee = 0;countEmployee < employees.length; countEmployee++)
    	{
    		newString += employees[countEmployee] + "\n";
    	}
    	return newString + payroll;
    }//End String toString
    }//End Class Company
    
    public class Employee 
    {
    private String empID;
    private double pay;
    
    
    
    public Employee(String id, double pa)
    {
    	empID = id;
    	pay = pa;
    }
    
    double getPay()
    {
    	return pay;
    }
    }//End Class Company
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io*;
    
    class Test  
    { 
    	
    	public static void main (String[] args)
    	{
    		try
    		{
    			//Create comp company object
    			Company comp = new Company(5);
    			
    			//Compute the payroll data
    			comp.computePayroll();
    			
    			//Read the total payroll
    			comp.readPayrollData();
    			
    			System.out.println(comp.toString());//This is where I run into trouble, I feel like
    												//I should put this in a loop.Not sure how to call
    												//an object from Class Company...
    		
    		}
    		catch (IOException e){
    			System.out.println(e.getMessage());
    		
    				
    		}
    		
    	}
    }
    Last edited by jeskoston; 03-01-2011 at 01:57 AM. Reason: Poor Readablilty

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Before even reading all of your code, why are you using an array? Some other data structure might be better suited, like an ArrayList, what if you want to fire or hire a new employee?

    Also, you catch the io reading exceptions in main, you might want to try/catch them in the method that reads from the text file. What errors are you running into? More detail on what your errors/problems are would help a bunch. You also refer to a method called computePayroll in main which I don't see in the Company class. Maybe I missed it though, it's a bit hard to read code w/o code tags.
    Last edited by sunde887; 03-01-2011 at 12:09 AM.

  3. #3
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    I agree with you about using an ArrayList. However this is a hw assignment and it explicitly asks to use the Array data structure, ArrayList is beyond the scope of this particular assignment.

    I'm not sure if I'm using the toString method correctly and I'm not sure how to print out the employees.txt file.

    I'm getting a "Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Test.main(Test.java:9) " message.

    Thank you so much for you feedback.

  4. #4
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    I might be thinking of the wrong thing, but to print something to a file, can't you use the PrintWriter class's print Method?

    Example:
    Java Code:
    String name = "Bob";
    String id = "123456";
    PrintWriter outputFile = new PrintWriter("employeeID.txt");
    outputFile.print(name + " ");
    outputFile.println(id);
    outputFile.close();
    I don't know if that's what you mean, or if that will work for what you're trying to do, but it's one way to write a txt file.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your to string method looks good, however, it is slightly better to use a stringBuilder if you know how, append the information to the string builder, and then use StringBuilder.toString

    Java Code:
    StringBuilder sb = new StringBuilder("");
    for(int i = 0; i < 10; i++){
      sb.append(("" + i));
      sb.append("\n");
    }
    String s = sb.toString();
    But using a regular string is however fine, and probably better for this assignment.(I am assuming you have not covered StringBuilder just yet)

    You are trying to move too fast, slow down a bit, get one piece working, test it, then move on.

    For now create 3 employee objects in main, then create a company object and fill it with the employee objects. Then test your methods, setting, getting, and toString. This way you will know if your toString method is correct.

    The next step will be to write something to a text file. writing to a file is very similar to reading from a file. So you will simply write some info to a file in main, then read it from the file using the method. Any time you use i/o streams you should probably wrap it in a try/catch block as well.

    A final thing I suggest is to add a main method to the Employee class, in this main method, create a few employees, and apply the methods you have. Do this and test just the mployee class. Then move onto a main method in company and test all the company methods, then finally move onto testing class. If you move slow and test often you will have a much easier process.

    When testing the company methods you will, for example, realize that the compute payroll method is incorrect. Look it over and try and figure out whats wrong.

    One last thing, in the testing main method, you call computePayroll before readPayRoll, what is the value of the items in employees after creating the company object?
    Last edited by sunde887; 03-01-2011 at 03:28 AM.

Similar Threads

  1. How do I write or read data text?
    By off99555 in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2010, 11:37 AM
  2. Replies: 0
    Last Post: 11-21-2010, 06:10 PM
  3. Replies: 4
    Last Post: 04-10-2009, 06:35 AM
  4. Replies: 40
    Last Post: 07-04-2008, 09:41 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •