In: Computer Science
A clothing store sells shoes, pants, and tops. The store also allows a customer to buy an “outfit,” which consists of three items: one pair of shoes, one pair of pants, and one top. Each clothing item has a description and a price. The four type of clothing items are represented by the four classes Shoes, Pants, Top, and Outfit. All four classes implement the following ClothingItem interface.
public interface ClothingItem
{
/** Return the description of the clothing item
*//
String getDescription();
/** Return the price of the clothing item */
double getPrice();
}
The following diagram shows the relationship between the ClothingItem interface and the Shoes, Pants, Top, and Outfit classes.
The store allows customers to create Outfit clothing items, each of which includes a pair of shoes, pants, and a top. The description of the outfit consists of the description of the shoes, pants, and top, in the order, separated by “/” and followed by a space and “outfit”. The price of an outfit is calculated as follows. If the sum of the prices of any two items equals or exceeds $100, there is a 25% discount on the sum of the prices of all three items. Otherwise, there is a 10% discount.
For example, an outfit consisting of sneakers ($40), blue jeans ($50), and a T-shit ($10), would have the name “sneakers/blue jeans/T-shirt outfit” and a price of 0.90(40 + 50 +10) = $90.00. An outfit consisting of loafers ($50), cutoffs ($20), and dress-shirt ($60), would have the description “loafers/cutoffs/dress-shirt outfit” and a price of 0.75(50 + 20 + 60) = $97.50
Write the Outfit class the implements the ClothingItem interface. Your implementation must include a constructor that takes three parameters representing a pair of shoes, pants, and a top, in that order.
A client class that uses the Outfit class should be able to create an outfit, and its description, and get its price. Your implementation should be such that the client code has the following behavior:
Shoes shoes;
Pants pants;
Top top;
/* Code to initialize shoes, pants, and top */
ClothingItem outfit =
new Outfit(shoes, pants, top); //Compiles without error
ClothingItem outfit =
new Outfit(pants, shoes, top); //Compile-time error
ClothingItem outdit =
new Outfit(shoes, top, pants); //Compile-time error
Hi,
Please find below code as per your requirement.
I have added ClothingItemDriver.java class to test the functionality.
Let me know if you have any doubt/concerns on this answer via comments.
Hope this answer helps you.
Thanks.
/********************JAVA CODE*********************/
/******************ClothingItem.java*********************/
public interface ClothingItem {
/** Return the description of the clothing item */
String getDescription();
/** Return the price of the clothing item */
double getPrice();
}
/*******************Shoes.java********************/
public class Shoes implements ClothingItem {
//instance variables
private String description;
private double price;
/**
* Parameterized constructor which takes 2 parameter to
construct shoe object
* @param description
* @param price
*/
public Shoes(String description, double price) {
this.description =
description;
this.price = price;
}
/**
* Overrides method from interface ClothingItem to
return description of Shoes
*/
@Override
public String getDescription() {
return this.description;
}
/**
* Overrides method from interface ClothingItem to
return price of Shoes
*/
@Override
public double getPrice() {
return this.price;
}
}
/*******************Pants.java********************/
public class Pants implements ClothingItem {
//instance variables
private String description;
private double price;
/**
* Parameterized constructor which takes 2 parameter to
construct pants object
* @param description
* @param price
*/
public Pants(String description, double price) {
this.description =
description;
this.price = price;
}
/**
* Overrides method from interface ClothingItem to
return description of Pants
*/
@Override
public String getDescription() {
return this.description;
}
/**
* Overrides method from interface ClothingItem to
return price of Pants
*/
@Override
public double getPrice() {
return this.price;
}
}
/********************Top.java*******************/
public class Top implements ClothingItem {
//instance variables
private String description;
private double price;
/**
* Parameterized constructor which takes 2 parameter to
construct top object
* @param description
* @param price
*/
public Top(String description, double price) {
this.description =
description;
this.price = price;
}
/**
* Overrides method from interface ClothingItem to
return description of Top
*/
@Override
public String getDescription() {
return description;
}
/**
* Overrides method from interface ClothingItem to
return price of Top
*/
@Override
public double getPrice() {
return this.price;
}
}
/********************Outfit.java*******************/
public class Outfit implements ClothingItem {
//instance variables
private Shoes shoes;
private Pants pants;
private Top top;
/**
* Parameterized constructor which takes 3 parameter to
construct Outfit object
* @param shoes
* @param pants
* @param top
*/
public Outfit(Shoes shoes, Pants pants, Top top)
{
this.shoes = shoes;
this.pants = pants;
this.top = top;
}
/**
* Overrides method from interface ClothingItem to
return description of Shoes,Pants and Top
*/
@Override
public String getDescription() {
return shoes.getDescription() +
"/"+pants.getDescription()+"/"+top.getDescription()+"
outfit";
}
/**
* Overrides method from interface ClothingItem to
return price of all outfits with discount
*/
@Override
public double getPrice() {
//calculating prices of 2 product
to identify discount
double price1 = shoes.getPrice() +
pants.getPrice();
double price2 =shoes.getPrice() +
top.getPrice();
double price3 = pants.getPrice() +
top.getPrice();
double totalPrice =
shoes.getPrice() + pants.getPrice() + top.getPrice();
//if two item prices exceeds or
equals to 100 then 25% discount otherwise 10%
if(price1 >= 100 || price2 >=
100 || price3 >= 100) {
totalPrice =
0.75 * (totalPrice);
} else {
totalPrice =
0.90 * totalPrice;
}
return totalPrice;
}
}
/*******************ClothingItemDriver.java********************/
/**
* This is driver class to test Outfit class
*
*/
public class ClothingItemDriver {
public static void main(String[] args) {
//Creating outfits which not exceed
$100 for 2 outfits
Shoes shoes = new Shoes("sneakers",
40);
Pants pants = new Pants("blue
jeans", 50);
Top top = new Top("T-shirt",
10);
ClothingItem outfit = new
Outfit(shoes, pants, top);
System.out.println(outfit.getDescription());
//This will print price with 10%
discount
System.out.println("Total Price :
$"+outfit.getPrice());
//uncomment this you will get
compile time error
//ClothingItem outfit1 = new
Outfit(pants, shoes, top);
//ClothingItem outfit2 = new
Outfit(top, pants, shoes);
//Creating outfits which exceed
$100 for 2 outfits
Shoes shoes1 = new Shoes("loafers",
50);
Pants pants1 = new Pants("cutoffs",
20);
Top top1 = new Top("dress-shirt",
60);
ClothingItem outfit1 = new
Outfit(shoes1, pants1, top1);
System.out.println(outfit1.getDescription());
//This will print price with 25%
discount
System.out.println("Total Price :
$"+outfit1.getPrice());
}
}