Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment No.

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;
}

public void setter(){

HashSet<Integer> idSet = new HashSet<Integer>();


HashSet<String> nameSet = new HashSet<String>();
HashSet<Double> purchasePriceSet = new HashSet<Double>();
HashSet<Double> salesPriceSet = new HashSet<Double>();
HashSet<String> gradeSet = new HashSet<String>();

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;

PriceException(Double purchasePrice, Double salesPrice){


this.purchasePrice = purchasePrice;
this.salesPrice = salesPrice;

if(salesPrice < purchasePrice){


System.out.println("Sales price should be grater than purchase price....");
}
}
}

class EssentialCommodityException{
Double purchasePrice;
Double salesPrice;
Double eprice;

EssentialCommodityException(Double purchasePrice, Double salesPrice){


this.purchasePrice = purchasePrice;
this.salesPrice = salesPrice;
eprice = (purchasePrice * 0.25)+salesPrice;

if(salesPrice > eprice){


System.out.println("Sales price for essential commodity should be 25% grater than
purchase price....");
}
}
}

class GradeMismatchException{
String grade;
GradeMismatchException(String grade){
this.grade = grade;

String patt = "N|E";

if(grade.matches(patt)==false){
System.out.println("Grade should be E for Essential Commodity or N for Normal
Commodity");
}
}
}

public class Product_detail {


public static void main(String[] args)throws IOException {
int id;
String name;
Double purchasePrice;
Double salesPrice;
String grade;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter how many items you want add : ");
int num = Integer.parseInt(br.readLine());

for(int i = 1; i <= num; i++){


System.out.println("Enter ID :");
id = Integer.parseInt(br.readLine());

System.out.println("Enter name :");


name = br.readLine();

System.out.println("Enter Purchase Price :");


purchasePrice = Double.parseDouble(br.readLine());

System.out.println("Enter Sales Price :");


salesPrice = Double.parseDouble(br.readLine());

System.out.println("Enter Purchase Grade :");


grade = br.readLine();

PriceException pe = new PriceException(purchasePrice, salesPrice);


EssentialCommodityException ece = new
EssentialCommodityException(purchasePrice, salesPrice);
GradeMismatchException gme = new GradeMismatchException(grade);
Prodcut p = new Prodcut();
p.getter(id, name, purchasePrice, salesPrice, grade);
p.setter();

}
}
}
Output:-

You might also like