JAVA code Need help with creating a delete button on the following code 1 packag
ID: 3695356 • Letter: J
Question
JAVA code
Need help with creating a delete button on the following code
1 package log;
2
3 import java.io.BufferedWriter;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.io.Writer;
10 import java.nio.charset.Charset;
11 import java.nio.file.Files;
12 import java.nio.file.Paths;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.List;
16
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19
20 /**
21 *
22 * @author Chris
23 * Changes by Matt
24 */
25
26 public class EntryList {
27
28 List<Entry> logbase = new ArrayList<Entry>();
29 JPanel panel = new JPanel();
30 Date today = new Date();
31
32 /**
33 * Constructor
34 * @throws IOException
35 * @throws FileFormatException
36 */
37 public EntryList() throws IOException, FileFormatException{ //LOG BASE HAS BEEN BUILT BUT MUST BE TESTED THOROUGHLY!
38
39 String file = readFile("Logs.txt", Charset.forName("UTF-8"));
40 System.out.println(file.length());
41 if(file.length()<2){
42 System.out.println("Log is Empty");
43 }
44
45 else{
46 String[] entries = file.split("/;E/; ");
47 for(int i = 0; i < entries.length; i++){
48 System.out.println(entries[i]); //For testing purposes only
49 String[] words = entries[i].split("/;e;/");
50 if(words.length > 3)
51 throw new FileFormatException("File has become corrupt.");
52
53 long dat = Long.parseLong(words[1]);
54 logbase.add(new Entry(words[0], words[2], new Date(dat)));
55 }
56 }
57 }
58
59 /**
60 * Method to read the file
61 * @param path
62 * @param encoding
63 * @return
64 * @throws IOException
65 */
66 private String readFile(String path, Charset encoding) throws IOException{
67 byte[] encoded = Files.readAllBytes(Paths.get(path));
68 return new String(encoded, encoding);
69 }
70
71 /**
72 * Add log entry to Logs.txt
73 * @param userName
74 * @param text
75 */
76 public void createLogEntry(String userName, String text){
77 Entry entry = new Entry(userName, text, today);
78 logbase.add(entry);
79 try {
80 updateLogFile();
81 } catch (IOException e) {
82 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
83 "Error!",JOptionPane.WARNING_MESSAGE);
84 e.printStackTrace();
85 }
86 JOptionPane.showMessageDialog(null, "Successfully added entry",
87 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
88 }
89
90
91 public void editLogEntry(Entry entry){//TODO
92
93 }
94
95 //Returns entry creation date, creator, and text
96 public String viewLogEntry(Entry entry){
97 return entry.view();
98 }
99
100
101 public void deleteLogEntry(Entry entry){//TODO
102 logbase.remove(entry);
103 try {
104 updateLogFile();
105 } catch (IOException e) {
106 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
107 "Error!",JOptionPane.WARNING_MESSAGE);
108 e.printStackTrace();
109 }
110 JOptionPane.showMessageDialog(null, "Successfully deleted entry",
111 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
112 }
113
114
115 /**
116 * Clears Logs.txt file
117 */
118 public void deleteAllLogEntries(){
119 logbase.clear();
120 try {
121 updateLogFile();
122 } catch (IOException e) {
123 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
124 "Error!",JOptionPane.WARNING_MESSAGE);
125 e.printStackTrace();
126 }
127 JOptionPane.showMessageDialog(null, "Successfully deleted all entries",
128 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
129 }
130
131
132 /**
133 * Updates log file
134 * @throws IOException
135 */
136 public void updateLogFile()throws IOException{
137 /*FileWriter writer = new FileWriter("Logs.txt", false);
138 try (BufferedWriter bw = new BufferedWriter(writer)) {
139 bw.write(this.toString());
140 }*/
141 Writer out = new BufferedWriter(new OutputStreamWriter(new
142 FileOutputStream("Logs.txt"), "UTF-8"));
143 try {
144 out.write(this.toString());
145 } finally {
146 out.close();
147 }
148 }
149
150
151 /**
152 * All entries to string formated for saving to file.
153 * @return
154 */
155 @Override
156 public String toString(){
157 String output = "";
158 for(Entry e: logbase){
159 output += e.toString() + "/;E/; ";
160 }
161 return output;
162 }
163 }
Explanation / Answer
1 package log;
2
3 import java.io.BufferedWriter;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.io.Writer;
10 import java.nio.charset.Charset;
11 import java.nio.file.Files;
12 import java.nio.file.Paths;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.List;
16
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19
20 /**
21 *
22 * @author Chris
23 * Changes by Matt
24 */
25
26 public class EntryList {
27
28 List<Entry> logbase = new ArrayList<Entry>();
29 JPanel panel = new JPanel();
30 Date today = new Date();
31
32 /**
33 * Constructor
34 * @throws IOException
35 * @throws FileFormatException
36 */
37 public EntryList() throws IOException, FileFormatException{ //LOG BASE HAS BEEN BUILT BUT MUST BE TESTED THOROUGHLY!
38
39 String file = readFile("Logs.txt", Charset.forName("UTF-8"));
40 System.out.println(file.length());
41 if(file.length()<2){
42 System.out.println("Log is Empty");
43 }
44
45 else{
46 String[] entries = file.split("/;E/; ");
47 for(int i = 0; i < entries.length; i++){
48 System.out.println(entries[i]); //For testing purposes only
49 String[] words = entries[i].split("/;e;/");
50 if(words.length > 3)
51 throw new FileFormatException("File has become corrupt.");
52
53 long dat = Long.parseLong(words[1]);
54 logbase.add(new Entry(words[0], words[2], new Date(dat)));
55 }
56 }
57 }
58
59 /**
60 * Method to read the file
61 * @param path
62 * @param encoding
63 * @return
64 * @throws IOException
65 */
66 private String readFile(String path, Charset encoding) throws IOException{
67 byte[] encoded = Files.readAllBytes(Paths.get(path));
68 return new String(encoded, encoding);
69 }
70
71 /**
72 * Add log entry to Logs.txt
73 * @param userName
74 * @param text
75 */
76 public void createLogEntry(String userName, String text){
77 Entry entry = new Entry(userName, text, today);
78 logbase.add(entry);
79 try {
80 updateLogFile();
81 } catch (IOException e) {
82 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
83 "Error!",JOptionPane.WARNING_MESSAGE);
84 e.printStackTrace();
85 }
86 JOptionPane.showMessageDialog(null, "Successfully added entry",
87 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
88 }
89
90
91 public void editLogEntry(Entry entry){//TODO
92
93 }
94
95 //Returns entry creation date, creator, and text
96 public String viewLogEntry(Entry entry){
97 return entry.view();
98 }
99
100
101 public void deleteLogEntry(Entry entry){//TODO
102 logbase.remove(entry);
103 try {
104 updateLogFile();
105 } catch (IOException e) {
106 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
107 "Error!",JOptionPane.WARNING_MESSAGE);
108 e.printStackTrace();
109 }
110 JOptionPane.showMessageDialog(null, "Successfully deleted entry",
111 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
112 }
113
114
115 /**
116 * Clears Logs.txt file
117 */
118 public void deleteAllLogEntries(){
119 logbase.clear();
120 try {
121 updateLogFile();
122 } catch (IOException e) {
123 JOptionPane.showMessageDialog(null, "Error Updating Logs.txt File",
124 "Error!",JOptionPane.WARNING_MESSAGE);
125 e.printStackTrace();
126 }
127 JOptionPane.showMessageDialog(null, "Successfully deleted all entries",
128 "Entry List Updated", JOptionPane.WARNING_MESSAGE);
129 }
130
131
132 /**
133 * Updates log file
134 * @throws IOException
135 */
136 public void updateLogFile()throws IOException{
137 /*FileWriter writer = new FileWriter("Logs.txt", false);
138 try (BufferedWriter bw = new BufferedWriter(writer)) {
139 bw.write(this.toString());
140 }*/
141 Writer out = new BufferedWriter(new OutputStreamWriter(new
142 FileOutputStream("Logs.txt"), "UTF-8"));
143 try {
144 out.write(this.toString());
145 } finally {
146 out.close();
147 }
148 }
149
150
151 /**
152 * All entries to string formated for saving to file.
153 * @return
154 */
155 @Override
156 public String toString(){
157 String output = "";
158 for(Entry e: logbase){
159 output += e.toString() + "/;E/; ";
160 }
161 return output;
162 }
163 }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.