Store products in a TreeSet and print the content

2019-01-29 09:47发布

I'm trying to create a GUI which will let the user introduce and remove products in a GUI, by using a TreeSet. The user specifies the product name and the quantity, before choosing which operation to perform: add product or remove product.

My question is: how do I print a JTable that can be updated constantly each time the user adds or removes a product?

This is what I have so far. You'll also see the table content I'm planning to display. I don't know if I'm using the TreeSet right, but I'd appreciate any help.

  public class Warehouse implements Serializable {
        private int nrOfProducts = 0;
        private TreeSet<Product> products = new TreeSet<Product>();
        private JTable productsTable;
        private Object[][] entries = new Object[100][10];
        private String[] columnName = {"Product name", "Price", "In stock", "Understock", "Overstock"};

        public Warehouse() {

        }

        public Warehouse(Product product, int nrOfProducts) {
            this.nrOfProducts = 0;
            this.products = new TreeSet();
        }

        public void addProduct(Product newProduct) {
            products.add(newProduct);
            nrOfProducts++;
        }

        public void removeProduct(Product product) {
            products.remove(product);
            nrOfProducts--;
        }

        public int getNrOfProducts() {
            return nrOfProducts;
        }

        public void initFile() {
            try {
                FileOutputStream fos = new FileOutputStream("products.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(products);
                oos.close();
            }
            catch(Exception exc) {
                exc.printStackTrace();
            }
        }

        public void readFile() {
            try {
                FileInputStream fis = new FileInputStream("products.ser");
                ObjectInputStream ois = new ObjectInputStream(fis);
                Product prod = (Product) ois.readObject();
                while (prod != null)
                    addProduct(prod);
                ois.close();
        }
        catch(Exception exc) {
            exc.printStackTrace();
        }
        }

        public void printProducts() {
            for (Product p: products) {
               // here's where I got stuck
            }
        }
    }

I'll also attach here the Product class:

public class Product implements Comparable {
    String productName;
    private int price;
    private int stock;
    private boolean understock;
    private boolean overstock;

    public Product() {

    }

    public Product(String productName, int price, boolean understock, boolean overstock, int stock) {
        this.productName = productName;
        this.price = price;
        this.understock = understock;
        this.overstock = overstock;
        this.stock = stock;
        if (stock > 15) {
            this.overstock = true;
            this.understock = false;
        }
        else if (stock < 3) {
            this.overstock = false;
            this.understock = true;
        }
        else {
            this.overstock = false;
            this.understock = false;
        }
    }

    public int compareTo(Object obj) {
        Product prod = (Product) obj;
        return (productName.compareTo(prod.productName));
    }
}

1条回答
欢心
2楼-- · 2019-01-29 10:45

Extend AbstractTableModel as shown here. In this case, you can access the TreeSet in your implementation of getValueAt(), et al., as shown here for Map. Because TreeSet implements SortedSet, the toArray() method will return a sorted array of Product based on your implementation of Comparable.

查看更多
登录 后发表回答