Basically I want to create a GUI that allows the user to select an item from a given list then displays the total amount of the added items which is the price. So, it will be quantity*price of the item. The quantity will be entered from the user. SO far, this is the code and I want to modify the code in a way that the user will enter the quantity and it will be overall calculated and displayed in the GUI.
How can I add that the user will specify the quantity of the item into the GUI?
Class Item
public class Item {
private String itemName;
private Integer itemPrice;
public Item(){}
public Item(String itemName, int itemPrice){
this.itemName = itemName;
this.itemPrice = itemPrice;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Integer getItemPrice() {
return itemPrice;
}
public void setItemPrice(Integer itemPrice) {
this.itemPrice = itemPrice;
}
@Override
public String toString(){
return this.itemName;
}
}
Class ShoppingCart
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.swing.*;
public class ShoppingCart extends JFrame {
private JList leftlist;
private JList rightlist;
private JButton addbutton;
private JButton totalbutton;
private JLabel totalLabel;
public ShoppingCart() {
super("Shopping Cart");
setLayout(new FlowLayout());
// preparing item list it might be coming from somewhere else
List<Item> itemList = new ArrayList<Item>();
itemList.add(new Item("Apple", 10));
itemList.add(new Item("Carrot", 40));
itemList.add(new Item("Cucumber", 50));
itemList.add(new Item("Iphone 6", 6000));
itemList.add(new Item("Galaxy S6", 1100));
itemList.add(new Item("BlackBerry", 1300));
itemList.add(new Item("HairDryer", 200));
itemList.add(new Item("Ironer", 300));
itemList.add(new Item("Vacuum Cleaner", 400));
leftlist = new JList(itemList.toArray());
leftlist.setVisibleRowCount(5);
leftlist
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
addbutton = new JButton("ADD");
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//make sure you preserve the previously selected list items
int size = rightlist.getModel().getSize();
Set objects = new LinkedHashSet();
for (int i = 0; i < size; i++) {
objects.add(rightlist.getModel().getElementAt(i));
}
objects.addAll(Arrays.asList(leftlist.getSelectedValues()));
rightlist.setListData(objects.toArray());
}
});
add(addbutton);
rightlist = new JList();
rightlist.setVisibleRowCount(5);
rightlist.setFixedCellWidth(55);
rightlist.setFixedCellHeight(20);
add(new JScrollPane(rightlist));
totalbutton = new JButton("Total");
totalbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// iterate over item objects and calculate the total
int size = rightlist.getModel().getSize();
Integer totalAmount = 0;
for (int i = 0; i < size; i++) {
Object obj = leftlist.getModel().getElementAt(i);
Item item = (Item) obj;
totalAmount += item.getItemPrice();
}
//update the total Amount label
totalLabel.setText("Total Amount : " + totalAmount);
}
});
add(totalbutton);
totalLabel = new JLabel("Total Amount : ");
add(totalLabel);
}
public static void main(String[] args) {
ShoppingCart list = new ShoppingCart();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.setSize(300, 300);
list.setVisible(true);
}
}