Java Program to read item information (id, name, price, qty) in file “item.dat”. Write a menu driven program to perform operations - IProgramX

Q.  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 
 Progarm:




import java.io.*;
import java.util.*;

class item
{

String name,id;
int qty;
double price,total;

item(String i,String n,String q,String p)
{

name=n;
id=i;
qty=Integer.parseInt(q);
price=Double.parseDouble(p);
total=qty*price;

}

public String toString()
{

String s=name+" "+id+" "+qty+" "+price+" "+total;
return(s);

}

public static void search(item[] arr,int n)throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();

for(int i=0;i<n;i++)
{

if(arr[i].name.equals(s))
{

System.out.println(arr[i].toString());
return;

}

}

System.out.println("No Records Found");

}

public static void searchc(item[] arr,int n)
{

double max=0;int c=0;
     
        for(int i=0;i<n;i++)
        {
     
        if(arr[i].price > max)
        {
     
        c=i;
     
        }

}
System.out.println(arr[c].toString());

}

}

class ass5b1
{

public static void main(String[] args)throws IOException
{

String s,space=" ";
        int c,i;

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter no. of records");

int n=Integer.parseInt(b.readLine());

System.out.println("Enter Records:\n<id> <name> <price> <qty> :");

FileWriter f=new FileWriter("item.dat");

for(i=0;i<n;i++)
{

s=b.readLine()+"\n";
f.write(s);

}

f.close();

item it[]=new item[20];
FileReader fr=new FileReader("item.dat");
BufferedReader br=new BufferedReader(fr);

for(i=0;i<n;i++)
{

s=br.readLine();
StringTokenizer t=new StringTokenizer(s,space);
            String si=new String(t.nextToken());
            String sn=new String(t.nextToken());
            String sq=new String(t.nextToken());
            String sp=new String(t.nextToken());
it[i]=new item(si,sn,sq,sp);

}

do
{

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 :" );

c=Integer.parseInt(b.readLine());

switch (c)
{

case 1:
System.out.println("Enter item name to be searched:");
item.search(it,n); break;

case 2:
System.out.println("Costliest Item :");item.searchc(it,n);break;

case 3:
for(i=0;i<n;i++)
    System.out.println(it[i].toString());
    break;

case 4:
break;
     
        default:
        System.out.println("Invalid Option");
}
}while(c!=4);
}
}


Output:

Enter no. of records
3
Enter Records:
<id> <name> <price> <qty> :
1001 Tyres 2000 4
1002 Leather 750 5
1003 Mirrors 190 2
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
1
Enter item name to be searched:
Tyres
Tyres 1001 2000 4.0 8000.0
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
2
Costliest Item :
Mirrors 1003 190 2.0 380.0
Menu :
1:Search for a item by name
2:Find costliest item
3:Display all items and total cost
4:ExitChoice :
3
Tyres 1001 2000 4.0 8000.0
Leather 1002 750 5.0 3750.0
Mirrors 1003 190 2.0 380.0
 

Post a Comment

1 Comments

  1. Slip15_1: Accept the names of two files and copy the contents of the first to the second. First file
    having Book name and Author name in file.
    import java.io.*;
    import java.util.*;
    class demoFile
    {
    public static void main(String args[]) throws Exception
    MOBILE: 9730381255 | WWW.NRCLASSESPUNE.COM | WWW.BCSBCA.COM
    {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter the first file");
    String f1=sc.next();
    System.out.println("Enter the second file");
    String f2=sc.next();
    FileInputStream fis=new FileInputStream(f1);
    FileOutputStream fos=new FileOutputStream(f2);
    int ch;
    while((ch=fis.read())!=-1)
    {
    fos.write(ch);
    }
    System.out.println("file copied...");
    fis.close();
    fos.close();
    }
    }

    ReplyDelete