Menu Drivan Using Array
Question Is:-
1.Add Car
2.Display All Car Using ArrayList
3.Display By ID(Enter 1 Id so all Details About 1 id )
4.Display By Price (Enter 1000 in Price So it show 1000 Price of all Cars Details)
5.Display By Make Model (Model Year)
6.Display By Average (Average)
7.Exit
Code For Menu Drivan In Array
import java.util.*;
class Car
{
int cid,cmakemodel,cavg;
String cname,ccomapny,ccolor;
double cprice;
void getData()
{
Scanner scn = new Scanner(System.in);
System.out.print("Enter Car ID:");
cid=scn.nextInt();
scn = new Scanner(System.in);
System.out.print("Enter Car Name:");
cname=scn.nextLine();
System.out.print("Enter Car Company:");
ccomapny=scn.nextLine();
System.out.print("Enter Car Color:");
ccolor=scn.nextLine();
System.out.print("Enter Car Price:");
cprice=scn.nextDouble();
System.out.print("Enter Car Average:");
cavg=scn.nextInt();
System.out.print("Enter Car MAke Model:");
cmakemodel=scn.nextInt();
}
void printData()
{
System.out.println("-----------------------");
System.out.println("Car ID:"+cid);
System.out.println("Car Name"+cname);
System.out.println("Car Company:"+ccomapny);
System.out.println("Car Color:"+ccolor);
System.out.println("Car Price:"+cprice);
System.out.println("Car Average:"+cavg);
System.out.println("Car MAke Model:"+cmakemodel);
System.out.println("-----------------------");
}
}
class CarDemo
{
public static void main(String args[])
{
ArrayList<Car> arr = new ArrayList<Car>();
while(true)
{
System.out.println("1.Add Car");
System.out.println("2.Display All");
System.out.println("3.Display by id");
System.out.println("4.Display by Price");
System.out.println("5.Display by Make Model");
System.out.println("6.Display by Color");
System.out.println("7.Display by Average");
System.out.println("8.Exit");
Scanner scn = new Scanner(System.in);
System.out.println("Enter Choice:");
int choice = scn.nextInt();
switch(choice)
{
case 1:
Car c = new Car();
c.getData();
arr.add(c);
break;
case 2:
for (int index=0;index<arr.size();index++)
{
Car cIndex = arr.get(index);
cIndex.printData();
}
break;
case 3:
System.out.print("Enter Car ID to print:");
int searchid = scn.nextInt();
boolean flag=false;
for (int index=0;index<arr.size();index++)
{
Car cI = arr.get(index);
if( cI.cid == searchid)
{
cI.printData();
flag=true;
break;
}
}
if(flag==false)
System.out.println("Not Found");
break;
case 4:
System.out.print("Enter Car Price:");
double cprice = scn.nextInt();
boolean flg=false;
for (int index=0;index<arr.size();index++)
{
Car cI = arr.get(index);
if( cI.cprice >= cprice)
{
cI.printData();
flg=true;
}
}
if(flg==false)
System.out.println("Not Found");
break;
case 5:
System.out.print("Enter Car MakeModel:");
int cmakemodel = scn.nextInt();
boolean f=false;
for (int index=0;index<arr.size();index++)
{
Car cI = arr.get(index);
if( cI.cmakemodel >= cmakemodel)
{
cI.printData();
f=true;
}
}
if(f==false)
System.out.println("Not Found");
break;
case 6:
System.out.print("Enter Car Color:");
scn = new Scanner(System.in);
String ccolor = scn.nextLine();
boolean fl=false;
for (int index=0;index<arr.size();index++)
{
Car cI = arr.get(index);
if(cI.ccolor.equals(ccolor))
{
cI.printData();
fl=true;
}
}
if(fl==false)
System.out.println("Not Found");
break;
case 7:
System.out.print("Enter Car Average:");
int cavg = scn.nextInt();
boolean fl1=false;
for (int index=0;index<arr.size();index++)
{
Car cI = arr.get(index);
if( cI.cavg >= cavg)
{
cI.printData();
fl1=true;
}
}
if(fl1==false)
System.out.println("Not Found");
break;
case 8:
System.exit(0);
default:
}
}
}
}
OutPut:-
1)Simple Menu In Array
2)Enter Choice Add Car(2 Car Added)
3)Display All Car
4)Display By Id
5)Display By Price
6)Display By Make Model
7)Display By Color
8)Display By Average
9)Exit
Also Watch This Video For More Details...
0 Comments