Inheritance and File I/O Help

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2619 Views - Last Post: 09 August 2010 - 05:30 PM Rate Topic: -----

#1 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Inheritance and File I/O Help

Posted 08 August 2010 - 02:10 PM

I have the Employee class and the ProductionWorker class done but am having problems with the rest. any help is appreciated.

Assignment Description:
A Java program should be created to read an employee text file (Information.txt) and
decide from the employee number and shift which department and shift the employee is
assigned. A list of the employees and their information should be listed to the monitor as
well as written to a text file named Department.txt.

Create a class named Employee including a constructor and the necessary accessor and
mutator methods. The class should keep the following information in fields:

o Employee Name
o Employee Number in the format NNN-X, where each N is a digit and X is a
letter which can be one of the following:
H - represents Human Resources
A - represents Accounting
P - represents Production
S - represents Shipping
o Hire Date

Next, write a class named ProductionWorker that inherits from the Employee class. Include
a constructor and the necessary accessor and mutator methods. The ProductionWorker class
should keep the following information in fields:

o Shift Number which is an integer and can be one of the following:
1 - represents Morning Shift
2 - represents Swing Shift
3 - represents Night Shift
o Hourly Pay Rate

Input: The input file will be named Information.txt. One record (line) will contain the
employee name, employee number, hire date, shift number and pay rate each separated by
spaces. A sample text file is below.

Jane Rivers 902-A 05/16/2001 1 16.25
Bob Cox 823-S 06/21/1990 2 17.50
Ann Ramsey 715-A 02/12/1998 1 16.25
Joseph Chandler 723-P 12/22/2000 3 14.35
Arnold Kennedy 133-S 08/10/1999 2 18.20
Larry Huber 198-P 02/12/2000 3 17.25
Annette Wilson 501-H 04/04/1995 1 20.25
Robert Ferguson 674-H 04/10/2002 2 16.50
Ava Gaines 434-H 01/05/2000 3 15.65

Output: Output should consist of a listing of the employee name, employee number,
employee's department, hire date, shift name and pay rate This information should be
displayed on the monitor as well as written to a text file named Department.txt.


I have the Employee class and the ProductionWorker class done but am having problems with the rest. any help is appreciated.
here is what I have so far.

Here is the code for the Employee Class:


//Employee Class
public class Employee
{
	private String EmployeeName, EmployeeNumber, HireDate;
	private String type;
	 
   public void setEmployeeName(String e)
	{
		EmployeeName = e;
	}
	public void setEmployeeNumber(String n) 
	{
		EmployeeNumber = n; 
	}
	public void setHireDate (String h)
	{
		HireDate = h; 
	}
	public String getEmployeeName() 
	{
		return EmployeeName; 
	}
	public String getEmployeeNumber() 
	{
		return EmployeeNumber;
	}
	public Employee(String type)
	{ 
		this.type = type; 
	}
	public String toString()
	{ 
		if(type.equals("H"))
		return "Human Resources";
		if(type.equals("A"))
		return "Accounting";
		if(type.equals("P"))
		return "Production";
		if(type.equals("S"))
		return "Shipping";
		return type;
	}

	public String getHireDate()
	{
		return HireDate;
	}
}






Here is my ProductionWorker class

//ProductionWorker class that inherts from Employee class
public class ProductionWorker extends Employee 
{
	private int ShiftNumber; 
	private double HourlyPayRate; 
	
	public void setShiftNumber(int s) 
	{
		ShiftNumber = s;
	}
	public void setHourlyPayRate(double h) 
	{
		HourlyPayRate = h;
	}
	public int getShiftNumber() 
	{
		return ShiftNumber; 

	}
	public double getHourlyPayRate() 
	{
		return HourlyPayRate;
	}
}



import java.io.*; 
import java.util.StringTokenizer;

public class Employees
	{
	public static void main (String[] args) //throws IOException
   {
		String line, inFile = "Information.txt", outFile = "Department.txt";
		StringTokenizer tokens;
		File file = new File(filename);
		try
		{	
			//build input stream
			FileReader fr = new FileReader (inFile);
			BufferedReader fileIn = new BufferedReader (fr);
		
		
			//build output stream
      	FileWriter fw = new FileWriter (fileOut);
      	BufferedWriter bw = new BufferedWriter (fw);
      	PrintWriter outFile = new PrintWriter (bw);
		
			line = inFile.readLine();
			while (line != null)
			{
				tokens = new StringTokenizer(line);
				employeeName = null;
				employeeNumber = null;
				hireDate = null;
				shiftNumber = null;
				hourlyPayRate = null;
			
				if(tokens.hasMoreTokens())
					employeeName = tokens.nextToken();//tokenize part employeeName
					employeeNumber = tokens.nextToken();//tokenize part employeeNumber
					hireDate = tokens.nextToken();//tokenize part hireDate
					shiftNumber = tokens.nextToken();//tokenize part shiftNumber
					hourlyPayRate = tokens.nextToken();//tokenize part hourlyPayRate
					
				//output to file
				outFile.print(employeeName + " ");
				outFile.print(employeeNumber + " ");
				outFile.print(hireDate + " ");
				outFile.print(employeeName + " ");
				outFile.print(shiftNumber + " ");
				outFile.print(hourlyPayRate);
				
				//output to screen
				System.out.println(employeeName + " " + employeeNumber + " " + hireDate + " " + employeeName +
											" " + shiftNumber + " " + hourlyPayRate);
			}
			catch(FileNotFoundException exception)
			{
				System.out.println("Exception: The file " + fileIn + " was not found.");
			}
         finally
         {
         	System.out.println(line + " \n");
         }


         // read next line
         line = inFile.readLine();
         }


        // close files
        inFile.close();
        outFile.close();
      }
		 catch (IOException exception)
      {
        System.out.println(exception);
      }

      finally
      {
       System.out.println("Done");
      }
   }
}


This post has been edited by defiant8483: 08 August 2010 - 04:18 PM
Reason for edit:: Title renamed to be more descriptive.


Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance and File I/O Help

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 02:15 PM

Please post your code in code tags, like this. :code:

Also, what problems are you having with the code? Are you getting any errors?
Was This Post Helpful? 1
  • +
  • -

#3 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 02:29 PM

sorry I fixed it...I am not sure how to write the rest of it

sorry I fixed it...I am not sure how to write the rest of it
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 02:34 PM

Well, what exactly are you having trouble with?

You might want to utilize a BufferedReader or something of the like as well as a scanner to read in your input and assign the data accordingly. Don't know what else you might have trouble with unless you specficially state them.
Was This Post Helpful? 0
  • +
  • -

#5 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 02:43 PM

Where are the constructors of your classes? Make constructors for both the classes. Also, you can check and see if the string parameter taken in by the constructor is either H,A,P or S, and print out its type(e.g. A for Accounting) in a toString() method. Here's how to write a constructor.

private String type;
public Employee(String type){

     //Check and see if "type" is either H,A,P or S
     //If not, throw an exception or something
     this.type = type;
     
}




And a toString() method...

public String toString(){
     if(type.equals("H")
          return "Human Resources"
     // and the statements for A, P and S respectively
}



Remember, constructors are compulsory, or else you won't be able to instantiate Employee class.
Was This Post Helpful? 1
  • +
  • -

#6 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 04:16 PM

i have updated it but still looking for some more help
Was This Post Helpful? 0
  • +
  • -

#7 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 04:20 PM

Be specific about what you need help with. If we do not know what you need help with, how can we help you? No, we will not go through the entire assignment telling you how to do everything nor will we debug everyline of code.
Was This Post Helpful? 0
  • +
  • -

#8 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 08:43 PM

I am getting errors on the last code. And I am not looking for you to tell me how to do everything, I have a good majority of it done, but I need help finishing it. Is that not what this forum is for is for people to get help. Likie Cuzzie, he read through what I had and saw something I missed and helped me out.
Was This Post Helpful? 0
  • +
  • -

#9 the g-man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 08:52 PM

I think we are taking the same class, lol.

I am just going to give some tips:
You are going to have an issue with the tokenizer and employee name.
The constructor for employee is not going to do anything.
ProductionWorker doesn't really inherit anything from Employee right now.
You should probably try to turn the lines of the file into individual ProductionWorker objects, or else you won't get full credit.

I could probably help you out. I am nearly done with mine, but if you read my thread I am running into my own difficulties.


**EMAIL REMOVED**
Was This Post Helpful? 0
  • +
  • -

#10 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 09:05 PM

@OP, bcranger was referring to your extremely unhelpful previous post above his. You HAVE to post your updated code along with errors encountered or we have no idea what you've changed or modified
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Inheritance and File I/O Help

Posted 08 August 2010 - 09:09 PM

Please don't post your email in a public post like that. Use the PM system if you want private communication. Questions should be kept in the forums.
Was This Post Helpful? 0
  • +
  • -

#12 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Inheritance and File I/O Help

Posted 09 August 2010 - 02:13 AM

View PostCuzzie, on 08 August 2010 - 08:43 PM, said:

Remember, constructors are compulsory, or else you won't be able to instantiate Employee class.


You sure about that?

public class Employee {

    public String toString(){

        return "Hello Default Constructor";
    }
    public static void main (String [] args){

        Employee n = new Employee();
        System.out.println(n);

    }
 
  }




Remember if you do not define any constrctor in the class, a default no args constructor is created by the compiler.
Was This Post Helpful? 2
  • +
  • -

#13 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 09 August 2010 - 09:10 AM

I am getting some errors on my last code. The employees code when I try to run it is saying it is missing a class or inner class. Also I am getting some errors with the try, catch and finalize.
Was This Post Helpful? 0
  • +
  • -

#14 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Inheritance and File I/O Help

Posted 09 August 2010 - 09:46 AM

View Postm-e-g-a-z, on 09 August 2010 - 08:13 AM, said:

View PostCuzzie, on 08 August 2010 - 08:43 PM, said:

Remember, constructors are compulsory, or else you won't be able to instantiate Employee class.


You sure about that?

public class Employee {

    public String toString(){

        return "Hello Default Constructor";
    }
    public static void main (String [] args){

        Employee n = new Employee();
        System.out.println(n);

    }
 
  }




Remember if you do not define any constrctor in the class, a default no args constructor is created by the compiler.


Sorry, I forgot about the default no-arg constructors. My bad. :P

View Postdefiant8483, on 09 August 2010 - 03:10 PM, said:

I am getting some errors on my last code. The employees code when I try to run it is saying it is missing a class or inner class. Also I am getting some errors with the try, catch and finalize.


Wait, what's your last code? You might wanna post your updated code.
Was This Post Helpful? 0
  • +
  • -

#15 defiant8483  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 10

Re: Inheritance and File I/O Help

Posted 09 August 2010 - 09:52 AM

View PostCuzzie, on 09 August 2010 - 08:46 AM, said:

View Postm-e-g-a-z, on 09 August 2010 - 08:13 AM, said:

View PostCuzzie, on 08 August 2010 - 08:43 PM, said:

Remember, constructors are compulsory, or else you won't be able to instantiate Employee class.


You sure about that?

public class Employee {

    public String toString(){

        return "Hello Default Constructor";
    }
    public static void main (String [] args){

        Employee n = new Employee();
        System.out.println(n);

    }
 
  }




Remember if you do not define any constrctor in the class, a default no args constructor is created by the compiler.


Sorry, I forgot about the default no-arg constructors. My bad. :P

View Postdefiant8483, on 09 August 2010 - 03:10 PM, said:

I am getting some errors on my last code. The employees code when I try to run it is saying it is missing a class or inner class. Also I am getting some errors with the try, catch and finalize.


Wait, what's your last code? You might wanna post your updated code.

Here is updated code
import java.io.*; 
import java.util.StringTokenizer;

public class Employees
	{
	public static void main (String[] args) //throws IOException
   {
		String line, inFile = "Information.txt", outFile = "Department.txt";
		StringTokenizer tokens;
		File file = new File(filename);
		
		try
		{	
			//build input stream
			FileReader fr = new FileReader (inFile);
			BufferedReader fileIn = new BufferedReader (fr);
		
		
			//build output stream
      	FileWriter fw = new FileWriter (fileOut);
      	BufferedWriter bw = new BufferedWriter (fw);
      	PrintWriter outFile = new PrintWriter (bw);
		
			line = inFile.readLine();
			while (line != null)
			{
				tokens = new StringTokenizer(line);
				employeeName = null;
				employeeNumber = null;
				hireDate = null;
				shiftNumber = null;
				hourlyPayRate = null;
			
				if(tokens.hasMoreTokens())
					employeeName = tokens.nextToken();//tokenize part employeeName
					employeeNumber = tokens.nextToken();//tokenize part employeeNumber
					hireDate = tokens.nextToken();//tokenize part hireDate
					shiftNumber = tokens.nextToken();//tokenize part shiftNumber
					hourlyPayRate = tokens.nextToken();//tokenize part hourlyPayRate
					
				//output to file
				outFile.print(employeeName + " ");
				outFile.print(employeeNumber + " ");
				outFile.print(hireDate + " ");
				outFile.print(employeeName + " ");
				outFile.print(shiftNumber + " ");
				outFile.print(hourlyPayRate);
				
				//output to screen
				System.out.println(employeeName + " " + employeeNumber + " " + hireDate + " " + employeeName +
											" " + shiftNumber + " " + hourlyPayRate);
			}
			catch(FileNotFoundException exception)
			{
				System.out.println("Exception: The file " + fileIn + " was not found.");
			}
         finally
         {
         	System.out.println(line + " \n");
         }


         // read next line
         line = inFile.readLine();
		}


        // close files
        inFile.close();
        outFile.close();
	}
		 catch (IOException exception)
      {
        System.out.println(exception);
      }

      finally
      {
       System.out.println("Done");
      }
	}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2