[go: up one dir, main page]

0% found this document useful (0 votes)
91 views11 pages

Adv Java Skill 5

The code implements an interface and abstract class to model different goods (books, CDs, cosmetics) that can be added to a shopping cart. The interface defines common methods for getting tax and price. An abstract class implements this interface and includes tax calculation logic. Concrete classes extend this abstract class and indicate tax/import settings specific to each good. A main class demonstrates creating items and printing totals.

Uploaded by

Reddy Owner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views11 pages

Adv Java Skill 5

The code implements an interface and abstract class to model different goods (books, CDs, cosmetics) that can be added to a shopping cart. The interface defines common methods for getting tax and price. An abstract class implements this interface and includes tax calculation logic. Concrete classes extend this abstract class and indicate tax/import settings specific to each good. A main class demonstrates creating items and printing totals.

Uploaded by

Reddy Owner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SKILL-5

NAME:T.DURGA SAI KIRAN


ID NAME:Donthireddy
NO: 2000031877 Chakri
Sampath Hanuman Reddy
SECID: NO : 2000030257
S-10(B)
SEC : S-6

QUESTION :
A company named XYZ Retail is in the business of selling Books, CDs and Cosmetics. Books are sales
tax exempt, CDs and Cosmetics have a sales tax of 10%. CDs can be imported and attracts an import
tax of 5%. Write a simple shopping basket program, which will calculate extended price (qty *
(unitprice + tax)) inclusive of tax for each item in the basket?

CODE :

STEP-1: The “Item” interface that defines the methods to be implemented by


the “Goods” class.

package com.patterns.gof;

public interface Item {

//abstract by default

double getExtendedTax();

double getExtendedTaxedPrice();

void setImported(boolean b);

String getDescription();

STEP-2: The “Goods” class is abstract as it implements the common behavior


of Book, CD, and Cosmetics, and implements the interface “Item”. It
composes (i.e composition) “TaxCalculator” to perform the tax calculation.
package com.patterns.gof;

import java.math.BigDecimal;

//must be abstract when at least a single method is abstract

public abstract class Goods implements Item {

// define attributes

private String description;

private int qty;

private BigDecimal price;

private TaxCalculator tax = new TaxCalculatorImpl(); // composition

// can be set at runtime via a setter


method

// different implementations can be


assigned to the interface

public Goods(String description, int qty, BigDecimal price) {

this.description = description;

this.qty = qty;

this.price = price;

protected abstract boolean isTaxed(); // abstract method

protected abstract boolean isImported(); // abstract method

public double getExtendedTax() {

BigDecimal calculatedTax = tax.getCalculatedTax(isTaxed(),


isImported(), price);

BigDecimal extTax = calculatedTax.multiply(BigDecimal.valueOf(qty));

return extTax.doubleValue();

}
public double getExtendedTaxedPrice() {

if (tax == null) {

throw new IllegalArgumentException("Tax should be calculated first:");

BigDecimal calculatedTax = tax.getCalculatedTax(isTaxed(),


isImported(), price);

BigDecimal extPrice =
BigDecimal.valueOf(qty).multiply(calculatedTax.add(price));

return extPrice.doubleValue();

// getters and setters go here for attributes like description etc

public String getDescription() {

return description;

public String toString() {

return qty + " " + description + " : ";

STEP-3: The TaxCalculator interface & the implementation TaxCalculatorImpl.

package com.patterns.gof;
import java.math.BigDecimal;
public interface TaxCalculator {
abstract BigDecimal getCalculatedTax(boolean isTaxable, boolean
isImported, BigDecimal price);
}
package com.patterns.gof;
import java.math.BigDecimal;
public class TaxCalculatorImpl implements TaxCalculator {
// Stay away from hard coding values. Read from a “.properties”
file
public static final BigDecimal SALES_TAX =
BigDecimal.valueOf(0.10); // 10%
public static final BigDecimal IMPORT_TAX =
BigDecimal.valueOf(0.05); // 5%
public BigDecimal getCalculatedTax(boolean isTaxable, boolean
isImported, BigDecimal price) {
if(price == null){
throw new IllegalArgumentException("Price cannot be null");
}
BigDecimal salesTax = BigDecimal.ZERO;
BigDecimal importTax = BigDecimal.ZER0
if (isTaxable) {
salesTax = price.multiply(SALES_TAX);
}
if (isImported) {
importTax = price.multiply(IMPORT_TAX);
}
return salesTax.add(importTax);
}
}

STEP-4: Now, the specific Goods like Book, CD, and Cosmetics that
extends the shared behavior. In other words, inherits the shared
behavior of Goods. The isTaxable & isImportedare specific to the
product. New products can be added in the future with specific
taxable & importable behaviors.
package com.patterns.gof;
import java.math.BigDecimal;
public class Book extends Goods {
//default values
private boolean isTaxed = false;
private boolean isImported = false;
public Book(String description, int qty, BigDecimal price) {
super(description, qty, price);

public boolean isTaxed() {


return isTaxed;
}
public boolean isImported() {
return isImported;
}
//default can be overridden
public void setImported(boolean b) {
throw new UnsupportedOperationException("Books Cannot be
imported");
}
}
package com.patterns.gof;
import java.math.BigDecimal;
public class CD extends Goods {
//default values
private boolean isTaxed = true;
private boolean isImported = false;
public CD(String description, int qty, BigDecimal price) {
super(description, qty, price);
}
public boolean isTaxed() {
return isTaxed;
}
public boolean isImported() {
return isImported;
}
//default can be overridden
public void setImported(boolean b) {
isImported = b;
}
}
package com.patterns.gof;
import java.math.BigDecimal;
public class Cosmetics extends Goods {
//defaults
private boolean isTaxed = true;
private boolean isImported = false;
public Cosmetics(String description, int qty, BigDecimal price) {
super(description, qty, price);
}
public boolean isTaxed() {
return isTaxed;
}
public boolean isImported() {
return isImported;
}
//default can be overridden
public void setImported(boolean b) {
throw new UnsupportedOperationException("Cosmetics Cannot
be imported");
}
}
STEP-5: Finally, a simple main class to run the code.
package com.patterns.gof;
import java.math.BigDecimal;
public class SimpleShoppingCart {
public static void main(String[] args) {
Book book1 = new Book("Java Basics", 2,
BigDecimal.valueOf(39.95));
//use log4j instead of println
System.out.println(book1.getExtendedTax());
System.out.println(book1.getExtendedTaxedPrice());
CD cd1 = new CD("Java in 30 Days", 2,
BigDecimal.valueOf(59.00));
System.out.println(cd1.getExtendedTax());
System.out.println(cd1.getExtendedTaxedPrice());
Cosmetics cosmetics1 = new Cosmetics("Facials", 2,
BigDecimal.valueOf(20.00));
System.out.println(cosmetics1.getExtendedTax());
System.out.println(cosmetics1.getExtendedTaxedPrice());
CD cd2 = new CD("Hadoop in 30 Days", 2,
BigDecimal.valueOf(10.00));
cd2.setImported(true);
System.out.println(cd2.getExtendedTax());
System.out.println(cd2.getExtendedTaxedPrice());

}
}

OUTPUT:

VIVA:
1) What are interfaces in java and how they are implemented in
java?

A) An interface is just like Java Class, but it only has static


constants and abstract method. Java uses Interface to
implement multiple inheritance. A Java class can implement
multiple Java Interfaces. All methods in an interface are
implicitly public and abstract.

2) Difference between multiple interfaces and multiple inheritance?


A)Interface is notihg but it act as a intermediator between two
objects or two programs(program or object may be different). In
interface both can use. But multiple interface is a process of
obtaining or reciving the properties of more than one class. In
multiple inheritance only informations are used by derived class.

3) What are static and default methods implemented for interfaces in


java?
A) Static Methods in Interface are those methods, which are defined
in the interface with the keyword static. Similar to Default Method in
Interface, the static method in an interface can be defined in the
interface, but cannot be overridden in Implementation Classes.

4) Name the keyword used to implement interfaces in java?


A)Implements: In Java, the implements keyword is used to
implement an interface.

You might also like