Computer Science, asked by Anonymousxyz, 11 months ago

A binary file named “ABC.DAT” contains the product code (pc), unit price (up) and
quantity(q) for number of items.
Write a Method to accept a product code ‘p’ and check the availability of the product
and display with an appropriate message.
The method declaration is as follows:
void findpro( int p ).

Answers

Answered by AaronJacob2004
7

Answer:

public static void findpro(int p)

throws IOException

{ boolean found = false;

boolean flag = false;

 FileInputStream fis = new FileInputStream ("ABC.dat");

DataInputStream dis = new DataInputStream (fis);

while (!flag)

{ try

{ int pc = dis.readInt();

 double price = dis.readDouble();

 int q = dis.readInt();

 if (p==pc)

{ found = true; break; } }

catch(EOFException e){ System.out.println("Reached end of file!"); flag=true; } }

if(found)

 System.out.println(p+ " is available");

else System.out.println(p+ " not found"); dis.close(); fis.close(); }

Answered by mufaddal52hamid53
12

Perfectly formatted answer:

static public void findpro(int p) throws IOException

   {

       boolean found = false;

       boolean flag = false;

       FileInputStream fis =  new  FileInputStream("ABC.dat");

       DataInputStream dis =  new  DataInputStream(fis);

       while ( ! flag) {

           try {

               int pc = dis.readInt();

               double price = dis.readDouble();

               int q = dis.readInt();

               if (p == pc) {

                   found = true;

                   break;

               }

           }

           catch (EOFException e) {

               System.out.println("Reached end of file!");

               flag = true;

           }

       }

       if (found) {

           System.out.println(p + " is available");

       }

       else {

           System.out.println(p + " not found");

       }

       dis.close();

       fis.close();

   }

Similar questions