In: Computer Science
In Java
An outlet store is having a sale in their Cabin brand sweaters.
There are two different pricing systems depending on if it is a
Cabin brand or not. Tax must be added on after the sweater charge
is computed.
You must have two classes using separate files.
Requirements for Sweater Class
Fields
1. sweater price (in dollars)
2. Boolean to indicate if it is a Cabin brand or not
3. number of sweaters purchased
Methods
1. One 3 parameter constructor- the constructor uses
three parameters representing the sweater price, whether it is a
Cabin Brand or not, and the number of sweaters purchased.
2. Getter and setter for each field
3. getTotalPurchase method
This method must call the appropriate getter member methods where
necessary. Do not access the fields directly.
This method calculates and returns the total amount of the
sweater.
If the sweater is Cabin brand, calculate the discount as
follows;
-If the customer purchases 1 sweater the discount is 20% of the
sweaters price.
-if the customer purchases 2 or more sweaters the discount is
30%
-the customer cannot purchase less than 1 sweater.
-compute the purchase subtotal by subtracting the appropriate
discount from the sweaters price.
Use a tax rate of 7% of the purchase subtotal to compute the sales
tax in dollars. Add the sales tax amount to the purchase subtotal
to determine the total purchase amount.
Return the total purchase amount to the calling code.
Requirements for the SweaterDriver Class
Main method
1. customer must be prompted appropriately
2. All values related to money may include values after
the decimal point. All values displayed to the screen must display
with 2 places after the decimal.
3. The customer must indicate whether the sweater is
Cabin brand or not by typing a single character (y for yes, n for
no) program must accept Upper and lower case, Y,y,N,n.
4. If the sweater is Cabin brand, prompt the customer
to enter the number of Cabin sweaters being purchased.
5. Instantiate a Sweater object using a three parameter
constructor.
Note that the parameter that indicates if the sweater is a Cabin
brand is a Boolean data type.
The customer must type a single character. You will have to use
selection to instantiate a Sweater object with the correct data
type foe this parameter.
6. Display the values in the output by calling the
appropriate method of the Sweater object. The output must line up
at the decimal point as in the sample runs.
Sample runs
1
Enter the price of the sweater: $50.00
Is the swear a Cabin(Y/N)? N
Price of sweater $50.00
Total purchase $53.50
Run 2
Enter the price of the sweater: $60.00
Is the sweater a Cabin(Y/N)? Y
Enter the number of sweaters being purchased: 2
Price of sweater $60.00
Total Purchase $89.88
Run 3
Enter price of sweater: $40.00
Is the sweater a Cabin(Y/N)? Y
Enter the number of sweaters being purchased: 1
Price of sweater $40.00
Total purchase $34.24
Have a look at the below code. I have ran the code for the sample inputs, you can check the output also in the attached code snippet.
import java.util.*;
class Sweater{
private double price;
private boolean isCabin;
private int noOfSweater;
double totalPrice;
public Sweater(double p, boolean flag, int n){
this.price = p;
this.isCabin = flag;
this.noOfSweater = n;
}
public void setPrice(int p){
this.price = p;
}
public void setFlag(boolean flag){
this.isCabin = flag;
}
public void setNumber(int n){
this.noOfSweater = n;
}
public double getPrice(){
return this.price;
}
public boolean getFlag(){
return this.isCabin;
}
public int getNumber(){
return this.noOfSweater;
}
public double getTotalPurchase(){
if (this.isCabin){
if (this.noOfSweater>=2){
double discount = this.price*0.3;
totalPrice+=this.noOfSweater*(this.price - discount);
}
else{
double discount = this.price*0.2;
totalPrice+=this.noOfSweater*(this.price - discount);
}
double tax = totalPrice*0.07;
totalPrice+=tax;
}
else{
totalPrice+=this.noOfSweater*this.price;
double tax = totalPrice*0.07;
totalPrice+=tax;
}
return this.totalPrice;
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the price of the sweater:");
double price = sc.nextDouble();
System.out.println("Is the swear a Cabin(Y/N)?:");
char response = sc.next().charAt(0);
boolean flag;
int number = 1;
if (response=='Y'){
flag = true;
}
else{
flag = false;
}
if (flag){
System.out.println("Enter the number of sweaters being purchased: ");
number = sc.nextInt();
Sweater sw = new Sweater(price,flag,number);
System.out.println("Price of sweater $"+sw.getPrice());
System.out.println("Total Purchase: $" + sw.getTotalPurchase());
}
else{
Sweater sw = new Sweater(price,flag,number);
System.out.println("Price of sweater $"+sw.getPrice());
System.out.println("Total Purchase: $" + sw.getTotalPurchase());
}
}
}
// Below is the output...
//Enter the price of the sweater:60
//Is the swear a Cabin(Y/N)?:Y
//Enter the number of sweaters being purchased: 2
//Price of sweater $60.0
//Total Purchase: $89.88
Happy Learning!