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

You have a file having Snacks details.

The format of the file is as below ('#' is the field delimeter):


ID#Name#Count
101#Sweet#22
234#chips#24
341#juice#26
511#chocolates#27

Write a Unix command to count the number of Snacks with count greater than 25.
Print output in following format:
Total count: <count>
Sample Input 1 :
ID#Name#Count
101#Sweet#22
234#chips#24
341#juice#26
511#chocolates#27
Output : Total count: 2

awk 'BEGIN{FS="#";count=0}
{
if($3>25)
{
count=count+1;
}
}
END{
print"Total count:",count;
}'

Create a class Retail with below attributes:


id - int
name - String
quantity- int
price(per unit) - double
Write getters, setters and parameterized constructor in the above-mentioned
attribute sequence as required.
Create class Solution with main method Implement two static methods -
findRetailWithMaximumPrice and searchRetailByName in Solution class.
findRetailWithMaximumPrice Create a static method findRetailWithMaximumPrice in the
Solution class.
This method will take array of Retail objects and returns the Retail object having
the maximum Price if found else return null if not found.
searchRetailByName Create a static method searchRetailByName in the Solution class.

This method will take array of Retail objects and Name as input and returns the
Retail object having the mentioned name if found else return null if not found.
4
111 cotton 1000 300.75
222 velvet 500 200.25
334 linen 200 617.79
444 flannel 789 222.65 flannel

package handson;

import java.util.Scanner;

class Retail
{
private int id;
private String name;
private int quantity;
private double price;
public Retail(int id, String name, int quantity, double price) {
super();
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Solution {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
Retail[]r = new Retail[n];
for(int i=0;i<n;i++)
{
int id =sc.nextInt();
sc.nextLine();
String name=sc.nextLine();
int quantity = sc.nextInt();
double price= sc.nextDouble();
r[i]= new Retail(id,name,quantity,price);
}
sc.nextLine();
String na=sc.nextLine();
Retail r1 = findRetailWithMaximumPrice(r);
Retail r2 = searchRetailByName(r,na);
if(r1!=null)
{
System.out.println("id-"+r1.getId());
System.out.println("name-"+r1.getName());
System.out.println("quantity-"+r1.getQuantity());
System.out.println("price-"+r1.getPrice());
}
else
{
System.out.println("No Retail found with mentioned
attribute");
}
if(r2!=null)
{
System.out.println("id-"+r2.getId());
System.out.println("name-"+r2.getName());
System.out.println("quantity-"+r2.getQuantity());
System.out.println("price-"+r2.getPrice());
}
else
{
System.out.println("No Retail found with mentioned
attribute");
}
}
private static Retail searchRetailByName(Retail[] r, String na)
{
Retail r2=null;
for(int i=0;i<r.length;i++)
{
if(r[i].getName().equalsIgnoreCase(na))
{
r2=r[i];
}
}return r2;
}

private static Retail findRetailWithMaximumPrice(Retail[] r) {


Retail r1=null;
double max=0.0;
for(int i=0;i<r.length;i++)
{
if(r[i].getPrice()>max)
{
max=r[i].getPrice();
r1=r[i];
}
}
return r1;
}
}

You might also like