I need help with this Java programming assignment: Duke Shirts sells Java t-shir
ID: 3908262 • Letter: I
Question
I need help with this Java programming assignment:
Duke Shirts sells Java t-shirts for $14.95 each, but discounts are possible for quantities as follows:
1 or 2 shirts, no discount and total shipping is $6.00
3-5 shirts, discount is 10% and total shipping is $8.00
6-10 shirts, discount is 20% and total shipping is $10.00
11 or more shirts, discount is 30% and shipping is free
Write a Java program that prompts the user for the number of shirts required. The program should then print the extended price of the shirts, the shipping charges, and the total cost of the order. Use currency format where appropriate.
Explanation / Answer
import java.text.DecimalFormat; //Import Decimal format
import java.util.Scanner; //Imporitng scanner class
public class JavaTshirts{
public static void main(String []args){
double E_price=0; //Initializing variables
double S_charges=0;
double T_cost=0;
double Temp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of shirts required:"); //Prompting user for input.
int num=sc.nextInt(); //Reading from user.
if(num==1|num==2){ //Case1
E_price=num*14.95;
S_charges=6;
T_cost=E_price+S_charges;
}
else if((num>=3)&(num<=5)){ //Case2
E_price=num*14.95;
Temp=0.1*E_price;
E_price=E_price-Temp;
S_charges=8;
T_cost=E_price+S_charges;
}
else if((num>=6)&(num<=10)){ //Case3
E_price=num*14.95;
Temp=0.2*E_price;
E_price=E_price-Temp;
S_charges=10;
T_cost=E_price+S_charges;
}
else if(num>=11){ //Case4
E_price=num*14.95;
Temp=0.3*E_price;
E_price=E_price-Temp;
S_charges=0;
T_cost=E_price+S_charges;
} //Printing the values.
DecimalFormat f=new DecimalFormat("##.00");//create an object for Decimalformat class ##.00 stands for round off to two decimal points.
System.out.println("Extended Price is:$"+f.format(E_price));
System.out.println("Shipping charges cost is:$"+f.format(S_charges));
System.out.println("Total cost is:$"+f.format(T_cost));
}
}