In: Computer Science
In other words, the price for a king-sized cashmere blanket is $115. Whenever the size or material is invalid, reset the blanket to the default values. Include a toString() method that returns a description of the blanket.
Do not allow the number of settings to be fewer than one or more than five; if it is, use the default setting of 1. Add a $5.75 premium to the price if the blanket has the automatic shutoff feature. Also include a toString() method that calls the parent class toString() method and combines the returned value with data about the new fields to return a complete description of features.
class Blanket
{
private String size,color,material;
private double price;
public Blanket()
{
size = "Twin";
color = "white";
material = "cotton";
price = 30.0;
}
public void setSize(String size)
{
this.size = size;
if(size == "Twin")
this.price = this.price + 10;
else if(size == "Queen")
this.price = this.price +25;
else if(size == "King")
this.price = this.price +40;
}
public void setColor(String color)
{
this.color = color;
}
public void setMaterial(String material)
{
this.material = material;
if(material == "wool")
this.price = this.price + 20;
else if(material ==
"cashmere")
this.price = this.price +45;
else if(size == "King")
this.price = this.price +40;
}
public double getPrice()
{
return price;
}
public String toString()
{
return "Blanket size : "+size +" color : "+color +"
material : "+material + " Price : "+getPrice();
}
}
class ElectricBlanket extends Blanket
{
private int numHeatSettings;
private boolean automaticShutOff;
public ElectricBlanket()
{
super();
numHeatSettings = 1;
automaticShutOff = false;
}
public void setNumHeatSettings(int
numHeatSettings)
{
if(numHeatSettings < 5)
this.numHeatSettings = 1;
else
this.numHeatSettings =
numHeatSettings;
}
public int getNumHeatSettings()
{
return numHeatSettings;
}
public void setAutomaticShutOff(boolean
automaticShutOff)
{
this.automaticShutOff =
automaticShutOff;
}
public boolean getAutomaticShutOff()
{
return automaticShutOff;
}
public String toString()
{
return super.toString() +" Number of Heat
Settings : "+numHeatSettings+" Has automatic shut off : "+
automaticShutOff;
}
public double getPrice()
{
if(automaticShutOff == true)
return (super.getPrice() + 5.75);
else
return super.getPrice();
}
}
class TestBlanket
{
public static void main (String[] args)
{
ElectricBlanket eb = new
ElectricBlanket();
eb.setSize("King");
eb.setColor("green");
eb.setMaterial("Kashmere");
eb.setAutomaticShutOff(true);
eb.setNumHeatSettings(6);
System.out.println(eb);
System.out.println("Price :
"+eb.getPrice());
}
}
Output:
Blanket size : King color : green material : Kashmere Price : 115.75 Number of Heat Settings : 6 Has automatic shut off : true Price : 115.75
Do ask if any doubt.Please upvote.