added input validation

This commit is contained in:
Simon
2021-02-03 13:54:28 +01:00
parent c7c27c4347
commit 6facc040a6
+36 -25
View File
@@ -12,12 +12,13 @@ import java.util.Scanner;
public class CorrectionHelper {
final static String FILE_PATH = "./answers.xls";
final static boolean CORRECT_ALL = false;
final static boolean CORRECT_ALL = true;
final static String TASK_TO_CORRECT = "1.2";
final static int LAST_NAME = 0;
final static int FIRST_NAME = 1;
final static int MATRICULATION_NUMBER = 2;
public static void main(String[] args) {
File file = new File(FILE_PATH);
@@ -53,16 +54,21 @@ public class CorrectionHelper {
for (int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if (row != null) {
boolean inRow = true;
for (int c = 0; c < cols; c++) {
cell = row.getCell((short) c);
if (cell != null) {
try {
if (cell.getStringCellValue().contains("MANUAL_CORRECTION")) {
if (inRow)
System.out.println(row.getCell(FIRST_NAME) + " " + row.getCell(LAST_NAME) + " " + row.getCell(MATRICULATION_NUMBER));
inRow = false;
int column = row.getCell(c - 1).getColumnIndex();
@@ -70,33 +76,38 @@ public class CorrectionHelper {
if (task.contains(TASK_TO_CORRECT) || CORRECT_ALL) {
Scanner scanner = new Scanner(System.in);
System.out.print(task + ": ");
String input = scanner.nextLine();
boolean inputOk = false;
if (input.equals("SAVE")) {
wb.write(file);
System.exit(0);
while (!inputOk) {
Scanner scanner = new Scanner(System.in);
System.out.print(task + ": ");
String input = scanner.nextLine();
// FIXME: only skip one entry
} else if (input.equals("SKIP")) {
break;
if (input.equals("SAVE")) {
wb.write(file);
System.exit(0);
} else if (input.equals("SKIP")) {
break;
} else {
int rating = Integer.parseInt(input);
if (0 <= rating && rating <= 100) {
inputOk = true;
row.getCell(column).setCellValue(Integer.parseInt(input));
wb.write(file);
if (Integer.parseInt(input) != 100) {
System.out.print("Comment: ");
String comment = scanner.nextLine();
row.getCell(column + 3).setCellValue(comment);
wb.write(file);
}
row.getCell(c).setCellValue("CORRECTED_MANUAL");
wb.write(file);
}
}
}
// TODO: validate input
row.getCell(column).setCellValue(Integer.parseInt(input));
wb.write(file);
if (Integer.parseInt(input) != 100) {
System.out.print("Comment: ");
String comment = scanner.nextLine();
row.getCell(column + 3).setCellValue(comment);
wb.write(file);
}
row.getCell(c).setCellValue("CORRECTED_MANUAL");
wb.write(file);
}
}
} catch (Exception ignored) {