Create the class Flowers with below attribute:
flowerId-int;
flowerName-String;
price-int;
rating-int;
type-String;
The above attributes must be private,write getters and setters and
parametrized constructor as well.
Create class MyClass with main method.
Implement one static method:findMinPriceByType in MyClass class.
findMinPriceByType Method:
This method will take two input parameters:Array of Flower objects and
String parameter.
This method will return the minimum priced flower object from array
of Flower objects for the given type(String parameter),only if the rating
is greater than 3.If no Flowers with the above conditions are present in
the array of the flower objects,then the method will return null.
for this method-The main method should print the flowerId from the returned
object if the returned value is not null.else it should print "There is no
flower with given type".
**********************************************************************
input1:
123
Yellow trout lilly
3000
5
ephemerals
345
snowdrop
2500
4
ephemerals
321
Yarrow
1000
4
perennials
213
red trillium
2250
4
ephemerals
ephemerals
output:
213
**********************
input2:
123
Yellow trout lilly
3000
5
ephemerals
345
snowdrop
2500
4
ephemerals
321
Yarrow
1000
4
perennials
213
red trillium
2250
4
ephemerals
Biennial
output2:
There is no flower with given type.
**************************************************************************
code is given below:
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Flowers[] flowers=new Flowers[4];
for (int i = 0; i <4 ; i++) {
int a=sc.nextInt();sc.nextLine();
String b=sc.nextLine();
int c=sc.nextInt();sc.nextLine();
int d=sc.nextInt();sc.nextLine();
String e=sc.nextLine();
flowers[i] = new Flowers(a,b,c,d,e);
}
String input=sc.nextLine();
int ans= findMinPriceByType(flowers,input);
if(ans==0)
{
System.out.println("There is no flower with given type");
}
else
{
System.out.println(ans);
}
}
public static int findMinPriceByType(Flowers[] flowers,String input)
{
int idd=Integer.MAX_VALUE;
int min=Integer.MAX_VALUE;
for (int i = 0; i <flowers.length ; i++) {
if(flowers[i].getType().equalsIgnoreCase(input) && flowers[i].getRating()>3 && flowers[i].getPrice()<min)
{
min=flowers[i].getPrice();
idd=flowers[i].getFlowerId();
}
}
if(min!=Integer.MAX_VALUE)
{
return idd;
}
return 0;
}
}
class Flowers
{
private int flowerId;
private String flowerName;
private int price;
private int rating;
private String type;
//parametrized constructor
public Flowers(int flowerId, String flowerName, int price, int rating, String type) {
this.flowerId = flowerId;
this.flowerName = flowerName;
this.price = price;
this.rating = rating;
this.type = type;
}
//getter and Setters
public int getFlowerId() {
return flowerId;
}
public void setFlowerId(int flowerId) {
this.flowerId = flowerId;
}
public String getFlowerName() {
return flowerName;
}
public void setFlowerName(String flowerName) {
this.flowerName = flowerName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Hi
ReplyDeleteI want to talk to you. Please contact me rpoins9889@gmail.com