Lab2 - Shopping Cart Demo: 1) Book - Java
Lab2 - Shopping Cart Demo: 1) Book - Java
1) Book.java
* Purpose: Book class has two attributes of its own: book_title, author and it implements displayItem()
method.
*/
/** Constructor */
public Book(String name, String model, float price, String book_title, String author) {
this.book_title = book_title;
this.author = author;
void displayItem() {
System.out.println(super.toString());
System.out.println("Book title: " + book_title);
2) GiftCard.java
* Purpose: Book class has one attribute of its own: giftcard_type and it implements displayItem()
method.
*/
/** Constructor */
this.giftcard_type = giftcard_type;
}
/** Implementing abstract method displayItem */
void displayItem() {
System.out.println(super.toString());
3) Item.java
/* Lab2 : UML
* 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 Item() {
item_id = counter++;
name = "Book";
model = "01HW999";
price = 150;
if(!(model.trim().isEmpty())) {
this.model = model;
if(!(name.trim().isEmpty())) {
this.name = name;
this.price = price;
return item_id;
}
/** access method to return the item model name */
return model;
return name;
return price;
4) ItemTester.java
/* Lab 2: UML
*/
import java.util.*;
System.out.println("SHOPPING CART\n");
Book book2 = new Book("Book", "01HW823", 30, "The God of Small Things", "Arundhati
Roy");
GiftCard card1 = new GiftCard("Gift Card", "01HW333", 45, "Sephora Gift Card");
GiftCard card2 = new GiftCard("Gift Card", "01H555", 64, "Starbucks Gift Card");
list.add(book2);
list.add(book3);
list.add(card1);
list.add(card2);
for(int i=0;i<list.size();i++)
list.get(i).displayItem();