Share:
Notifications
Clear all

[Solved] CS506 Assignment 1 Fall 2020  


admin
(@admin)
Admin
Joined: 2 years ago
Posts: 5384
Topic starter  

CS506 Web Design and Development Assignment 1 Solution & Discussion Fall 2020


Please read the following instructions carefully before solving & submitting assignment:

 

Uploading Instructions:

  • 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:

 

  • The assignment is submitted after due date.
  • The submitted assignment does not open or file is corrupted.
  • 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.
  • 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 [email protected]

Lectures Covered: This assignment covers Lecture # 1-9

 

GOOD LUCK

Problem Statement:

You are required to write a Java program, named BookShop, to purchase online CS506 course’s helping material. In this program a user can add and remove items to the BookShop Cart. The program will allow the user to proceed with checkout by displaying all items of the BookShop cart along with total bill. Further, user will also be fascinated by giving the option of emptying the BookShop cart without buying anything. All will be done by using Java basic GUI component i.e. JOptionPane.

 

Detailed Description:

At start, your program should display an Input Dialog (i.e. JOptionPane.showInputDialog) and ask for following options;

  1. Add Item(s) to Cart
  2. Remove an Item from Cart
  3. Go to Checkout
  4. Empty the Cart
  5. Exit the Program

Fig. 1: BookShop Cart GUI

 

  1. 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 BookShop cart by using ArrayList from java.util.* package.

 

Fig. 2: Adding items in BookShop Cart

  1. Remove an Item from Cart:

User can remove any item from the BookShop cart by using this option; JOptionPane.showInputDialog should be used here. However, if BookShop cart is already empty then message "Cart is empty" via JOptionPane.showMessageDialog should be displayed.

 

Fig. 3: Remove an Item

 

  1. Go to Checkout:

The program should first check if BookShop 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

  1. Empty the Cart:

User can remove all items from the BookShop cart at once by using this option; successful deletion message should be displayed by using JOptionPane.showMessageDialog. However, if BookShop cart is already empty then appropriate message like "Cart is empty" should be displayed.

 

Fig. 5: Empty the Cart

  1. 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;

 

Product Class:

  • 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(…)

BookShop Class: (This is main class)

  • myCart: Cart
  • Constructors: default, parameterized and copy constructor
  • Member Functions: main(), showGUI(), addItem(), removeItem(), checkOutCart(), removeAll(), developerInfo()
  • Main Method

Sample Data:

#

Item

Price (Rs)

1

Handouts

500.0

2

Reference Book

500.0

3

DVD

500.0

4

USB

2500.0

 

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 BookShop 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.

Students kindly share assignment files in relevant subject timely for discussion/solution.or directly share with us " Click here"
QueryVU WhatsApp groups subject wise Join Now


Quote
admin
(@admin)
Admin
Joined: 2 years ago
Posts: 5384
Topic starter  

CS506 Web Design and Development Assignment 1 Solution & Discussion Fall 2020


CS506 Assignment 1 Solution:

 

1 Code:

class Product{

 

              private int id , quantity;

              private String name;

              private float price;

 

 

              public Product(){

                             id = 0;

                             quantity = 0;

                             name = "";

                             price = 0;

              }

 

              public Product(String name, int quantity, float price)

              {

                             this.name = name;

                             this.quantity= quantity;

                             this.price = price;

              }

              public Product(Product p)

              {

                             p.id= id;

                             p.quantity = quantity;

                             p.name = name;

                             p.price = price;

              }

              public int getId()

              {

                             return id;

              }

              public void setId(int id){

                             this.id= id;

              }

              public int getQuantity()

              {

                             return quantity;

              }

              public void setQuantity(int quantity){

                             this.quantity = quantity;

              }

              public String getName(){

                             return name;

              }

              public void setName(String name){

                             this.name = name;

              }

              public float getPrice(){

                             return price;

              }

              public void setPrice(float price){

                             this.price = price;

              }

}

 

 

2. Cart.Jav

import java.util.ArrayList;

import javax.swing.JOptionPane;

//import a class same

class Cart extends Product{

 

              ArrayList<Product> cartItems;

 

              public Cart(){

                             cartItems = new ArrayList<>();

              }

              public Cart(String name , int quantity , float price){

                             super(name, quantity , price);

              }

              public Cart(Product p){

                             super(p);

              }

              public void addItem(){

                             int op = 0;

                             String option = JOptionPane.showInputDialog(null, "Please Enter \n \n "

                                           + "1 to Add 'Handout(Rs 500.0)'\n "

                                           + "2 to Add 'Reference Book(Rs 500.0)'\n "

                                           + "3 to Add 'DVD(Rs 500.0)'\n "

                                           + "4 to Add 'USB(Rs 2500.0)'\n "

                                           + "5 to Add 'Done'\n "

                                           , "Add Item(s) in Cart"

                                           , JOptionPane.INFORMATION_MESSAGE);

 

                            if (option.equals("")){

                                           JOptionPane.showMessageDialog(null, "Please Select an Item" , "Error" , JOptionPane.ERROR_MESSAGE);

                                           addItem();

                             }

                             else

                             {

                                           op = Integer.parseInt(option);

                             }

                             int qty = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter \n\n "

                                           + "Please specify the quantity (1- 10) "

                                           , "Quantity"

                                           , JOptionPane.INFORMATION_MESSAGE));

 

                             if (op == 1) {

                                           setPrice(500.0f);

                                           setName("Handout");

 

                             }

                                           if (op == 2) {

                                           setPrice(500.0f);

                                           setName("Reference Book");

 

 

                             }

                                           if (op == 3) {

                                           setPrice(500.0f);

                                           setName("DVD");

 

                             }

                                           if (op == 4) {

                                           setPrice(2500.0f);

                                           setName("USB");

 

                             }

                             setQuantity(qty);

                             if (getQuantity() > 10 || getQuantity() < 1) {

                                           JOptionPane.showMessageDialog(null, "Quantity must be between 1- 10 ", "Error", JOptionPane.ERROR_MESSAGE);

                             }

                             else

                             {

                                           Product p = new Product(getName(), getQuantity(), getPrice());

                                           cartItems.add(p);

                                           JOptionPane.showMessageDialog(null, "Item Added to Cart");

                             }

 

              }

              public void removeItem(String n){

                             if (cartItems.isEmpty())

                             {

                              JOptionPane.showMessageDialog(null, "Cart is empty");             

                             }

                             else

                             {

                                           for (int i=0; i<cartItems.size(); i++ ) {

                                                          Product p = (Product)cartItems.get(i);

                                                          if (n.equals(p.getName())) {

                                                                        cartItems.remove(i);

                                                                        JOptionPane.showMessageDialog(null, "Item Removed");

                                                          }

                                           }

                             }

 

              }

                             public void emptyCart(){

                                           if (cartItems.isEmpty())

                                            {JOptionPane.showMessageDialog(null , "Cart is empty");}

                                                          else

                                           {

                                                          cartItems.clear();

                                                          JOptionPane.showMessageDialog(null, "All items removed successfuly!");

                                           }

                             }

                             public void Checkout(){

                                           String str = "";

                                           int items = 0 ;

                                           int iterator = 1;

                                           float itemPrice, total = 0.0f;

                                           int size = cartItems.size();

 

                                           if (size < 1) {

                                                          JOptionPane.showMessageDialog(null, "Add item(s) first ", "Cart is empty", JOptionPane.ERROR_MESSAGE);

                                           }

                                           else

                                           {

                                                          for (Product cartItems : cartItems ) {

                                                                        Product p = (Product) cartItems;

                                                                        itemPrice = p.getPrice() * p.getQuantity();

                                                                        str += iterator + " . "+ p.getName() + ": Rs"+ p.getPrice() + "x" + p.getQuantity()+

                                                                        "= Rs"+ itemPrice + " \n";

 

                                                                        items += p.getQuantity();

                                                                        total += p.getPrice() * p.getQuantity();

                                                                        iterator ++;

                                                          }

                                                          str += " \n \n No. of items : "+ items + "- Totla Bill: Rs "+ total;

                                                          JOptionPane.showMessageDialog(null, str, "Go To Checkout", JOptionPane.INFORMATION_MESSAGE);

                                                          //please subscribe my channel

                                           }

 

                             }

              }

 

3. BookShop. Java

import javax.swing.JOptionPane;

 

public class BookShop{

              static int ch = 0;

 

 public static void main(String Args[]) {

                             String s= "";

                             Cart myCart = new Cart();

 

                             while(true){

                                           switch(showGUI())

                                           {

                                                          case 1:

                                                          myCart.addItem();

                                                          break;

                                                          case 2:

                                                          String op= JOptionPane.showInputDialog(null, "Please Enter \n \n "

                                           + "1 to Remove 'Handout'\n "

                                           + "2 to Remove 'Reference Book'\n "

                                           + "3 to Remove 'DVD'\n "

                                           + "4 to Remove 'USB'\n "

             

                                           , "Remove an Item"

                                           , JOptionPane.INFORMATION_MESSAGE);

 

                                                          if (op.equals("1")) {s = "Handout"; }

                                                          if (op.equals("2")) {s = "Reference Book"; }

                                                          if (op.equals("3")) {s = "DVD"; }

                                                          if (op.equals("4")) {s = "USB"; }

                                                          myCart.removeItem(s);

                                                          break;

                                                          case 3:

                                                          myCart.Checkout();

                                                          break;

                                                          case 4:

                                                          myCart.emptyCart();

                                                          break;

                                                          case 5:

                                                          developerInfo();

                                                          System.exit(0);

                                           }

                             }

              }

              //add your id

              public static void developerInfo(){

//Enter Student ID

JOptionPane.showMessageDialog(null, "Develper By: Student Name(BCxxxxxx)", "Develper Info",

JOptionPane.INFORMATION_MESSAGE);

              };

              public static int showGUI(){

                             String option = JOptionPane.showInputDialog(null, "Please Enter \n \n "

                                           + "1 For 'Add Item to Cart'\n "

                                           + "2 For 'Remove an item from cart '\n "

                                           + "3 For  'Go To Checkout'\n "

                                           + "4 For 'Empty Cart'\n "

                                           + "5 For  'Exit Program'\n "

             

                                           , "BookShop Cart"

                                           , JOptionPane.INFORMATION_MESSAGE);

                             ch= Integer.parseInt(option);

                             return ch;

              }

}

//so run the program

//So run the program Best of luck

 

Download attachment for codes in MS Word.

Students kindly share assignment files in relevant subject timely for discussion/solution.or directly share with us " Click here"
QueryVU WhatsApp groups subject wise Join Now


ReplyQuote
Share: