Write a program to read item information (id, name, price, qty) in file “item.dat”. Write a menu driven program to perform the following operations using Random access file:
i. Search for a specific item by name.
ii. Find costliest item.
ii. Display all items and total cost
Ex5setb1.java
import java.io.*;
import java.util.*;
class Ex5setb
{
public static void main(String args[])throws IOException
{
int c;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String[] part=null;String line=null;
do
{
BufferedReader fr=new BufferedReader(new FileReader("item.dat"));
int search()
{
while((line=fr.readLine())!=null)
{
a[i]=line;
part=line.split(" ");
if(Integer.parseInt(part[2])>max)
max=Integer.parseInt(part[2]);i++;return i;
}
System.out.println("Menu :\n"+"1:Search for a item by name\n"+"2:Find costliest item\n"+"3:Display all items and total cost\n4:Exit\n"+"Choice :" );
int flag=0;
c=Integer.parseInt(b.readLine());
switch (c)
{
case 1:
System.out.println("Enter item name to be searched:");
String tname=b.readLine();
while((line=fr.readLine())!=null)
{
part=line.split(" ");
if(part[1].equals(tname)){
flag=1;
break;
}
}
if(flag==1)
System.out.println(part[0]+" "+part[1]+" "+part[2]+" "+part[3]);
else
System.out.println("Name is not in list");
fr.close();
break;
case 2:
int max=0,i=0; String [] a;
int m=search();
System.out.println(a[m]);
}
System.out.println(max);
fr.close();
break;
case 3:
while((line=fr.readLine())!=null)
{
part=line.split(" ");
System.out.println(part[0]+" "+part[1]+" "+part[2]+" "+part[3]+" "+Integer.parseInt(part[2])*Integer.parseInt(part[3]));
}
fr.close();
break;
default:
System.out.println("Invalid Option");
}
}while(c!=4);
}
}
0 Comments
If anyone has Doubts or suggestions please let me know