-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellPhoneInventory.java
149 lines (133 loc) · 6.19 KB
/
CellPhoneInventory.java
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
/**
Lab 5 Starter program
The CellPhoneInventory class build the CellPhone Inventory GUI
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Paths;
import java.util.*;
public class CellPhoneInventory extends JFrame {
JLabel labelModel, labelManufacturer, labelRetailPrice, labelBanner;
JTextField jTextFieldModel, jTextFieldManufacturer, jTextFieldRetailPrice;
JButton jButtonAdd, jButtonSave, jButtonNext,jButtonShow;
JPanel newPanel,buttonPanel;
ArrayList<CellPhone> phoneArrayList = new ArrayList<CellPhone>();
CellPhoneInventory() {
setTitle("Cellphone Inventory");
setLayout(new BorderLayout());
labelBanner = new JLabel("Cellphone Inventory Management");
labelBanner.setFont(new Font("Serif",Font.BOLD,20));
labelBanner.setForeground(Color.BLUE);
labelModel = new JLabel("Model");
labelManufacturer = new JLabel("Manufacturer");
labelRetailPrice = new JLabel("Retail Price");
jTextFieldModel = new JTextField(15);
jTextFieldManufacturer = new JTextField(15);
jTextFieldRetailPrice = new JTextField(15);
jButtonAdd = new JButton("Add");
jButtonSave = new JButton("Save");
jButtonNext = new JButton("Next");
jButtonShow = new JButton("Show Inventory");
newPanel = new JPanel(new GridLayout(3, 2));
newPanel.add(labelModel);
newPanel.add(jTextFieldModel);
newPanel.add(labelManufacturer);
newPanel.add(jTextFieldManufacturer);
newPanel.add(labelRetailPrice);
newPanel.add(jTextFieldRetailPrice);
buttonPanel = new JPanel(new GridLayout(2,2));
buttonPanel.add(jButtonAdd);
buttonPanel.add(jButtonNext);
buttonPanel.add(jButtonSave);
buttonPanel.add(jButtonShow);
add(labelBanner, BorderLayout.NORTH);
add(newPanel, BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
jButtonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String model = jTextFieldModel.getText();
String manufacturer = jTextFieldManufacturer.getText();
double price = Double.parseDouble(jTextFieldRetailPrice.getText());
// Lab 5 Step 4: incorporate CellPhone's user-defined Exception classes
// here in try catch block
//.............
//............
//.............
phoneArrayList.add(new CellPhone(model, manufacturer, price));
String InventoryDisplay = "";
for (CellPhone p : phoneArrayList) {
System.out.println(p);
InventoryDisplay += p;
}
JOptionPane.showMessageDialog(null, InventoryDisplay);
}
});
jButtonNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextFieldModel.setText(null);
jTextFieldManufacturer.setText(null);
jTextFieldRetailPrice.setText(null);
}
});
jButtonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Formatter outCellPhoneList = null;
try {
// file stream for output file
outCellPhoneList = new Formatter("cellPhones.txt");
for (int i = 0; i < phoneArrayList.size(); i++) {
outCellPhoneList.format("%s %s %.2f\n", phoneArrayList.get(i).getModel(),
phoneArrayList.get(i).getManufacturer(),
phoneArrayList.get(i).getRetailPrice());
}
System.out.println("cellphone list stored in txt file.");
} catch (SecurityException securityException) {
System.err.println(
"You do not have write access to this file.");
System.exit(1);
} catch (FileNotFoundException fileNotFoundException) {
System.err.println(
"Error creating file.");
System.exit(1);
} catch (FormatterClosedException closedException) {
System.err.println(
"Error writing to file - file has been closed.");
System.exit(1);
} catch (IllegalFormatException formatException) {
System.err.println("Error with output.");
System.exit(1);
} finally {
if (outCellPhoneList != null) {
outCellPhoneList.close();
}}
}
});
jButtonShow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = String.format("%-20s%-20s%10s%n","Model", "Manufacturer",
"retail price");
// open cellPhoneList.txt, read its contents and close the file
try(Scanner input = new Scanner(Paths.get("cellPhones.txt"))) {
// read record from file
while (input.hasNext()) { // while there is more to read
// display record contents
msg += String.format("%-20s%-20s%10.2f%n",input.next(),
input.next(), input.nextDouble());
}
}
catch ( NoSuchElementException | IllegalStateException | IOException exception) {
//e.printStackTrace();
System.out.println("Error: " + exception.getMessage());
}
JOptionPane.showMessageDialog(null,msg);
}
});
}
}