【发布时间】:2015-12-06 04:57:56
【问题描述】:
我是你们都知道和喜爱的 Java 编码的新手,所以我有一些非常基本和业余的问题。
首先,我创建的程序的目的是从文件中读取员工列表的文件,并将每个员工对象(具有 7 个属性)放入我的“emps”数组的一部分。
我的问题是我不知道如何搜索数组并一次只显示一个特定属性。
例如,如果有人想要某个部门的所有员工的工资列表(两个属性),如何使用来自用户输入的搜索并输出工资?我是否必须为每个属性创建数组并通过这些进行搜索?
这是我的文件的一部分,员工属性按以下顺序列出:部门名称、部门中的员工人数、姓氏、名字、公司年限、薪水、状态(在职或退休) 和工作部分。
Occupational
14
Daley Richie 3 5678.54 a Atomic Waste
Blago Donald 14 2367.00 a Public Relations
Smith John 15 32400.40 a Public Relations
Bush Jorge 6 11004.45 a Atomic Waste
Gore Alvin 37 119003.45 r Public Relations
Ryan Ryan 4 111000.00 a Human Resources
Hanrath Jon 6 63452.22 a Public Relations
Jones Amanda 13 9222.34 a Staff Concerns
Nader Raul 23 21290.67 r Human Resources
Alexander Sheila 12 25111.89 a Atomic Waste
Thompson Bess 34 133292.89 r Public Relations
Schwarzenegger Cindy 27 133292.90 r Human Resources
Allen Ethan 30 30343.44 a Staff Concerns
Stewart Martha 17 172348.00 r Public Relations
这是我的主要方法目前的样子:
import java.io.*;
import java.util.*;
public class useHamayelSajaEmployee
{
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in); //scanner for reading from keyboard
File file = new File("empList.txt");
Scanner scan2 = new Scanner(file); //scanner for reading from file
String divisionTitle = scan2.nextLine();
int numOfEmps;
final int MAX_EMPLOYEES = 1000;
HamayelSajaEmployee []emps = new HamayelSajaEmployee[MAX_EMPLOYEES];
for (int k = 0; k < 1000; k++) //for loop to set all the objects in array to constructor values
{
emps [k] = new HamayelSajaEmployee();
}
int lastCount = 0;
while(!divisionTitle.equals("END_OF_FILE"))
{
System.out.println(divisionTitle + '\n');
numOfEmps = scan2.nextInt();
for (int i = lastCount; i < numOfEmps + lastCount; i++) //instead of starting from the 0 point and writing over the objects in array, starting from point we stopped with lastCount
{
HamayelSajaEmployee emp1 = new HamayelSajaEmployee();
emp1.SetDivisionTitle(divisionTitle);
String lastName = scan2.next();
System.out.print(lastName + "\t" );
emp1.SetLastName(lastName);
String firstName = scan2.next();
System.out.print(firstName + "\t");
emp1.SetFirstName(firstName);
int yearsInCompany = scan2.nextInt();
System.out.print(yearsInCompany + "\t" );
emp1.SetYearsInCompany(yearsInCompany);
double salary = scan2.nextDouble();
System.out.print(salary + "\t" + "\t" );
emp1.SetSalary(salary);
String status = scan2.next();
char status1 = status.charAt(0);
System.out.print(status1 + "\t" );
emp1.SetStatus(status1);
String section = scan2.nextLine();
System.out.println(section);
emp1.SetSection(section);
//System.out.println(emp1.toString());
emps [i]= emp1;
}
System.out.println('\n'); //prints extra line between divisions for clarity
divisionTitle = scan2.next();
lastCount = numOfEmps + lastCount;
}
for (int i = 0; i < 1000; i++)
{
if(!emps[i].GetDivisionTitle().equals("noDivsionTitle"))//so that no empty slots in array print
{System.out.println(emps[i]);}
}
System.out.print("Enter 'M' to go to the Menu or 'Q' to Quit");
String newPlace = scan.next();
char newPlace1 = newPlace.charAt(0);
if(newPlace1 == 'M' || newPlace1 == 'm')
{menu();}
if (newPlace1 == 'Q' || newPlace1 == 'q')
{finalStats();}
else
{
while (!(newPlace1 == 'M' || newPlace1 == 'm' || newPlace1 == 'Q' || newPlace1 == 'q'))
{
System.out.println("ERROR");
System.out.println("Please try again.");
System.out.print("Enter 'M' to go to the Menu or 'Q' to Quit");
newPlace = scan.next();
newPlace1 = newPlace.charAt(0);
if(newPlace1 == 'M' || newPlace1 == 'm')
{menu();}
if (newPlace1 == 'Q' || newPlace1 == 'q')
{finalStats();}
}
}
}
我需要编写一些方法来搜索和列出不同的属性:
public static void menu()
{
int countListAll = 0;
int countEmployeeReport = 0;
int countDivisionReport = 0;
int countSalaryReport = 0;
int countRetirementReport = 0;
int countMain = 0;
System.out.println("You have accessed Menu()");
System.out.println("Enter 'L' for list of the employee data available. \nEnter 'E' to dislpay information on a particular employee. \nEnter 'D' to display division information. \nEnter 'S' to display salary information. \nEnter 'R' to display retirement information. \nEnter 'Q' to quit Menu and return to Main.");
Scanner scan = new Scanner(System.in);
String first = scan.next();
char first1 = first.charAt(0);
if (first1 == 'L'|| first1 =='l'||first1 =='E'||first1 =='e'||first1 =='D'||first1 =='d'||first1 =='S'||first1 =='s'||first1 =='R'||first1 =='r'||first1 =='Q'||first1 =='q')
{
switch (first1)
{
case 'L':
case 'l':
listAll();
countListAll++;
break;
case 'E':
case 'e':
employeeReport();
countEmployeeReport++;
break;
case 'D':
case 'd':
divisionReport();
countDivisionReport++;
break;
case 'S':
case 's':
salaryReport();
countSalaryReport++;
break;
case 'R':
case 'r':
retirementReport();
countRetirementReport++;
break;
case 'Q':
case 'q':
countMain++;
break;
}
}
else
{menu();}
}
public static boolean listAll()
{
/*list all the employees names, years in company, salaries, statuses, and sections as read in from file*/
System.out.println("You have accessed listAll()");
return true;
}
public static boolean employeeReport()
{
/*prompts user to search last name and search through data read in from file and print division, last name, first name, and section from any line that matches (HOW DO YOU NOT PRINT EVERYTHING?)*/
System.out.println("You have accessed employeeReport()");
return true;
}
public static boolean divisionReport()
{
/*prompts user to search for a division or say "all" and display only ACTIVE employees ('a' for status) in that division by last name, first name, years in company and salary. Also displays the total salaries and the average salary in the division. (HOW WOULD YOU TAKE ONLY ONE ATTRIBUTE OUT OF EVERY SINGLE OBJECT?)*/
System.out.println("You have accessed divisionReport()");
return true;
}
public static boolean salaryReport()
{
/*prompts user to search for last name or "all" and displays each employee that matches last name with their salary*/
System.out.println("You have accessed salaryReport()");
return true;
}
public static boolean retirementReport()
{
/* prompts user to search for a retirement status ('a' or 'r') or "all". For each matching status, the employee last name, first name and status displayed. If 'r' then pension for each employee (depending on years in company and salary with specific formula) displayed*/
System.out.println("You have accessed retirementReport()");
return true;
}
public static boolean finalStats()
{
/*Displays number of times each menu letter was entered by the user. the output should display the number of times each letter was input (hence the counters in the menu() method)*/
System.out.println("You have accessed finalStats()");
return true;
}
}
我知道这是一个超长的问题,但是代码就在那里,以防万一你想看它,但我的问题不需要你全部看完,只要理解我的问题。
非常感谢!
【问题讨论】: