CS506 Assignment 1 Solution and Discussion
Please read the following instructions carefully before solving & submitting assignment:
• You are not allowed to use any IDE tool like NetBeans for this assignment.
• Use Notepad or Notepad++ for coding and JDK package for Java source code compilation and running.
• Place all the source code (.java & .class files) in a Zip/RAR file, save with your own Student ID (e.g. bc000000000.zip) and Upload it on VULMS within due date.
• Your assignment should be in .zip /.rar format. Other file formats will not be accepted.
• No assignment will be accepted through email.
Rules for Marking:
It should be clear that your assignment will not get any credit if:
o The assignment is submitted after due date.
o The submitted assignment does not open or file is corrupted.
o The assignment is fully or partially copied from other student or ditto copy from handouts or Internet; strict disciplinary action will be taken in this case.
o The assignment is not submitted in .zip /.rar format.
Note: Do not put any query on MDB regarding this assignment, if you have any query then email at
Lectures Covered: This assignment covers Lecture # 1-9
You are required to write a Java program, named ShoppingCart, for a typical online shopping system. In which a user can add and remove items to the shopping cart. The program will allow the user to proceed with checkout by displaying all items of the shopping cart along with total bill. Further, user will also be fascinated by giving the option of emptying the shopping cart without buying anything. All will be done by using Java basic GUI component i.e. JOptionPane.
At start, your program should display an Input Dialog (i.e. JOptionPane.showInputDialog) and ask for following options;
Add Item(s) to Cart
Remove an Item from Cart
Go to Checkout
Empty the Cart
Exit the Program
Fig. 1: Shopping Cart GUI
Add Item(s) to Cart:
Each time, user selects this option, s/he will be prompted via two Input Dialogs (i.e. JOptionPane.showInputDialog) for selecting an item from available items list (sample data is given below) and its quantity respectively. If inputs are correct (i.e. found no empty/null value and quantity is between 1 and 10) then selected item and its quantity will be added in the shopping cart by using ArrayList from java.util.* package.
Fig. 2: Adding items in Shopping Cart
- Remove an Item from Cart:
User can remove any item from the shopping cart by using this option; JOptionPane.showInputDialog should be used here. However, if shopping cart is already empty then message “Cart is empty” via JOptionPane.showMessageDialog should be displayed.
Fig. 3: Remove an Item
- Go to Checkout:
The program should first check if shopping cart contains any item; if not, it means no item is added yet so ask user to add a new item first. Otherwise, a Message Dialog (i.e. JOptionPane.showMessageDialog) will be opened, displaying all added items along with their quantity and prices. Furthermore, total number of items and grand total should be provided at bottom as well.
Fig. 4: Go to Checkout
4. Empty the Cart:
User can remove all items from the shopping cart at once by using this option; successful deletion message should be displayed by using JOptionPane.showMessageDialog. However, if shopping cart is already empty then appropriate message like "Cart is empty" should be displayed.
Fig. 5: Empty the Cart
5. Exit the Program:
Exit option will terminate the program and show the developer information (i.e. Student Id and name) via JOptionPane.showMessageDialog.
Fig. 6: Developer Info
Required Java Classes:
For this purpose, you have to create three java classes (i.e. Product, Cart and MainClass). Data members, member functions and other details of these classes are given below;
• id: int • name: String • price: float • quantity: int • Constructors: default, parameterized and copy constructor • Standard Setters: setId(…), setName(…), setPrice(…), setQuantity(…) • Standard Getters getId(), getName(), getPrice(), getQuantity()
Cart Class: • cartItems: ArrayList • Constructors: default, parameterized and copy constructor • Member Functions: addItem(…), removeItem(…), emptyCart(), getItemsList(), getProductIndex(…)
• myCart: Cart • Constructors: default, parameterized and copy constructor • Member Functions: main(), showGUI(), addItem(), removeItem(), checkOutCart(), removeAll(), developerInfo()
Important Things to Implement:
- For GUI, you have to use JOptionPane.showInputDialog and JOptionPane.showMessageDialog for taking input from user and showing output to user respectively.
- Use single ArrayList for adding/removing items to/from shopping cart.
- Need to make sure that exceptions are managed properly throughout the program; especially NullPointerException and NumberFormatException while taking input from user.
- All images and sample data, given in this document, are just for reference purpose only; you have to provide your own implementations. It is not required to be exactly the same.