Program Description
A local company has requested a program to calculate the cost of different hot tubs that it sells. Use the following instructions to code the program:
File 1 - HotTubLastname.java
The capacity of the hot tub can be calculated by using the following formula:
Capacity in gallons = (Length * Width * Depth) / 1728 * 4.8
Hot Tub Capacity |
Package |
Cost Per Gallon |
Less than 350 gallons |
Basic |
$5.00 |
Less than 350 gallons |
Premium |
$6.50 |
350 but not more than 500 gallons |
Basic |
$6.00 |
350 but not more than 500 gallons |
Premium |
$8.00 |
Over 500 gallons |
Basic |
$7.50 |
Over 500 gallons |
Premium |
$10.00 |
Price = Cost Per Gallon * Capacity
File 2 - DemoLastname.java
Sample Input and Output (formatting and spacing should be exactly as shown below)
What is the model of the hot tub? HT Intro Model 4
Person
Is the hot tub package Basic or Premium?
Basic
What is the length (in inches) of the hot tub?
60
What is the width (in inches) of the hot tub?
65
What is the depth (in inches) of the hot tub?
30
Do you wish to enter information for another hot tub (Y/N)? Y
What is the model of the hot tub? HT Relaxation 6
Person
Is the hot tub package Basic or Premium?
Premium
What is the length (in inches) of the hot tub?
75
What is the width (in inches) of the hot tub?
75
What is the depth (in inches) of the hot tub?
32
Do you wish to enter information for another hot tub (Y/N)? y
What is the model of the hot tub? HT
Ultimate 8 person
Is the hot tub package Basic or Premium?
Premium
What is the length (in inches) of the hot tub?
100
What is the width (in inches) of the hot tub?
100
What is the depth (in inches) of the hot tub?
36
Do you wish to enter information for another hot tub (Y/N)? N
Model: HT Intro Model 4 Person
Package: Basic
Length: 60 inches
Width: 65 inches
Depth: 30 inches
Capacity: 325.00 gallons
Price: $1,625.00
Model: HT Relaxation 6 Person
Package: Premium
Length: 75 inches
Width: 75 inches
Depth: 32 inches
Capacity: 500.00 gallons
Price: $4,000.00
Model: HT Ultimate 8 person
Package: Premium
Length: 100 inches
Width: 100 inches
Depth: 36 inches
Capacity: 1000.00 gallons
Price: $10,000.00
The total cost of all the hot tubs is: $15,625.00
Hi,
Please find below code as per yout requirement.
Let me know if you have any doubt/concerns in this answer via comments.
Hope this answer helps you.
Thanks
/********************JAVA CODE**********************/
/***************HotTubLastname.java*******************/
public class HotTubLastname {
//Instance variable
private String model;
private String tubPackage;
private int length;
private int width;
private int depth;
/**
* This is default constructor used to construct object
by default values
*/
public HotTubLastname() {
}
/**
* This is parameterized constructor used to construct
object by given parameters
* @param model
* @param tubPackage
* @param length
* @param width
* @param depth
*/
public HotTubLastname(String model, String tubPackage,
int length, int width, int depth) {
this.model = model;
this.tubPackage = tubPackage;
this.length = length;
this.width = width;
this.depth = depth;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the tubPackage
*/
public String getTubPackage() {
return tubPackage;
}
/**
* @param tubPackage the tubPackage to set
*/
public void setTubPackage(String tubPackage) {
this.tubPackage = tubPackage;
}
/**
* @return the length
*/
public int getLength() {
return length;
}
/**
* @param lenght the length to set
*/
public void setLength(int length) {
this.length = length;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the depth
*/
public int getDepth() {
return depth;
}
/**
* @param depth the depth to set
*/
public void setDepth(int depth) {
this.depth = depth;
}
/**
* This method returns capacity of the tub using
length,width and depth
* @return
*/
public double getCapacity() {
return (Double.valueOf(length) *
Double.valueOf(width) * Double.valueOf(depth)) / 1728 * 4.8;
}
/**
* This method returns price of the tub on the basis of
type and capacity
* @return
*/
public double getPrice() {
double price = 0;
double capacity =
getCapacity();
String basic = "Basic";
String premium = "Premium";
if(capacity < 350) {
if(getTubPackage().equalsIgnoreCase(basic)) {
price = capacity * 5.00;
} else if
(getTubPackage().equalsIgnoreCase(premium)) {
price = capacity * 6.50;
}
} else if(capacity >=350
&& capacity <500) {
if(getTubPackage().equalsIgnoreCase(basic)) {
price = capacity * 6.00;
} else if
(getTubPackage().equalsIgnoreCase(premium)) {
price = capacity * 8.00;
}
} else if(capacity >=500)
{
if(getTubPackage().equalsIgnoreCase(basic)) {
price = capacity * 7.50;
} else if
(getTubPackage().equalsIgnoreCase(premium)) {
price = capacity * 10.00;
}
}
return price;
}
}
/***************DemoLastname.java*******************/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class DemoLastname {
public static void main(String[] args) {
//Scanner is used to ask user
input from console
Scanner sc = new
Scanner(System.in);
//Creating ArrayList of Hot tub
to store user entered data for end result
ArrayList<HotTubLastname>
listOfTub = new ArrayList<HotTubLastname>();
//Decimal format is used to
format amount
DecimalFormat df = new
DecimalFormat("#.00");
//NumberFormat is used to format
currency in dollar
NumberFormat currencyFormatter
=
NumberFormat.getCurrencyInstance(Locale.US);
//Temporary variables to store
data for validation and constructing object
String packageType = null;
String modelName = null;
int length = 0;
int width = 0;
int depth = 0;
double totalCost = 0;
String basic = "Basic";
String premium = "Premium";
while(true) {
//Getting model
from user
System.out.print("What is the model of the hot tub? ");
modelName =
sc.nextLine();
//Checking
package of Tub if invalid input then continue asking details
otherwise breaking from loop
do {
System.out.print("Is the hot tub package Basic
or Premium? ");
packageType = sc.nextLine();
if(!(packageType.equalsIgnoreCase(basic) ||
packageType.equalsIgnoreCase(premium))) {
System.out.println("Invalid
Package, can be Basic or Premium");
continue;
} else {
break;
}
}
while(true);
//Checking
length of Tub if invalid input then continue asking details
otherwise breaking from loop
do {
System.out.print("What is the length (in inches)
of the hot tub? ");
length = Integer.parseInt(sc.nextLine());
if(length < 60) {
System.out.println("Invalid
length,Please enter 60 and above.");
} else {
break;
}
}
while(true);
//Checking
width of Tub if invalid input then continue asking details
otherwise breaking from loop
do {
System.out.print("What is the width (in inches)
of the hot tub? ");
width = Integer.parseInt(sc.nextLine());
if(width < 60) {
System.out.println("Invalid
width,Please enter 60 and above.");
} else {
break;
}
}
while(true);
//Checking
depth of Tub if invalid input then continue asking details
otherwise breaking from loop
do {
System.out.print("What is the depth (in inches)
of the hot tub? ");
depth = Integer.parseInt(sc.nextLine());
if(depth < 30) {
System.out.println("Invalid
depth,Please enter 30 and above.");
} else {
break;
}
}
while(true);
//Creating
object of Hot tub by above given input
HotTubLastname
hotTubLastname = new HotTubLastname(modelName, packageType, length,
width, depth);
//Adding object
of hot tub to print details later
listOfTub.add(hotTubLastname);
//Asking if
user wants to continue adding tub or not, If Y then continue
otherwise exit from while loop
System.out.print("Do you wish to enter information for another hot
tub (Y/N)? ");
String decision
= sc.nextLine();
if(decision.equalsIgnoreCase("Y")) {
continue;
} else if
(decision.equalsIgnoreCase("N")) {
break;
} else {
System.out.println("Invalid input");
break;
}
}
//Printing individual hot tub
details in required format
for (HotTubLastname hotTubLastname
: listOfTub) {
double price =
hotTubLastname.getPrice();
totalCost+=
price;
String result =
"Model: " + hotTubLastname.getModel() + "\n Package: " +
hotTubLastname.getTubPackage() + "\n Length:" +
hotTubLastname.getLength() + " inches \n Width: "
+ hotTubLastname.getWidth() +
"inches \n Depth: " + hotTubLastname.getDepth() + " inches \n
Capacity: "+df.format(hotTubLastname.getCapacity()) + " gallons \n
Price: "+currencyFormatter.format(price);
System.out.println(result);
}
//Printing total cost of the hot
tubs
System.out.println("The total cost
of all the hot tubs is: "+currencyFormatter.format(totalCost));
}
}
Get Answers For Free
Most questions answered within 1 hours.