-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXYZ_Shop_exception.java
46 lines (42 loc) · 1.29 KB
/
XYZ_Shop_exception.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Read Description for Question.
import java.util.*;
class InvalidChoiceException extends Exception{
public InvalidChoiceException(String msg){
super(msg);
}
}
class Product{
String name;
int qty;
double price;
double amount;
void readData(Scanner sc)throws InvalidChoiceException{
name=sc.next();
qty=sc.nextInt();
price=sc.nextDouble();
amount=price*qty;
if(name.equals("Shoe")&&qty>1)
throw new InvalidChoiceException("You can not buy more than one Shoe");
else if(name.equals("Perfume")&&amount>1500)
throw new InvalidChoiceException("Bill Amount can not exceed Rs.1500");
else if(name.equals("Chocolate")&&qty>20)
throw new InvalidChoiceException("You can not choose more than 20 Chocolates");
printData();
}
void printData(){
System.out.println("Product Name:"+name);
System.out.println("Quantity:"+qty);
System.out.println("Price:"+price);
System.out.println("Bill Amount:"+amount);
}
}
class Main{
public static void main(String s[]){
Product p1=new Product();
Scanner ip=new Scanner(System.in);
try{
p1.readData(ip);}
catch (InvalidChoiceException e){
System.out.println(e);
}
}}