A cloth showroom has announced the following seasonal discounts on purchase of items: Purchase Discount amount Mill cloth Handloom items 0 - 100 - 5% 101 - 200 5% 7.5% 201 - 300 7.5% 10.0% Above 300 10.0% 15.0% Write a program using switch and if statements to compute the net amount to be paid by customer.
Answers
import java.util.*;
public class CalculateNetAmount
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.print("Enter the item type(M/H)");
char c=scan.nextLine().toUpperCase().toCharArray()[0];
System.out.print("Enter the cost:");
double cost=scan.nextDouble();
double dis=discount(c,cost);
double netAmt=cost-dis;
System.out.println("The net paid amount="+netAmt);
}
public static double discount(char typ, double cost) {
double dis = 0;
double rate = 0;
switch (typ) {
case 'M':
if (cost > 100 && cost <= 200) {
rate = 5;
} else if (cost > 200 && cost <= 300) {
rate = 7.5;
} else if (cost > 300) {
rate = 10;
}
break;
case 'H':
if (cost > 0 && cost <= 100) {
rate = 5;
} else if (cost > 100 && cost <= 200) {
rate = 7.5;
} else if (cost > 200 && cost <= 300) {
rate = 10;
} else if (cost > 300) {
rate = 15;
}
break;
}
dis = cost * rate / 100;
return dis;
}
}
Answer:
import java.util.Scanner;
public class clothShowroom {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter puchase amount: ");
int cost = sc.nextInt();
double rate = 0;
double dis = 0;
System.out.println("Enter item type, Cloth(type 0) or Handloom(type 1): ");
int c = sc.nextInt();
switch(c){
case 0 :
if (cost > 100 && cost <= 200) {
rate = 5;
} else if (cost > 200 && cost <= 300) {
rate = 7.5;
} else if (cost > 300) {
rate = 10;
}
break;
case 1 :
if (cost > 0 && cost <= 100) {
rate = 5;
} else if (cost > 100 && cost <= 200) {
rate = 7.5;
} else if (cost > 200 && cost <= 300) {
rate = 10;
} else if (cost > 300) {
rate = 15;
}
break;
}
dis = cost * rate / 100;
System.out.println("Total is: "+(cost-dis));
}
}
Explanation: