在课堂上,我们正在创建一个程序,它是汽车零件的库存。我们打开一个包含汽车零件的文本文件,我们可以使用名称,零件名称,库存数量,序列号和价格搜索特定零件。找到名称,部件名称,库存数量,序列号或价格后,我们可以使用这些文本字段进行更改。然后,在文本字段中输入的内容将保存到数组中,但是,我需要使用新信息覆盖该数组并将其保存到文本文件中。我怎么能这样做?
public class CarPartsGUI extends javax.swing.JFrame {
public CarPartsGUI() {
initComponents();
}
//OBJECT ARRAY
CarPart[] partsArray;
int location;
int arraySize;
//variables for array
String make;
String name;
String serialNumber;
double price;
int onHand;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
inputTxt = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
openButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
findButton = new javax.swing.JButton();
updateButton = new javax.swing.JButton();
partNamTxt = new javax.swing.JTextField();
onHandTxt = new javax.swing.JTextField();
serialNumTxt = new javax.swing.JTextField();
priceTxt = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
display = new javax.swing.JTextArea();
jLabel6 = new javax.swing.JLabel();
makeTxt = new javax.swing.JTextField();
clearButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Enter File Name:");
openButton.setText("OPEN");
openButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openButtonActionPerformed(evt);
}
});
saveButton.setText("SAVE");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
findButton.setText("FIND");
findButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
findButtonActionPerformed(evt);
}
});
updateButton.setText("UPDATE");
updateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateButtonActionPerformed(evt);
}
});
partNamTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
partNamTxtActionPerformed(evt);
}
});
jLabel2.setText("Part Name");
jLabel3.setText("On Hand");
jLabel4.setText("Serial #");
jLabel5.setText("Price");
display.setEditable(false);
display.setColumns(20);
display.setRows(5);
jScrollPane1.setViewportView(display);
jLabel6.setText("Make");
clearButton.setText("CLEAR");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(findButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(openButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(saveButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(updateButton, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(clearButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(onHandTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(partNamTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(serialNumTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(priceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(makeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(openButton)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(saveButton)
.addGap(18, 18, 18)
.addComponent(findButton)
.addGap(18, 18, 18)
.addComponent(updateButton)
.addGap(18, 18, 18)
.addComponent(clearButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(makeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(partNamTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(onHandTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(serialNumTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(priceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addGap(19, 19, 19))))
);
pack();
}// </editor-fold>
//C:/Users/287648/Desktop/ExhaustParts.txt
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
try{
File file = new File(inputTxt.getText());
FileReader fileReader1 = new FileReader(file);
BufferedReader bRGetNumLines = new BufferedReader(fileReader1);
int lines = 0;
while(bRGetNumLines.readLine()!= null){
lines++;
}
fileReader1.close();
arraySize = lines/5;
partsArray = new CarPart[arraySize];
FileReader fileReader2 = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader2);
String line;
for(int counter = 0; counter < arraySize; counter++){
make = bufferedReader.readLine();
name = bufferedReader.readLine();
serialNumber = bufferedReader.readLine();
price = Double.parseDouble(bufferedReader.readLine());
onHand = Integer.parseInt(bufferedReader.readLine());
partsArray[counter] = new CarPart(name, serialNumber, price, onHand, make);
display.append(partsArray[counter].getMake()+" ");
display.append(partsArray[counter].getName()+" ");
display.append(partsArray[counter].getSerialNum()+" ");
display.append(Double.toString(partsArray[counter].getPrice())+" ");
display.append(Integer.toString(partsArray[counter].getOnHand())+" ");
display.append("\n");
}
fileReader2.close();
}
catch (java.io.FileNotFoundException e){
JOptionPane.showMessageDialog(inputTxt, "File not found");
}
catch (IOException e){
}
}
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
//saves every update, and overrides new information
// overrides array
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(inputTxt.getText(), false));
for (int i = 0; partsArray.length; i++) {
CarPart x = partsArray[i];
writer.write(x);
}
}
}
private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {
String sNum;
sNum = serialNumTxt.getText();
location = 0;
boolean cont = true;
while(location < arraySize && cont){
if(partsArray[location].getSerialNum().equals(sNum))
cont = false;
else location++;
}
if(location == arraySize)
JOptionPane.showMessageDialog(null, "Serial Number Not Found");
else{
display.setText(null);
display.append(partsArray[location].getMake());
display.append(partsArray[location].getName());
display.append(partsArray[location].getSerialNum());
display.append(Double.toString(partsArray[location].getPrice()));
display.append(Integer.toString(partsArray[location].getOnHand()));
}
}
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {
String sNum, stock, p;
sNum = serialNumTxt.getText();
stock = onHandTxt.getText();
p = priceTxt.getText();
if(sNum.equals("")){
JOptionPane.showMessageDialog(serialNumTxt, "Serial Number not entered");
}
if(!(p.equals(""))){
partsArray[location].setPrice(Double.parseDouble(priceTxt.getText()));
}
if(!(stock.equals(""))){
partsArray[location].setOnHand(Integer.parseInt(onHandTxt.getText()));
}
if(stock.equals("") && p.equals("")){
JOptionPane.showMessageDialog(null, "You didn't enter an update value!");
}
display.setText(null);
display.append(partsArray[location].getMake()+"\n");
display.append(partsArray[location].getName()+"\n");
display.append(partsArray[location].getSerialNum()+"\n");
display.append(Double.toString(partsArray[location].getPrice())+"\n");
display.append(Integer.toString(partsArray[location].getOnHand())+"\n");
}
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
inputTxt.setText("");
display.setText("");
partNamTxt.setText("");
onHandTxt.setText("");
serialNumTxt.setText("");
priceTxt.setText("");
makeTxt.setText("");
}