I was wondering if i could incorporate swing, into the code to create a GUI?:

public class ComputerMerchandise {
           
            public static void main(String[] args) {
                        java.util.Hashtable<Integer, Product> htProducts = new java.util.Hashtable<Integer, Product>();
                        for(Product product:Products.GetAllAvailableProduct())
                        {
                                    htProducts.put(product.getProductId(),product);
                                    System.out.println(product.toString());
                        }
                        Hashtable<Integer,Integer> orders = new Hashtable<Integer,Integer>();
                        java.util.Scanner scanner = new Scanner(System.in);
                        while(true)
                        {
                                    System.out.print("To Order new item. Press Y to leave press any other key..");
                                    String input = scanner.next();
                                    if(!input.equalsIgnoreCase("Y")) break;
                                    System.out.print("Enter the product id:");
                                    int productId = scanner.nextInt();
                                    Product selectedProduct = htProducts.get(new Integer(productId));
                                    if(selectedProduct==null)
                                                System.out.println("InValid Product.");
                                    else
                                    {
                                                System.out.print("Enter nos:");
                                                int quantity = scanner.nextInt();
                                 Integer intOrdered = orders.get(new Integer(productId));
                                                int alreadyOrdered = intOrdered==null?0:intOrdered.intValue();
                                                if(alreadyOrdered<=0)
                                                {
                                                            orders.put(new Integer(productId), new Integer(quantity));
                                                }else
                                                {
                                                            orders.put(new Integer(productId), new Integer(quantity+alreadyOrdered));
                                                }
                                    }
                        }
                        if(!orders.isEmpty())
                        {
                                    double total = 0.0d;                             
                                    Enumeration<Integer> enu = orders.keys();
                                    while(enu.hasMoreElements())
                                    {
                                                Integer key = enu.nextElement();
                                                int nos = orders.get(key).intValue();
                                                Product soldProduct = htProducts.get(key);
                                                double price = soldProduct.getCost()*nos;
                                                total+=price;
                                                String display = "Product Id:"+soldProduct.getProductId()+",Item:"+soldProduct.getName()+",Quantity:"+nos+",Cost="+price;
                                                System.out.println(display);
                                    }
                                    if(total>0)
                                                System.out.println("Total="+total);
                        }
            }
}

class Products
{
            /*
             * Returns a collection of avilable products
             * */
            static Product[] GetAllAvailableProduct()
            {
                        Product[] products = {
                                                new Product("iBallMouse",600,1234),
                                                new Product("Samsung CRT Monitor",5000,3457),
                                                new Product("LG Keyboard",1500,200)
                        };
                        return products;
            }
}

class Product
{
            private double cost;
            private String name;
            private Integer productId;

            public Integer getProductId() {
                        return productId;
            }

            public void setProductId(Integer productId) {
                        this.productId = productId;
            }

            Product(String name,double cost,int productId)
            {
                        this.name = name;
                        this.cost = cost;
                        this.productId = new Integer(productId);
            }

            public String getName() {
                        return name;
            }

            public void setName(String name) {
                        this.name = name;
            }

            public double getCost() {
                        return cost;
            }

            public void setCost(double cost) {
                        this.cost = cost;
            }

            public String toString()
            {
                        return String.format("ProductId:%d,ProductName:%s,Price:%s", this.productId,this.name,this.cost);
            }
}

Swing is the new GUI classes for java.
Instead of prompting for input, present a window/form with input fields and some buttons.
The user fills in the input fields and presses a button for some program action to process the data.

You don't describe the function of the code you posted so its hard to recommend how to put your GUI together.

Without even reading the code, I'm going to say yes, you can. I may be wrong, but generally it's possible to bolt a Swing GUI onto most things.

What do you want the GUI to do? The design part is your job, though you might get some suggestions from the peanut gallery.

First you may try the class : javax.swing.JOptionPane
You should at first

import javax.swing.JOptionPane;

Then you may, for example, replace line 14 and 15 by the following code:

String input = JOptionPane.showInputDialog("To Order new item. Press Y to leave press any other key..");

Try it. Does it work?
Read API about this class (JOptionPane) try to use its other methods to replace your rest DOS input/output .

This is one of the selections from swing. You may use JTextField, JButton, and others as well.

Edited 6 Years Ago by tong1: n/a

This article has been dead for over six months. Start a new discussion instead.