Java OPA code
please change variable names
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//code to read values
//code to call required method
//code to display the result
int bakerId;
String bakersName;
char bakerClass;
double bakerRating;
boolean onlineDelivery;
Scanner s = new Scanner(System.in);
Bakers[] b = new Bakers[4];
for(int i=0;i<b.length;i++){
bakerId = s.nextInt();s.nextLine();
bakersName = s.nextLine();
bakerClass = s.next().charAt(0);
bakerRating = s.nextDouble();
onlineDelivery = s.nextBoolean();
b[i] = new Bakers(bakerId, bakersName, bakerClass, bakerRating, onlineDelivery);
}
int getBakerId = s.nextInt();
char getBakerClass = s.next().charAt(0);
String deliveryType = findDeliveryType(b, getBakerId);
Bakers[] baker = sortBakersRatingOfClass(b, getBakerClass);
if(deliveryType == null)
System.out.println("There is no Bakers with given id");
else
System.out.println(deliveryType);
if(baker == null)
System.out.println("Bakers with given class is not present");
else{
for(int i=0;i<baker.length;i++){
System.out.println(baker[i].getBakerRating());
}
}
}
public static String findDeliveryType(Bakers[] b, int bakerId){
for(int i=0;i<b.length;i++){
if(b[i].getBakerId() == bakerId){
if(b[i].getOnlineDelivery()){
return "Online Available";
}
else
return "Online Not Available";
}
}
return null;
}
//code the first method
public static Bakers[] sortBakersRatingOfClass(Bakers[] b, char bakerClass){
int count = 0;
for(int i=0;i<b.length;i++){
if(b[i].getBakerClass() == bakerClass)
count++;
}
if(count == 0) return null;
Bakers[] baker = new Bakers[count];
count = 0;
for(int i=0;i<b.length;i++){
if(b[i].getBakerClass() == bakerClass){
baker[count++] = b[i];
}
}
for(int i=0;i<baker.length;i++){
for(int j=i+1;j<baker.length;j++){
if(baker[i].getBakerRating() >= baker[j].getBakerRating()){
Bakers temp = baker[i];
baker[i] = baker[j];
baker[j] = temp;
}
}
}
return baker;
}
//code the second method
}
//code the class
class Bakers{
int bakerId;
String bakersName;
char bakerClass;
double bakerRating;
boolean onlineDelivery;
Bakers(int bakerId, String bakersName, char bakerClass, double bakerRating, boolean onlineDelivery){
this.bakerId = bakerId;
this.bakersName = bakersName;
this.bakerClass = bakerClass;
this.bakerRating = bakerRating;
this.onlineDelivery = onlineDelivery;
}
int getBakerId(){
return bakerId;
}
String getBakersName(){
return bakersName;
}
char getBakerClass(){
return bakerClass;
}
double getBakerRating(){
return bakerRating;
}
boolean getOnlineDelivery(){
return onlineDelivery;
}
}
Comments
Post a Comment