用FJrame写一个Java代码,做一个超市商品结算界面并可以通过坐标调整,读取文件1.txt,文件包括商品名,商品数量,商品价格,运行代码后,将会弹出一个窗口显示超市商品结算界面。你可以输入商品名称并点击搜索按钮来查找商品信息。找到商品后,它的名称、数量和价格将显示在相应的文本框中。你可以修改数量并点击“添加”按钮将商品添加到购物车中,在购物车中有删除该商品的按键。购物车中的商品将显示在文本区域中,并计算出总价。当你点击“结算”按钮时,将会跳转到一个新的界面显示感谢信息和总价且修改文件里相对于的商品数量,即现在的数量为初始数量减去购买数量。
时间: 2024-04-18 19:32:42 浏览: 179
以下是修改后的代码,实现了超市商品结算界面的功能,并在购物车中添加了删除商品的按钮,并在结算时更新文件中的商品数量:
```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(null);
JLabel searchLabel = new JLabel("Search:");
searchLabel.setBounds(20, 20, 50, 30);
frame.add(searchLabel);
searchField = new JTextField();
searchField.setBounds(80, 20, 150, 30);
frame.add(searchField);
JButton searchButton = new JButton("Search");
searchButton.setBounds(240, 20, 80, 30);
searchButton.addActionListener(new SearchButtonListener());
frame.add(searchButton);
JLabel nameLabel = new JLabel("Name:");
nameLabel.setBounds(20, 70, 50, 30);
frame.add(nameLabel);
nameField = new JTextField();
nameField.setBounds(80, 70, 150, 30);
nameField.setEditable(false);
frame.add(nameField);
JLabel quantityLabel = new JLabel("Quantity:");
quantityLabel.setBounds(20, 120, 60, 30);
frame.add(quantityLabel);
quantityField = new JTextField();
quantityField.setBounds(80, 120, 150, 30);
frame.add(quantityField);
JLabel priceLabel = new JLabel("Price:");
priceLabel.setBounds(20, 170, 50, 30);
frame.add(priceLabel);
priceField = new JTextField();
priceField.setBounds(80, 170, 150, 30);
priceField.setEditable(false);
frame.add(priceField);
addButton = new JButton("Add");
addButton.setBounds(240, 170, 80, 30);
addButton.setEnabled(false);
addButton.addActionListener(new AddButtonListener());
frame.add(addButton);
JLabel cartLabel = new JLabel("Cart:");
cartLabel.setBounds(20, 220, 50, 30);
frame.add(cartLabel);
cartArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(cartArea);
scrollPane.setBounds(80, 220, 240, 100);
frame.add(scrollPane);
JButton deleteButton = new JButton("Delete");
deleteButton.setBounds(330, 220, 80, 30);
deleteButton.addActionListener(new DeleteButtonListener());
frame.add(deleteButton);
JLabel totalPriceTextLabel = new JLabel("Total Price:");
totalPriceTextLabel.setBounds(250, 340, 80, 30);
frame.add(totalPriceTextLabel);
totalPriceLabel = new JLabel("0.0");
totalPriceLabel.setBounds(330, 340, 80, 30);
frame.add(totalPriceLabel);
checkoutButton = new JButton("Checkout");
checkoutButton.setBounds(330, 290, 80, 30);
checkoutButton.addActionListener(new CheckoutButtonListener());
frame.add(checkoutButton);
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 deleteFromCart() {
String selectedText = cartArea.getSelectedText();
if (selectedText != null) {
String[] parts = selectedText.split(" x ");
String name = parts[0];
int quantity = Integer.parseInt(parts[1].split(" - ")[0]);
double price = Double.parseDouble(parts[1].split(" - \\$")[1]);
double subtotal = quantity * price;
cartArea.replaceSelection("");
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 DeleteButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
deleteFromCart();
}
}
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
...
```
运行代码后,将会弹出一个窗口显示超市商品结算界面。你可以输入商品名称并点击搜索按钮来查找商品信息。找到商品后,它的名称、数量和价格将显示在相应的文本框中。你可以修改数量并点击“添加”按钮将商品添加到购物车中。购物车中的商品将显示在文本区域中,并计算出总价。你可以选择购物车中的商品,并点击“删除”按钮将其从购物车中删除,并更新文件中的商品数量。当你点击“结算”按钮时,将会跳转到一个新的界面显示感谢信息和总价。
请注意,这只是一个简单的示例,可能需要根据具体需求进行进一步的修改和完善。
阅读全文