For a homework assignment, I have to create a random salary generator which is pretty
easy. We have to create a random number of employees, 3-15 only and then the number of
years of that salary with a limit of 3-15. This is easy so far. After this, we have to loop the
salary through the employees and the years and have this save to a file. That is a little
more tricky, but I have got that too. The salary has to be between 40k and 100k. After the
file is created and saved, the program has to read the data from the file and calculate the
average salary. The average salary will determine a salary grade and all of this has to be
output on the screen. I have no problems with the file creation or reading, and the
averaging and grading scheme work fine.

With this description, you could say I have the program, but the problem I am having is that
for each employee, the salary has to increase from the previous salary, not decrease. I
need to figure out how to create a test to make sure that not only is the salary within the
range, but that the previous collected salary is smaller than the current created salary (per
employee).

This means that employee 1 could have 45k, 49k, and 80k and employee 2 could have 40k,
45k, and 68k, but employee 1 can't have 89k, 45k, 67k, etc etc.


The code uses 5 seperate classes for the program:

The first main class

import java.text.DecimalFormat;

public class Assignment10 {

	public static void main(String[] args) {
		Assignment10 myAssignment10 = new Assignment10();
		myAssignment10.process();
	}

	public Assignment10(){	}

	public void process(){
		RecordHandler myRecordHandler = new RecordHandler();
		myRecordHandler.cleanup();
		prepare(myRecordHandler);
		use(myRecordHandler);
	}

	public void prepare(RecordHandler myRecordHandler){
		myRecordHandler.open();
		SalaryGenerator mySalaryGenerator = new SalaryGenerator();

		int numberOfTeamWorkers = 0;
		while (numberOfTeamWorkers < 3 || numberOfTeamWorkers > 15){
			numberOfTeamWorkers = mySalaryGenerator.getSalary(15);
		}

		int numberOfYears = 0;
		while (numberOfYears < 3 || numberOfYears > 15){
			numberOfYears = mySalaryGenerator.getSalary(15);
		}


		//THIS TESTS TO SEE HOW MANY WORKERS AND YEARS THERE ARE
		//System.out.println("# of workers: " + numberOfTeamWorkers + " with # of years: " + numberOfYears);


		for (int i = 0; i < numberOfTeamWorkers; i++){
			String line = "";
			for (int j = 0; j < numberOfYears; j++){
				line = line + mySalaryGenerator.getSalary() + " ";
			}
			if (i == (numberOfTeamWorkers -1)){
				myRecordHandler.append(line);
			}else{
				myRecordHandler.append(line+ "\n");
			}
		}

		myRecordHandler.close();
	}

	public void use(RecordHandler myRecordHandler){
		myRecordHandler.open();
		Editor myEditor = new Editor();
		Calculations myCalculations = new Calculations();
		DecimalFormat formater = new DecimalFormat("###,###,##0.00");
		int i = 1;
		String readLine = myRecordHandler.read();
		while(readLine != null){
			int counter = myEditor.getCounter(readLine);
			int[] Salaries = myEditor.parse(readLine, counter);
			int total = myCalculations.getTotalSalaries(Salaries);
			double averageSalary = myCalculations.getAverageSalary(Salaries);
			String salaryGrade = myCalculations.getSalaryGrade(averageSalary);


			System.out.println("Worker " + i + " average:\t" + "$" + formater.format(averageSalary) + "\t\t" + salaryGrade);

			readLine = myRecordHandler.read();
			i++;
		}

		myRecordHandler.close();
		System.out.println("\n\n");
	}
}

The second file is:

public class Calculations {

	public Calculations(){ }

	public double getAverageSalary(int[] Salaries){

		int total = getTotalSalaries(Salaries);
		double averageSalary = (double) total / Salaries.length;
		return averageSalary;
	}

	public int getTotalSalaries(int[] Salaries){
		int total = 0;
		for (int i = 0; i < Salaries.length; i++){
			total = total + Salaries[i];
		}
		return total;
	}

	public String getSalaryGrade(double averageSalary){

		String salaryGrade = "";

		if(averageSalary >= 90000.0){
			salaryGrade = "salary grade 12";
		}else if (averageSalary >= 80000.0){
			salaryGrade = "salary grade 11";
		}else if (averageSalary >= 70000.0){
			salaryGrade = "salary grade 10";
		}else if (averageSalary >= 60000.0){
			salaryGrade = "salary grade 9";
		}else if (averageSalary >= 50000.0){
			salaryGrade = "salary grade 8";
		}else {
			salaryGrade = "salary grade 7";
		}

		return salaryGrade;

	}
}

The third file is:

public class Editor {

	public Editor(){ }

	public int getCounter(String input){
		int count = 1;
		boolean endOfLine = false;
		while(!endOfLine){
			input = input.substring(input.indexOf(" ")).trim();
			count++;
			if (input.indexOf(" ") < 0){
				endOfLine = true;
			}
		}

		return count;
	}


	public int[] parse(String input, int count){
		int[] Salaries = new int[count];

		for (int i = 0; i < (count - 1); i++){
			int score = Integer.parseInt(input.substring(0, input.indexOf(" ")).trim());
			input = input.substring(input.indexOf(" ")).trim();
			Salaries[i] = score;
		}

		Salaries[count - 1] = Integer.parseInt(input.trim());

		return Salaries;
	}

}

The fourth file is:

import java.io.*;

public class RecordHandler {

	private RandomAccessFile myAccess;

	public RecordHandler(){ }

	public void cleanup(){
		File myFile = new File("stevenRecord.smp");
		myFile.delete();
	}

	public void open(){
		try {
			myAccess = new RandomAccessFile("stevenRecord.smp", "rwd");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void close(){
		try {
			myAccess.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void append(String inputString){
		try {
			myAccess.writeBytes(inputString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String read(){
		try {
			return myAccess.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}

The last file is:

import java.util.Random;

public class SalaryGenerator {

	private Random myRandom = new Random();

	public SalaryGenerator(){ }

	public int getSalary(){
		int Salary = 0;
		while (Salary < 40000 || Salary > 100000){
			Salary = myRandom.nextInt(200000);
			Salary = Salary * 2;
		}
		return Salary;
	}

	public int getSalary(int limit){
		int Salary = myRandom.nextInt(limit);
		while (Salary < 5){
			Salary = myRandom.nextInt(limit);
		}
		return Salary;
	}

	public int getSalary(int high, int low){
		int Salary = myRandom.nextInt(high);
		while (Salary < low){
			Salary = myRandom.nextInt(high);
		}
		return Salary;
	}

}

With this description, you could say I have the program, but the problem I am having is that
for each employee, the salary has to increase from the previous salary, not decrease. I
need to figure out how to create a test to make sure that not only is the salary within the
range, but that the previous collected salary is smaller than the current created salary (per
employee).

Come up with some pseudo code...

Perhaps your SalaryGenerator should take the current salary as a parameter and add a random amount to it, a randomRaise() perhaps.

Come up with some pseudo code...

I have already included the actual code for the program so I do not know what help pseudo
code would be. I think the problem has something to do with the random generator class
and how it is set up.

Perhaps your SalaryGenerator should take the current salary as a parameter and add a random amount to it, a randomRaise() perhaps.

Could you show pseudo code or something how to do this to verify that the salary did indeed
increase from the previous one for that particular employee.

It's not a matter of verifying a random change after it's done. Generate the random salary progression by starting with a base and adding a random raise to it. Assigning a series of random salaries to an employee makes little sense if an upward progression is desired (and I think most employees would agree that is the only acceptable progression :P )

This article has been dead for over six months. Start a new discussion instead.