Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Saving Abstrace, ToString To File

$
0
0
I have a little Problem,

I am trying to Save a ToString to a Text file with a public abstract holidays();

The Part i'm having problems with is is asking me to change the StaffList to an Int,

my Text file is just showing

Staff@35a8767Total Holidays: 22.0
-----------------------------------

                           System.out.println(StaffList[count]);
	    		amount = StaffList[count].holidays();
	    		amount += basic;	
	    		out.write(StaffList.lengthtoString());
	    		out.write ("Total Holidays: " + amount);
	    		out.write("\n");
	    		out.write ("-----------------------------------");
	    	   	
	    		//Close the output stream
	    		out.close();




	public String toString()
	{
		String result ="\n";
		result += "Name: " + Name + "\n";
		result += "Address: " + Address + "\n"; 
		result += "Phone: " + phone + "\n"; 
		result += "Start Date: " + StartDate + "\n";
		result += "End Date: " + EndDate + "\n";
		result += "Position: " + position + "\n";
		result += "Salary: " + Salary + "\n";
		result += "Sex: " + Sex + "\n";
		result += "Personal Public Service Number: " + PPSNumber + "\n";
		return result;
	}


import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;


public class Staff {

	private staffmember[] StaffList;
	public Staff()
	{
	
	
	StaffList = new staffmember[3];
	  
	
	StaffList[0] = new Engineer("David Foley", "Gurranabraher", "021-11111", "Head Engineer", 60000 , "Male", "19/03/2003", "19/03/20014", "33404GH", "Network Engineer" , 7);

	StaffList[1] = new Cleaner("Niamh Wallace", "Togher", "021-22222", "Supervisor", 30000, "Female", "19/03/2001", "Present", "1222W", "Office Block B", 2);

	StaffList[2] = new LineWorker("Tom Kelly" , "Douglas", "021-33333", "Employee", 22000, "Male", "16/11/2011", "19/01/2013", "1210NW", 12, 1);




	}
	
	
	public void holidays()
	{
	double amount;
	int basic = 21; 
	
	for (int count = 0; count < StaffList.length; count++) 
	{
		
		 try
		 {
	      		
	    		// Create file 
	    		FileWriter out = new FileWriter("EmployeeRecords.txt");
	    		
	    		/*
	    		 * Takes the EmployeeMember Temp 
	    		 * and Writes the Object of the ArrayList 
	    		 * To a Text file using the Following Code. 
	    		 */
	    		
	    		System.out.println(StaffList[count]);
	    		amount = StaffList[count].holidays();
	    		amount += basic;	
	    		out.write(StaffList.lengthtoString());
	    		out.write ("Total Holidays: " + amount);
	    		out.write("\n");
	    		out.write ("-----------------------------------");
	    	   	
	    		//Close the output stream
	    		out.close();
	    		
	    }//End Try Write 
		 
		 
		 		//Catch exception if any
		 		catch (Exception e) 
		 		{
		 		//Out Put Error Message if Any Exception is Found. 
	    		System.err.println("Error: Can Not Create File!  " + e.getMessage()); 
	    		} //end Catch 
	      	
		 
		 	//Reading a file
	      	File file = new File("EmployeeRecords.txt");
	      	int ch;
	      	StringBuffer strContent = new StringBuffer("");
	      	FileInputStream fin = null;
	      	
	      	try
	      	{
	      	    fin = new FileInputStream(file);
	      	    while ((ch = fin.read()) != -1)
	      	           strContent.append((char) ch);
	      	    fin.close();
	      	} // Try Read 
	      	
	      	catch (Exception e) 
	      	{
	      	    System.out.println(e);
	      	} //End Catch 
	      	
	      	//Out Print the File Content to Console. 
	      	System.out.println(strContent.toString());
	      	


	}// End If Read / Write Method
	
} //Ends Main Method 
		
	
	 public void RemoveEmployee(String Name) 
	   { 
		   try{
			  for (int i = 0; i < StaffList.length; i++)
				  if(StaffList[i].getName() == Name)
					  StaffList[i] = null;
			  
			  for (int remove = StaffList.length; remove < StaffList.length; remove++)
			  {
				  StaffList[remove] = StaffList[remove+1];
			  } 
			  StaffList[StaffList.length-1] = null; 
		   }
		   catch(Exception e)
		   {
			   System.err.println("Error result: " + "Cant Not Remove Account "+  e.getMessage());
		   }
	   } 
	
	 
	 
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles