2 ms·
This can be done in Java just fine: public interface PriceAble { int getPrice(); } class Book implements PriceAble { int royalty = 2; int markup
by teh64 11y ago
This can be done in Java just fine:
public interface PriceAble
{
int getPrice();
}
class Book implements PriceAble
{
int royalty = 2;
int markup = 8;
@Override
public int getPrice()
{
return royalty + markup;
}
}
class Movie implements PriceAble
{
int price = 15;
@Override
public int getPrice()
{
return price;
}
}
class Cart
{
int cartTotal = 0;
void increaseTotal(PriceAble priceAble)
{
cartTotal += priceAble.getPrice();
}
}
public class forExample
{
public static void main(String[] args)
{
PriceAble movie = new Movie();
PriceAble book = new Book();
Cart cart = new Cart();
System.out.println(cart.cartTotal); //prints 0
cart.increaseTotal(movie);
System.out.println(cart.cartTotal); //prints 15
cart.increaseTotal(book);
System.out.println(cart.cartTotal); //prints 25
}
}