-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItfile.java
More file actions
114 lines (101 loc) · 3.79 KB
/
Itfile.java
File metadata and controls
114 lines (101 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
public class itfile extends JFrame {
private JButton sendButton;
private JTextField problemTypeField, detailsField, severityField;
private JTextArea outputArea;
private static Formatter output;
private static final String FILE_PATH = System.getProperty("user.home") + "/IT.txt";
if(args.length>0&&args[0].equals("console"))
{
consoleLogging();
}else
{
SwingUtilities.invokeLater(() -> new itfile().setVisible(true));
}
public itfile() {
setTitle("IT Issue Logger");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLayout(new BorderLayout());
sendButton = new JButton("Log Issue");
problemTypeField = new JTextField();
detailsField = new JTextField();
severityField = new JTextField();
outputArea = new JTextArea();
outputArea.setEditable(false);
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
inputPanel.add(new JLabel("Problem Type:"));
inputPanel.add(problemTypeField);
inputPanel.add(new JLabel("Details:"));
inputPanel.add(detailsField);
inputPanel.add(new JLabel("Severity:"));
inputPanel.add(severityField);
inputPanel.add(new JLabel()); // Placeholder
inputPanel.add(sendButton);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
sendButton.addActionListener(e -> {
String problemType = problemTypeField.getText();
String details = detailsField.getText();
String severity = severityField.getText();
if (!problemType.isEmpty() && !details.isEmpty() && !severity.isEmpty()) {
processInput(problemType, details, severity);
problemTypeField.setText("");
detailsField.setText("");
severityField.setText("");
} else {
outputArea.append("Please fill in all fields.\n");
}
});
}
private void processInput(String problemType, String details, String severity) {
try (FileWriter fileWriter = new FileWriter(FILE_PATH, true)) {
fileWriter.write(String.format("%s|%s|%s%n", problemType, details, severity));
outputArea.append(
String.format("Data written to file successfully: %s|%s|%s%n", problemType, details, severity));
} catch (IOException ex) {
outputArea.append("An error occurred while writing to the file.\n");
ex.printStackTrace();
}
}
public static void consoleLogging() {
try {
openFile();
addRecords();
} finally {
closeFile();
}
System.out.println("IT Issue Log has been saved successfully!");
}
public static void openFile() {
try {
output = new Formatter(FILE_PATH);
} catch (SecurityException | IOException e) {
System.err.println("Unable to open file. Terminating.");
System.exit(1);
}
}
public static void addRecords() {
Scanner input = new Scanner(System.in);
System.out.println("Enter problem type, details, and severity on separate lines. Type 'EOF' to end:");
while (true) {
String line = input.nextLine();
if ("EOF".equals(line))
break;
output.format("%s%n", line);
}
input.close();
}
public static void closeFile() {
if (output != null) {
output.close();
}
}
}