so I have this assignment , I have to Create a data file with 2000 lines. Each line in the file consists of a faculty member’s first name, last name, rank, and salary. The faculty member’s first name and last name for the ith line are FirstNamei and LastNamei. The rank is randomly generated as Assisstant, Associate, and Full. The salary is randomly generated as a number with two digits after the decimal point.The salary for an Assistant professor should be in the range from 40000 to 70000 The salary for an Associate professor from 50000 to 90000,The salary for Full professor from 60000 to 100000.This file must saved as Salary.txt FirstName1 LastName1 assistant 58654.90
================================
FirstName1000 LastName1000 associate 95271.36
and I have to use printf in this assignment
THIS IS MY CODE:
package data;
String rank = "";
double salary;
try {
File file = new File("Salary.txt");
try (PrintWriter myWriter = new PrintWriter(file)) {
String[] ranks = {"Assistant", "Associate", "Full"};
String foundRank = ranks[(int)(Math.random() * ranks.length)];
for(int i =1; i<=2000; i++){
myWriter.println(" FirstName"+ i +" LastName"+ i);
if (rank.equals("assistant")){
double assis = 40000 + (double)(Math.random());
myWriter.printf("%s %.2f",rank,assis);}
else if(rank.equals("associate")){
double assoc = 50000 + (double)(Math.random());
myWriter.printf("%s %.2f",rank,assoc);}
else{
double full = 60000 + (double)(Math.random());
myWriter.printf("%s %.2f",rank,full);}
}}
}
catch (IOException ex) {
System.out.println("An error occurred.");
}}}
Aucun commentaire:
Enregistrer un commentaire