我对Java很陌生,所以经过几天试图弄清楚如何将用户输入与文本文件中的列进行比较后,我迫切需要帮助。我想确保一名员工在没有打孔之前没有打过一拳而没有打出来。此外,我希望能够确保用户无法打出系统,除非他们先前已经打了一拳。我知道我必须在文本文件中拆分行才能使它们单独访问,但我不知道如何将它们与用户输入进行比较。任何帮助是极大的赞赏!我的代码如下:
import java.text.*;
import java.util.*;
import java.io.*;
import java.nio.file.*;
public class TimeClockApp
{
// declare class variables
private static EmployeeDAO employeeDAO = null;
private static TimeClockDAO timeClockDAO = null;
private static Scanner sc = null;
// format date and time / //HH converts hour in 24 hours format (0-23), day calculation
private static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static void main(String[] args) throws Exception
{
// set the class variables
employeeDAO = DAOFactory.getEmployeeDAO();
timeClockDAO = DAOFactory.getTimeClockDAO();
sc = new Scanner(System.in);
int employeeID = 0;
List<Employee> employees = employeeDAO.readEmployees();
List<TimeClock> timePunches = timeClockDAO.readTimePunches();
if(timePunches == null)
{
timePunches = new ArrayList<TimeClock>();
}
// display a welcome message
System.out.println("Welcome to the Punch-In/Punch-Out Screen\n");
// print option menu
System.out.print("Please choose an option below:" + "\n"
+ "I. Punch In" + "\n"
+ "O. Punch Out" + "\n");
String choice = "";
// get input from user
Scanner sc = new Scanner(System.in);
while(choice != null)
{
// get input from user "i" or "o"
while (!choice.equalsIgnoreCase("i") && !choice.equalsIgnoreCase("o"))
{
// it will not continue if user does not enter a valid choice
choice = Validator.getScreenChoice(sc, "Choice: ");
if(!choice.equalsIgnoreCase("i") && !choice.equalsIgnoreCase("o"))
{
System.out.println("Invalid choice. Please try again.");
}
}
System.out.println(); // print a blank line
if(!choice.isEmpty())
{
// create employee object
Employee employee = null;
System.out.println("PUNCH CLOCK");
System.out.println("-----------");
// read employee ID and compare to employee.txt
while(employee == null)
{
employeeID = Validator.getEmployeeID(sc,
"Enter Employee ID: ");
for(Employee e : employees)
{
if(e.getEmployeeID() == employeeID)
{
employee = e;
break;
}
}
}
// if employee ID is valid, have they punched in already?
// if not, try again.
// read timeclock.txt
timeClockDAO.readTimePunches();
if(employeeID == employee.getEmployeeID() && choice.equalsIgnoreCase("o")) // <-- This is where I'm having trouble
{
if(timePunches.contains("i"))
{
if(timePunches.contains(employeeID))
{
System.out.println("Employee " + employeeID + " has already punched in. Please try again.");
}
}
// has employee punched in? If yes, continue. <-- Beginning here, NetBeans ignores this whole deal
for(TimeClock t : timePunches)
{
if(t.getPunchInOrOut() && choice.equalsIgnoreCase("i"))
{
timePunches = t;
break;
}
}
// if employee has not punched in, try again
if(timePunches.contains("i"))
{
if(timePunches.contains(employeeID))
{
System.out.println("Employee " + employeeID + " has not punched in yet. Please try again.");
}
}
} // <-- NetBeans stops ignoring and continues from here
TimeClock newTimePunch = new TimeClock(employeeID, new Date(), choice);
// if employee ID is valid,
// addition of date and time to arraylist/text file
timePunches.add(newTimePunch);
//write to the file
timeClockDAO.writeTimePunch(newTimePunch);
// conditional statement for in/out + formatting
System.out.println("Punch-" + (choice.equalsIgnoreCase("i") ? "In" : "Out") + " Successful!" + "\n"
+ "Date & Time: " + dateFormat.format(new Date()) + "\n"
+ "Employee Name: " + employee.getFirstName() + " " + employee.getLastName() + "\n"
+ "Employee ID: " + employee.getEmployeeID() + "\n");
System.out.println(); // print a blank line
break;
}
else
{
System.out.println("Invalid entry. Please try again.");
}
}
// press enter to continue to the main screen
System.out.printf("Press enter to return to the main screen. ");
sc.nextLine();
System.out.println("Okay, returning to Main Screen. Goodbye!");
System.out.println(); // print a blank line
MainScreenApp.main(args);
}
}
我是否可以使用代码的timeClockDAO.readTimePunches()
部分来读取和比较文本文件中的列与用户的输入,因为列已经拆分了? timeClockDAO.readTimePunches()
列表中的List<TimeClock> timePunches()
方法如下:
@Override
public List<TimeClock> readTimePunches()
{
if(timePunches != null)
return timePunches;
timePunches = new ArrayList<TimeClock>();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
if(Files.exists(Paths.get(timeClockPath))) // prevent the FileNotFoundException
{
try(BufferedReader in = new BufferedReader(
new FileReader(
new File(timeClockPath))))
{
// read all employees in the file into the array list
String line = in.readLine();
while(line != null)
{
// split text file into columns
String[] columns = line.split(EmployeeTextFile.FIELD_SEP);
/*if(columns.length != 3)
{
System.err.println("Could not read text file for Time Punches.");
return null;
}*/
// employee ID column
int employeeID = Integer.parseInt(columns[0]);
// time stamp column
Date timeStamp;
try
{
timeStamp = dateFormat.parse(columns[1]);
}
catch (ParseException e)
{
System.err.println("Could not parse time stamp: " + columns[1]);
timeStamp = null;
}
// in or out column
String punchInOrOut = columns[2];
timePunches.add(new TimeClock(employeeID, timeStamp, punchInOrOut));
line = in.readLine();
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
}
return timePunches;
}
提前谢谢!
答案 0 :(得分:0)
if(timePunches.contains("i"))
行和if(timePunches.contains(employeeID))
行无效,因为timePunches
是TimeClock
的数组列表,除非您使用比较器或实现Comparable接口,否则无法检查它是否包含String
值或Employee
值。
正如我在您的代码中所假设的那样,您可以将timePunches添加到文件的末尾,这样您只需要与ArrayList的最后一项进行比较,以查看当前员工的状态是否被打孔或打孔
而不是if(timePunches.contains("i"))
和if(timePunches.contains(employeeID))
,您应该首先使用当前的Employee打孔填充ArrayList。然后,做类似的事情:
if((timePunches.get(timePunches.size()).getPunchedInOrOut.equals("i") && choice.equals("o")) || (timePunches.get(timePunches.size()).getPunchedInOrOut.equals("o") && choice.equals("i"))
...