0% found this document useful (0 votes)
59 views3 pages

MinimumPrice and Search by Name

This Java program defines a Vehicle class with attributes like number, name, and price. It then reads vehicle data from user input, stores it in a Vehicle array, and finds the vehicle with the minimum price or searches by name. It outputs the number, name, and price of the found vehicle, or indicates no vehicle was found. It defines methods to find the vehicle with minimum price and search by name in the Vehicle array.

Uploaded by

Som
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

MinimumPrice and Search by Name

This Java program defines a Vehicle class with attributes like number, name, and price. It then reads vehicle data from user input, stores it in a Vehicle array, and finds the vehicle with the minimum price or searches by name. It outputs the number, name, and price of the found vehicle, or indicates no vehicle was found. It defines methods to find the vehicle with minimum price and search by name in the Vehicle array.

Uploaded by

Som
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package tcsTestPRA;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

import java.util.Scanner;

class MinimumPrice_And_Search_By_Name {

public static void main(String args[]){

Scanner sc = new Scanner(System.in);


int n = sc.nextInt();

sc.nextLine();

Vehicle[] vl = new Vehicle[n];


for(int i =0;i<vl.length;i++){
int id = sc.nextInt();
sc.nextLine();

String name =sc.nextLine();


double price =sc.nextDouble();
sc.nextLine();

vl[i]= new Vehicle(id,name,price);

}
String name =sc.nextLine();

Vehicle avg= findVehicleWithMinimumPrice(vl);


if(avg!=null){
System.out.println("number-"+avg.getNumber());

System.out.println("name-"+avg.getName());

System.out.println("price-"+avg.getPrice());

}else{
System.out.println("No Vehicle found with mentioned attribute");
}

Vehicle obj1 =searchVehicleByName(vl,name);

if(obj1!=null){
System.out.println("number-"+obj1.getNumber());

System.out.println("name-"+obj1.getName());

System.out.println("price-"+obj1.getPrice());

}else{
System.out.println("No Vehicle found with mentioned attribute");
}
}
public static Vehicle findVehicleWithMinimumPrice(Vehicle[] vl)

Vehicle v= new Vehicle();


int count=0;
double min=vl[0].getPrice();

for(int i =0;i<vl.length;i++){

if(vl[i].getPrice()<min)
{

v=vl[i];
min=vl[i].getPrice();
count++;

if(count>0)
{
return v;
}

else
return vl[0];
}

public static Vehicle searchVehicleByName(Vehicle[] vl,String name){

int count = 0;
Vehicle i1=null;
for (int i = 0; i < vl.length; i++) {

if(vl[i].getName().equalsIgnoreCase(name))
{
count ++;
i1=vl[i];

}
}
if(count>0)
return i1;
else
return null;

}
}

public class Vehicle {

private int number ;


private String name ;
private double price ;
public Vehicle(int number, String name, double price) {
super();
this.number = number;
this.name = name;
this.price = price;
}
public Vehicle() {
super();
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy