Program
To store and display product details using random access file mode.
Algorithm
Step 1: Start
Step 2: Declare and initialize variable
Step 3: Read fname
Step 4: File rf=new file(fname)
Step 5: Repeat steps 6 to 14 until temp=’y’,increment n by 1
Step 6: Print “Enter product code”
Step 7: Read code
Step 8: Print “Enter product name”
Step 9: Read name
Step 10: Print “Enter product cost”
Step 11: Read cost
Step 12: Print “Enter product stock”
Step 13: Read no
Step 14: Write data to the file
Step 15: Print fname
Step 16: Repeat steps 17-19until n>0
Step 17: Read code,name,cost,no from file
Step 18: Print code,name,cost,no
Step 19: Decrement n by 1
Step 20: Stop
Program
import java.io.*; class Rand { File fn; RandomAccessFile rf; int pcode,stock,blk_size; float cost; byte bpname[]; String pname; Rand(String fname) { fn=new File(fname); try { rf=new RandomAccessFile(fn,"rw"); } catch (Exception e) { System.out.println("Error : " + e); System.exit(0); } bpname=new byte[20]; blk_size=4*3+20; } void AddProduct(DataInputStream in) { int i; try { System.out.println("Enter the product code : "); pcode=Integer.parseInt(in.readLine()); System.out.println("Enter the product name :"); pname=in.readLine(); System.out.println("Enter the product cost : "); cost=Float.parseFloat(in.readLine()); System.out.println("Enter the product stock : "); stock=Integer.parseInt(in.readLine()); rf.seek(rf.length()); rf.writeInt(pcode); for(i=0;i<pname.length();i++) rf.writeByte((byte)pname.charAt(i)); for(;i<20;i++) rf.writeByte((byte)'\0'); rf.writeFloat(cost); rf.writeInt(stock); } catch (Exception e) { System.out.println("Error : " + e); System.exit(0); } } void DisplayAll() { System.out.println(" PCODE\tPNAME\t\tCOST\tSTOCK"); System.out.println("----------------------------------------"); try { rf.seek(0); long len=rf.length(); while(rf.getFilePointer()<len) { pcode=rf.readInt(); for(int i=0;i<20;i++) bpname[i]=rf.readByte(); cost=rf.readFloat(); stock=rf.readInt(); System.out.print(" "+ pcode + "\t"); for(int i=0;bpname[i]!='\0';i++) System.out.print((char)bpname[i]); System.out.print("\t\t" + cost + "\t"); System.out.print(stock + "\t\n"); } System.out.println("----------------------------------------"); } catch (Exception e) { System.out.println("Error : " + e); System.exit(0); } } void DisplayAlternate() { System.out.println(" PCODE\tPNAME\t\tCOST\tSTOCK"); System.out.println("----------------------------------------"); try { rf.seek(0); long len=rf.length(); while(rf.getFilePointer()<len) { pcode=rf.readInt(); for(int i=0;i<20;i++) bpname[i]=rf.readByte(); cost=rf.readFloat(); stock=rf.readInt(); System.out.print(" " + pcode + "\t"); for(int i=0;bpname[i]!='\0';i++) System.out.print((char)bpname[i]); System.out.print("\t\t" + cost + "\t"); System.out.print(stock + "\t\n"); rf.skipBytes(blk_size); } System.out.println("----------------------------------------"); } catch (Exception e) { System.out.println("Error : " + e); System.exit(0); } } } public class MyRandom { public static void main(String[] args) { int choice; DataInputStream in=new DataInputStream(System.in); Rand r=new Rand("product.dat"); try { do { System.out.println("Menu\n-------"); System.out.println("1.Add Product\n2.Display All Product"); System.out.println("3.Display Alternating Products\n4.Exit\n"); choice=Integer.parseInt(in.readLine()); if(choice==1) r.AddProduct(in); else if(choice==2) r.DisplayAll(); else if(choice==3) r.DisplayAlternate(); else if(choice==4) System.exit(0); else System.out.println("Invalid Choice"); }while(true); } catch(Exception e) { System.out.println("Error : "+ e); } } } |
Output
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
101
Enter the product name :
Jam
Enter the product cost :
10
Enter the product stock :
36
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
102
Enter the product name :
Cake
Enter the product cost :
25
Enter the product stock :
66
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
103
Enter the product name :
Pickle
Enter the product cost :
32
Enter the product stock :
26
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
104
Enter the product name :
Soap
Enter the product cost :
12
Enter the product stock :
8
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
1
Enter the product code :
105
Enter the product name :
Pen
Enter the product cost :
11
Enter the product stock :
10
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
2
PCODE PNAME COST STOCK
—————————————-
101 Jam 10.0 36
102 Cake 25.0 66
103 Pickle 32.0 26
104 Soap 12.0 8
105 Pen 11.0 10
—————————————-
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
3
PCODE PNAME COST STOCK
—————————————-
101 Jam 10.0 36
103 Pickle 32.0 26
105 Pen 11.0 10
—————————————-
Menu
——-
1.Add Product
2.Display All Product
3.Display Alternating Products
4.Exit
Leave a Reply