-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTextFile.java
More file actions
120 lines (102 loc) · 4.42 KB
/
CreateTextFile.java
File metadata and controls
120 lines (102 loc) · 4.42 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
115
116
117
118
119
120
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
import java.util.NoSuchElementException;
public class CreateTextFile {
private static Formatter output; // Declare the Formatter outside of methods to use it in multiple methods
public static void openFile() {
try {
output = new Formatter("IT.txt");
} catch (SecurityException securityException) {
System.err.println("Write denied. Terminating");
System.exit(1);
} catch (IOException fileNotFoundException) {
System.err.println("Error opening file. Terminating");
System.exit(1);
}
}
public static void addRecords() {
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n?", "Enter the problem you encountered, Details", "Enter end-of-file indicator to end input");
while (input.hasNext()) {
try {
output.format("%s|%s|%s%n", input.next(), input.next(), input.next());
} catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine(); // Discard input so user can try again
} catch (FormatterClosedException formatterClosedException) {
System.err.println("Error writing to file. Terminating");
break;
}
System.out.print("?");
}
}
public static void closeFile() {
if (output != null) {
output.close();
}
}
static class ITIssueLoggerGUI extends JFrame {
private JButton sendButton;
private JTextField problemTypeField;
private JTextField detailsField;
private JTextField severityField;
private JTextArea outputArea;
public ITIssueLoggerGUI() {
setTitle("IT Issue Logger");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLayout(new BorderLayout());
sendButton = new JButton("Send");
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(sendButton);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent 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("IT.txt", true); // append mode
fileWriter.write(problemType + "|" + details + "|" + severity + "\n");
fileWriter.close();
outputArea.append("Data written to file successfully: " + problemType + "|" + details + "|" + severity + "\n");
} catch (IOException ex) {
outputArea.append("An error occurred while writing to the file.\n");
ex.printStackTrace();
}
}
}
}
}