Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
122 views

Lab2 - Shopping Cart Demo: 1) Book - Java

The document describes a shopping cart demo with Java classes for items (Item.java), books (Book.java), and gift cards (GiftCard.java) that extend the base item class. ItemTester.java creates objects of these classes, adds them to a list, calls the displayItem() method to output details, and calculates total price. The classes implement abstract methods and constructors to initialize attributes and output item details consistently.

Uploaded by

Kavisha Solanki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Lab2 - Shopping Cart Demo: 1) Book - Java

The document describes a shopping cart demo with Java classes for items (Item.java), books (Book.java), and gift cards (GiftCard.java) that extend the base item class. ItemTester.java creates objects of these classes, adds them to a list, calls the displayItem() method to output details, and calculates total price. The classes implement abstract methods and constructors to initialize attributes and output item details consistently.

Uploaded by

Kavisha Solanki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab2 – Shopping Cart Demo

1) Book.java

/** Book.java extends Item

* derived class from Item

* @version Sept 24, 2020

* @author Kavisha Solanki

* Purpose: Book class has two attributes of its own: book_title, author and it implements displayItem()
method.

*/

public class Book extends Item{

/** Declaring attributes of this sub-class: Book Title and Author */

public String book_title;

public String author;

/** Constructor */

public Book(String name, String model, float price, String book_title, String author) {

super(name, model, price); //calls the parent class constructor

this.book_title = book_title;

this.author = author;

/** Implementing abstract method displayItem */

/** Prints all the attributes */

void displayItem() {

System.out.println("Item Id: " + getItemId());

System.out.println(super.toString());
System.out.println("Book title: " + book_title);

System.out.println("Book author: " + author);

System.out.println("Book model: " + getModel());

System.out.println("Book price: $" + getPrice() + "\n");

2) GiftCard.java

/** GiftCard.java extends Item

* derived class from Item

* @version Sept 24, 2020

* @author Kavisha Solanki

* Purpose: Book class has one attribute of its own: giftcard_type and it implements displayItem()
method.

*/

public class GiftCard extends Item{

/** Declaring attribute of this sub-class: type of gift card */

public String giftcard_type;

/** Constructor */

public GiftCard(String name, String model, float price, String giftcard_type) {

super(name, model, price); //calls the parent class constructor

this.giftcard_type = giftcard_type;

}
/** Implementing abstract method displayItem */

/** Prints all the attributes */

void displayItem() {

System.out.println("Item Id: " + getItemId());

System.out.println(super.toString());

System.out.println("Type of gift card: " + giftcard_type);

System.out.println("Gift Card model: " + getModel());

System.out.println("Gift Card price: $" + getPrice() + "\n");

3) Item.java

/* Lab2 : UML

* Base Class - Abstract class to create Items

* @author Kavisha Solanki

* @version Sept 24, 2020

* Purpose: an Item maintains an unique item id, item-name (not null), model name and price with
getters returning the values and an abstract method displayItem()

*/

public abstract class Item {

private static int counter = 1; /** maintain the current counter */

private int item_id; /** Unique id */

protected String name; /** non-blank value */

protected String model; /** non-blank value */

protected float price; /** Price of an item */


/* Constructor */

public Item() {

item_id = counter++;

name = "Book";

model = "01HW999";

price = 150;

/** Overloaded Constructor */

public Item(String name, String model, float price) {

this(); // will invoke the default constructor

if(!(model.trim().isEmpty())) {

this.model = model;

if(!(name.trim().isEmpty())) {

this.name = name;

if(price != 0.0 && price > 0.0) {

this.price = price;

/** access method to return item id */

public int getItemId() {

return item_id;

}
/** access method to return the item model name */

public String getModel() {

return model;

/** access method to return the name of item */

public String getName() {

return name;

/** access method to return the price of item */

public float getPrice() {

return price;

/** method to return Item name */

public String toString() {

return "Item Name: " + name;

/** Creating an abstract method "displayItem" */

abstract void displayItem();

4) ItemTester.java

/* Lab 2: UML

* @version Sept 24, 2020

* @author Kavisha Solanki


*

* Purpose: Creating objects of super class(Item.java) and sub-classes(Book.java and GiftCard.java),


calling necessary method and printing the output on console.

*/

import java.util.*;

public class ItemTester {

public static void main(String args[]) {

System.out.println("SHOPPING CART\n");

/** Creating an object of class Book */

Book book1 = new Book("Book", "01HW854", 60, "Hinduism", "S. Kulkarni");

Book book2 = new Book("Book", "01HW823", 30, "The God of Small Things", "Arundhati
Roy");

Item book3 = new Book("Book", "01HW852", 70, "Inferno", "Dan Brown");

/** Creating an object of class GiftCard */

GiftCard card1 = new GiftCard("Gift Card", "01HW333", 45, "Sephora Gift Card");

GiftCard card2 = new GiftCard("Gift Card", "01H555", 64, "Starbucks Gift Card");

/** Using ArrayList to call displayItem() method for all objects */

ArrayList<Item> list=new ArrayList<Item>();

list.add(book1); //Adding object in arraylist

list.add(book2);

list.add(book3);
list.add(card1);

list.add(card2);

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

list.get(i).displayItem();

/** Summing prices of all items in cart */

float totalPrice = book1.getPrice() + book2.getPrice() + book3.getPrice() +


card1.getPrice() + card2.getPrice();

/** Printing total number of items in cart */

System.out.println("\nTotal number of items in shopping cart is: " + card2.getItemId());

/** Printing total price of items in shopping cart. */

System.out.println("\nTotal price of items in shopping cart is: $" + totalPrice);

You might also like