上诉代码为啥无法更行文件
时间: 2024-04-18 16:32:26 浏览: 11
对不起,我之前提供的代码中确实没有包含更新文件的功能。以下是添加了更新文件功能的修改后代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SupermarketCheckout {
private JFrame frame;
private JTextField searchField, nameField, quantityField, priceField;
private JTextArea cartArea;
private JLabel totalPriceLabel;
private JButton addButton, checkoutButton;
private double totalPrice;
public SupermarketCheckout() {
frame = new JFrame("Supermarket Checkout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
JLabel searchLabel = new JLabel("Search:");
searchField = new JTextField(15);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new SearchButtonListener());
topPanel.add(searchLabel);
topPanel.add(searchField);
topPanel.add(searchButton);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4, 2));
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(15);
nameField.setEditable(false);
JLabel quantityLabel = new JLabel("Quantity:");
quantityField = new JTextField(15);
JLabel priceLabel = new JLabel("Price:");
priceField = new JTextField(15);
priceField.setEditable(false);
addButton = new JButton("Add");
addButton.setEnabled(false);
addButton.addActionListener(new AddButtonListener());
centerPanel.add(nameLabel);
centerPanel.add(nameField);
centerPanel.add(quantityLabel);
centerPanel.add(quantityField);
centerPanel.add(priceLabel);
centerPanel.add(priceField);
centerPanel.add(addButton);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
JLabel cartLabel = new JLabel("Cart:");
cartArea = new JTextArea(10, 30);
cartArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(cartArea);
JPanel totalPricePanel = new JPanel();
totalPricePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel totalPriceTextLabel = new JLabel("Total Price:");
totalPriceLabel = new JLabel("0.0");
totalPricePanel.add(totalPriceTextLabel);
totalPricePanel.add(totalPriceLabel);
bottomPanel.add(cartLabel, BorderLayout.NORTH);
bottomPanel.add(scrollPane, BorderLayout.CENTER);
bottomPanel.add(totalPricePanel, BorderLayout.SOUTH);
checkoutButton = new JButton("Checkout");
checkoutButton.addActionListener(new CheckoutButtonListener());
frame.add(topPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.add(checkoutButton, BorderLayout.EAST);
frame.setVisible(true);
}
private void searchProduct(String name) {
try {
Scanner scanner = new Scanner(new File("1.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
if (parts[0].equals(name)) {
nameField.setText(parts[0]);
quantityField.setText(parts[1]);
priceField.setText(parts[2]);
addButton.setEnabled(true);
return;
}
}
// If product not found
nameField.setText("");
quantityField.setText("");
priceField.setText("");
addButton.setEnabled(false);
JOptionPane.showMessageDialog(frame, "Product not found!");
} catch (FileNotFoundException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "File not found!");
}
}
private void addToCart() {
String name = nameField.getText();
int quantity = Integer.parseInt(quantityField.getText());
double price = Double.parseDouble(priceField.getText());
double subtotal = quantity * price;
cartArea.append(name + " x " + quantity + " - $" + subtotal + "\n");
totalPrice += subtotal;
totalPriceLabel.setText(String.valueOf(totalPrice));
updateQuantityInFile(name, quantity);
}
private void updateQuantityInFile(String name, int quantity) {
try {
File inputFile = new File("1.txt");
File tempFile = new File("temp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts[0].equals(name)) {
int originalQuantity = Integer.parseInt(parts[1]);
int updatedQuantity = originalQuantity - quantity;
writer.write(parts[0] + "," + updatedQuantity + "," + parts[2]);
} else {
writer.write(line);
}
writer.newLine();
}
reader.close();
writer.close();
if (inputFile.delete()) {
tempFile.renameTo(inputFile);
} else {
throw new IOException("Failed to update file.");
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Failed to update file!");
}
}
private void checkout() {
frame.dispose();
JFrame checkoutFrame = new JFrame("Checkout");
checkoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkoutFrame.setSize(300, 200);
checkoutFrame.setLayout(new FlowLayout());
JLabel thankYouLabel = new JLabel("Thank you for shopping!");
JLabel totalLabel = new JLabel("Total Price: $" + totalPrice);
checkoutFrame.add(thankYouLabel);
checkoutFrame.add(totalLabel);
checkoutFrame.setVisible(true);
}
private class SearchButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String productName = searchField.getText();
searchProduct(productName);
}
}
private class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
addToCart();
}
}
private class CheckoutButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
checkout();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SupermarketCheckout();
}
});
}
}
```
这次更新后的代码包含了更新文件的功能。当点击“添加”按钮时,会将商品的数量更新到文件中,以确保文件中的商品数量与购物车中的数量保持一致。
请注意,在运行代码之前,请确保将名为`1.txt`的商品信息文件放置在与代码文件相同的目录中,并按照以下格式编写文件内容:
```
商品1,数量1,价格1
商品2,数量2,价格2
...
```
这样,当你点击“添加”按钮时,文件中对应商品的数量将被更新。