Link to home
Start Free TrialLog in
Avatar of Indie101
Indie101

asked on

Trouble using test driver and also with writing to a text file

FIrst of all have a slight issue with the driver program, how is it picked up when used.
Having problems writing this to a file as part of payroll problemQuestion: Total java noob, got a lot of help regarding decimal formatting for following code,kudos to the site, working on a payroll project as part of a team, have been trying to write this report to a file, any help appreciated.. Have tried to use

http://java.sun.com/docs/books/tutorial/essential/io/file.html  for info...Cant seem to see how it works

From what I was told filewriter can be used as well, which is best and if possible could a working example both ways be constructed...I want to learn but also want to see some working examples before I modify it, even if both working examples were done for a sample which i could learn from that would be great.
public static void setContents(File aFile, String aContents) 
    throws FileNotFoundException, IOException  
    { 
                //System.out.println("File contents: " +aContents); 
                logger.debug("File contents: "+ aContents); 
                 
                //if the file is null, then IllegalArgumentException is raised 
                if (aFile == null)  
                { 
                        throw new IllegalArgumentException(EMFILESHOULDNOTBENULL); 
                } 
 
                //if the file is a directory, then IllegalArgumentException is raised 
                if (!aFile.isFile())  
                { 
                        throw new IllegalArgumentException(EMSHOULDNOTBEDIR + aFile); 
                } 
 
                //if the file is a read-only, then IllegalArgumentException is raised 
                if (!aFile.canWrite())  
                { 
                        throw new IllegalArgumentException(EMFILEREADONLY + aFile); 
                } 
 
                //use buffering 
                Writer output = new BufferedWriter(new FileWriter(aFile)); 
                try  
                { 
                        //FileWriter always assumes default encoding is OK! 
                        output.write( aContents ); 
                } 
                catch (IOException e)  
                { 
                } 
                finally  
                { 
                        try  
                        { 
                                //closing the I/O connection 
                                output.close(); 
                        }  
                        catch (Exception e)  
                        { 
                        }

Open in new window

Avatar of Indie101
Indie101

ASKER

First code is the driver, below is the Report.java how do they work together, as far as I know its just to test the a portion of full program, how does that work, any other info appreciated
import java.text.*;


public class Report

{
//	Instance Variables (all private)


	private String[] _departments; //A String array containing the names of four departments
	private double[] _grossTotals; //An array of doubles to hold gross totals by dept
	private double[] _taxTotals; //An array of doubles to hold tax totals by dept


//	constructor - Create two total arrays and initialise them to zero

	public Report()
	{

		_departments = new String[4]; 
		_departments[0] = "Accounting";
		_departments[1] = "Sales";
	    _departments[2] = "HR";
	    _departments[3] = "Administration";
		
		_grossTotals = new double[]{0,0,0,0};
		_taxTotals = new double[]{0,0,0,0};
	}

	public void addToTotals(double gr, double tx, char dept)
	{
		//first recover department code
		int dcode =  (int) dept - (int) '1';

		//now use numeric department code to index into array value to modify
		this._grossTotals[dcode] = this._grossTotals[dcode] + gr;
		this._taxTotals[dcode] = this._taxTotals[dcode] + tx;
	}

	public void printTotals()
	{
		//Purpose:  To print  a report showing dept name, gross totals and tax totals for each department 
NumberFormat nf = NumberFormat.getInstance(); 
 
System.out.println("Department Bank Report"); 
for(int j = 0; j<4; j++) { 
  System.out.println("Department" +"\t" + "Total" + "\t" + "Tax Paid"); 
  System.out.println(_departments[j] + "\t" + nf.format(_grossTotals[j]) + "\t" + nf.format(_taxTotals[j])); 
}

	}

	}

Open in new window

Apologies first snippet of code is towards writing file to text, (not sure how to use it) this is the driver program
\\Report test class Bill Fitzgerald
{
}

import java.util.*;

public class MainClass 
{
	public static void main(String[] args) 
	{		
		 Report newReport = new Report();

		//apply constructor to set initial values
		 newReport.addToTotals(100, 10, '1');
		 newReport.addToTotals(100, 10, '1');
		 newReport.addToTotals(100, 10, '1');
		 newReport.addToTotals(100, 10, '1');

		 newReport.addToTotals(150, 15, '2');
		 newReport.addToTotals(150, 15, '2');
		 newReport.addToTotals(150, 15, '2');

		 newReport.printTotals();
	}
}

Open in new window

Avatar of CEHJ
Indie101, you've already accepted an answer to this question (having completely ignored my comments) so i'm wondering why you're posting it again..?
I accepted previous question in relation to the number formatting, sorry If i ignored your comment, I just hadnt come across it before, TBH I just wanted a working example of the above...If you could kindly help me again CEHJ would appreciate it a lot... You mentioned before

"The easiest way to print to file is just to redirect the output (see below)

If you're interested in formatting in a table with correct widths and alignment, use System.out.printf
1:
 java MainClass >sales.log

Could you setup for my report class, I am trying to revise as I go along, just finding it hard!! :-)
If it's working (you said it was), first confirm that my first comment does what you want
The comment that was made by another expert worked in relation to decimal formatting. As I have said I wasnt aware of how to use your comment can you show me how it works for the report class.
I posted the command you need to issue at the command line. That's all you need
java MainClass >sales.log

Thanks for explaining above, :-) I wasnt aware initially it was for cmd line.

I can't use it at command line though has to be using fileoutputstream or filewriter. Sorry about any confusion earlier..

Can you provide any help with fileoutputstream or filewriter CEHJ?? :-)
Would be great if you can...
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Answer while accurate was not what was looking for