-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtractFrame.java
More file actions
161 lines (148 loc) · 5.97 KB
/
Copy pathSubtractFrame.java
File metadata and controls
161 lines (148 loc) · 5.97 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class SubtractFrame extends JFrame implements ActionListener{
File textFile;
FileWriter out;
BufferedWriter writeFile;
FileReader in;
BufferedReader readFile;
String[] arr;
String lineOfText;
JScrollPane scroll;
JLabel logo, enterstockPrompt, amountPrompt;
JTextArea area;
JButton back, subtract;
JTextField itemField, numberField;
JPanel addPanel,enterstockPanel,textarea;
ApplicationFrame mainframe;
SubtractFrame() {//this frame allows users to subtract amounts of items from inventory
addWindowListener(new WindowAdapter() {//detects when user clicks the exit button
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(null, "By not properly exiting, your inventory could be wiped","Confirmation",JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
ImageIcon img = new ImageIcon(getClass().getResource("/nofrills.png"));
Image image = img.getImage(); // make an Image object from the ImageIcon object
Image newImage = image.getScaledInstance(200, 200, Image.SCALE_SMOOTH); // transform it
img = new ImageIcon(newImage);
logo= new JLabel();
logo.setIcon(img);
subtract = new JButton("Subtract");
subtract.addActionListener(this);
back = new JButton("Back");
back.addActionListener(a->{//back button closes this frame and reopens the main frame
mainframe = new ApplicationFrame();this.dispose();
});
itemField = new JTextField(10);
numberField = new JTextField(5);
enterstockPrompt = new JLabel("Enter stock: ");
amountPrompt = new JLabel("Amount: ");
enterstockPanel = new JPanel();
enterstockPanel.add(back);
enterstockPanel.add(enterstockPrompt);
enterstockPanel.add(itemField);
enterstockPanel.add(amountPrompt);
enterstockPanel.add(numberField);
enterstockPanel.add(subtract);
try {
textFile = new File("inventory.txt");//reads from inventory.txt
in = new FileReader(textFile);
readFile = new BufferedReader(in);
lineOfText = readFile.readLine();
if(lineOfText==null) {
lineOfText = " ";
}
arr = lineOfText.split(" ");
}
catch(FileNotFoundException e){//file not found exception
JOptionPane.showMessageDialog(null, "File not found","Error",JOptionPane.ERROR_MESSAGE);
}
catch(IOException e){//io exception
JOptionPane.showMessageDialog(null, "IOException","Error",JOptionPane.ERROR_MESSAGE);
}
catch(Exception e) {//catches everything else
JOptionPane.showMessageDialog(null, "An error has occured","Error",JOptionPane.ERROR_MESSAGE);
}
area = new JTextArea(10,20);
for(int i = 0; i<arr.length-1;i=i+2) {
area.setText(area.getText() + "\n" + arr[i] + " " + arr[i+1]);
}
scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
area.setEditable(false);
addPanel = new JPanel();
addPanel.add(logo);
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
getContentPane().add(addPanel);
getContentPane().add(enterstockPanel);
getContentPane().add(scroll);
setTitle("Add");
setSize(500,400);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
out = new FileWriter(textFile);
writeFile = new BufferedWriter(out);
String itemID;
int subtractAmount;
try {
itemID = itemField.getText().replaceAll(" ", ""); //grabs the item name and removes all spaces
subtractAmount = Integer.parseInt(numberField.getText());//parses the string from the numberfield
boolean itemDupe = false;//checks for dupes
for(int i = 0; i < arr.length-1; i=i+2) {
if(itemID.equals(arr[i])) {//if itemid is found in the array then subtract from amount
itemDupe = true;
if(Integer.parseInt(arr[i+1])-subtractAmount<0) {//catches if number goes below 0
JOptionPane.showMessageDialog(null, "Item cannot be subtracted below 0","Error",JOptionPane.ERROR_MESSAGE);
}
else {
arr[i+1] = String.valueOf(Integer.parseInt(arr[i+1])-subtractAmount);
}
lineOfText = "";
for(int j = 0; j < arr.length-1;j=j+2) {//recreates lineOftext
lineOfText = lineOfText + " " + arr[j] + " " + arr[j+1];//im creating an extra space at beginning
}
lineOfText = lineOfText.replaceFirst(" ", "");//removing the first space
}
}
if(itemDupe==false) {//if item is not found then there is nothing to subtract from
JOptionPane.showMessageDialog(null, "Item not found","Missing Item", JOptionPane.ERROR_MESSAGE);
}
}
catch(NumberFormatException err) {
JOptionPane.showMessageDialog(null, "Invalid Number","Error",JOptionPane.ERROR_MESSAGE);
}
catch(Exception err) {
JOptionPane.showMessageDialog(null, "Something wrong happened","Error",JOptionPane.ERROR_MESSAGE);
}
area.setText("");
for(int i = 0; i<arr.length-1;i=i+2) {
area.setText(area.getText() + "\n" + arr[i] + " " + arr[i+1]);
}
writeFile.write(lineOfText);//writes lineoftext to the file
writeFile.close();
out.close();
}
catch(IOException err){
JOptionPane.showMessageDialog(null, "IOException","Error",JOptionPane.ERROR_MESSAGE);
}
}
}