21xrx.com
2025-03-18 23:59:14 Tuesday
文章检索 我的文章 写文章
《用Java编写购物程序,让购物更方便!》
2023-06-15 07:21:41 深夜i     --     --
Java 购物程序 Swing

购物是一种常见的消费行为,而且随着互联网技术的发展,越来越多的人开始购物,尤其是在线购物。为了让购物更方便和更高效,我们可以用Java编写一个购物程序。

在这个购物程序中,我们首先需要考虑的是商品的信息存储问题。我们可以使用Java中的数组或者集合来存储商品的信息,比如商品名称、价格、库存等信息。下面是一个商品类的示例:

class Commodity {
  private String name;
  private double price;
  private int quantity;
  public Commodity(String name, double price, int quantity)
    this.name = name;
    this.price = price;
    this.quantity = quantity;
  
  // getter and setter methods
}

接下来,我们需要实现购物车的功能。购物车应该支持添加商品、删除商品、修改商品数量和计算总价等功能。下面是一个购物车类的示例:

import java.util.ArrayList;
import java.util.List;
class ShoppingCart {
  private List
  items;
 
  public ShoppingCart() {
    this.items = new ArrayList<>();
  }
  public boolean addItem(Commodity item) {
    return this.items.add(item);
  }
  public boolean removeItem(Commodity item) {
    return this.items.remove(item);
  }
  public boolean updateItemQuantity(Commodity item, int quantity) {
    if (!this.items.contains(item))
      return false;
    
    item.setQuantity(quantity);
    return true;
  }
  public double getTotalPrice() {
    double totalPrice = 0;
    for (Commodity item : this.items) {
      totalPrice += item.getPrice() * item.getQuantity();
    }
    return totalPrice;
  }
}

最后,我们需要实现用户界面,让用户可以方便地使用购物程序。用户界面可以使用Java Swing框架来实现。下面是一个用户界面类的示例:


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class ShoppingPanel extends JPanel {

  private JTextField nameField;

  private JTextField priceField;

  private JTextField quantityField;

  private JButton addButton;

  private JButton removeButton;

  private JButton updateButton;

  private JList itemList;

  private JTextArea totalPriceArea;

  private ShoppingCart shoppingCart;

  public ShoppingPanel() {

    this.shoppingCart = new ShoppingCart();

    this.setLayout(new BorderLayout());

    JPanel controlPanel = new JPanel();

    controlPanel.setLayout(new GridLayout(4, 2));

    this.nameField = new JTextField();

    controlPanel.add(new JLabel("Name:"));

    controlPanel.add(this.nameField);

    this.priceField = new JTextField();

    controlPanel.add(new JLabel("Price:"));

    controlPanel.add(this.priceField);

    this.quantityField = new JTextField();

    controlPanel.add(new JLabel("Quantity:"));

    controlPanel.add(this.quantityField);

    this.addButton = new JButton("Add");

    this.addButton.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        String name = nameField.getText();

        double price = Double.parseDouble(priceField.getText());

        int quantity = Integer.parseInt(quantityField.getText());

        Commodity item = new Commodity(name, price, quantity);

        shoppingCart.addItem(item);

        itemList.updateUI();

        totalPriceArea.setText("Total price: " + shoppingCart.getTotalPrice());

      }

    });

    controlPanel.add(this.addButton);

    this.removeButton = new JButton("Remove");

    this.removeButton.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        Commodity item = itemList.getSelectedValue();

        if (item != null) {

          shoppingCart.removeItem(item);

          itemList.updateUI();

          totalPriceArea.setText("Total price: " + shoppingCart.getTotalPrice());

        }

      }

    });

    controlPanel.add(this.removeButton);

    this.updateButton = new JButton("Update");

    this.updateButton.addActionListener(new ActionListener() {

      @Override

      public void actionPerformed(ActionEvent e) {

        Commodity item = itemList.getSelectedValue();

        if (item != null) {

          int quantity = Integer.parseInt(quantityField.getText());

          shoppingCart.updateItemQuantity(item, quantity);

          itemList.updateUI();

          totalPriceArea.setText("Total price: " + shoppingCart.getTotalPrice());

        }

      }

    });

    controlPanel.add(this.updateButton);

    this.add(controlPanel, BorderLayout.NORTH);

    JPanel listPanel = new JPanel();

    listPanel.setLayout(new BorderLayout());

    this.itemList = new JList<>();

    this.itemList.setModel(new DefaultListModel<>());

    this.itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane scrollPane = new JScrollPane(this.itemList);

    listPanel.add(scrollPane, BorderLayout.CENTER);

    this.add(listPanel, BorderLayout.CENTER);

    this.totalPriceArea = new JTextArea();

    this.totalPriceArea.setEditable(false);

    listPanel.add(this.totalPriceArea, BorderLayout.SOUTH);

  }

}

三个

  
  

评论区