Professional Documents
Culture Documents
Assignment No 3(Aniket Digge)
Assignment No 3(Aniket Digge)
3
Fair Shop requires to stock consumable goods. Every good item has an unique id, name,
purchase price, sales price, grade.
Sales price must be higher than purchase price otherwise an exception will be thrown.
Grade value is either “N” (normal commodity) or “E” (essential commodity) .
E graded items sales price cannot be more than 25% of purchase price otherwise it will throw
an exception.
Finally you need to list all items in a collection that refuses to accept duplicate ids.
Create Product class with member data
Integer id,
String name,
Double purchasedPrice,
Double salesPrice,
String grade
Create getter and setter method, override toString() with String.format("%-5s %-20s %-10s %-
10s %-5s",....);
Override hashCode() and equals().
Create PriceException class
Create EssentialCommodityException class.
Create GradeMismatchException class
Create Main class with method public static void main(String [] arg)-> It will accept the
number of items to accept then each item as a String with comma separate format eg:
1001,Salt,20.00,22.00,E
1002,Biriyani Masala,45.00,55.00,N
Finally display all listed items only. Do not show which are rejected in the list.
Ans:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.regex.Pattern;
class Prodcut{
int id;
String name;
Double purchasePrice;
Double salesPrice;
String grade;
public void getter(int id, String name, Double purchasePrice, Double salesPrice, String
grade) throws IOException{
this.id = id;
this.name = name;
this.purchasePrice = purchasePrice;
this.salesPrice = salesPrice;
this.grade = grade;
}
idSet.add(id);
nameSet.add(name);
purchasePriceSet.add(purchasePrice);
salesPriceSet.add(salesPrice);
gradeSet.add(grade);
}
@Override
public String toString() {
String.format("%-5s %-20s %-10s %-10s %-5s", id);
return super.toString();
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
class PriceException{
Double purchasePrice;
Double salesPrice;
class EssentialCommodityException{
Double purchasePrice;
Double salesPrice;
Double eprice;
class GradeMismatchException{
String grade;
GradeMismatchException(String grade){
this.grade = grade;
if(grade.matches(patt)==false){
System.out.println("Grade should be E for Essential Commodity or N for Normal
Commodity");
}
}
}
}
}
}
Output:-