Nothing Special   »   [go: up one dir, main page]

Create Sequential File Program Aim

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

EX.

NO:10
DATE:
CREATE SEQUENTIAL FILE PROGRAM
AIM:

Write a Java Program to create a sequential file that could store details
about five products. Details include product code, cost, and number of items
available and are provided through the keyboard. Compute and print the total
value of all the five products.

PROCEDURE:

Step 1: Start the Program.

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Step 7:

Step 8:

Step 9:

Step 10: Stop the program.

PROGRAM:

import java.io.*;
import java.util.*;
class Stock
{
static DataInputStream dis=new DataInputStream(System.in);
static StringTokenizer st;
public static void main(String args[])throws IOException
{
FileOutputStream fos=new FileOutputStream("invent.txt");
DataOutputStream dos=new DataOutputStream(fos);

int n=5;
for(int i=1;i<=n;i++)
{
System.out.println("Product: "+i);
System.out.println("Enter Code Number");
st=new StringTokenizer(dis.readLine());
int code=Integer.parseInt(st.nextToken());
dos.writeInt(code);
System.out.println("Enter Number of items");
st=new StringTokenizer(dis.readLine());
int items=Integer.parseInt(st.nextToken());
dos.writeInt(items);
System.out.println("Enter Cost");
st=new StringTokenizer(dis.readLine());
double cost=new Double(st.nextToken()).doubleValue();
dos.writeDouble(cost);
}
dos.close();

FileInputStream fis=new FileInputStream("invent.txt");


DataInputStream dis=new DataInputStream(fis);
double totalcost=0;;
for(int i=1;i<=n;i++)
{
int codenumber=dis.readInt();
System.out.println();
System.out.println("Product: "+i);
System.out.println("CodeNumber : "+codenumber);
int totalitems=dis.readInt();
System.out.println("Total Items:" +totalitems);
double itemcost=dis.readDouble();
System.out.println("ItemCost:" +itemcost);
totalcost+=totalitems*itemcost;
}
dis.close();

System.out.println();
System.out.println("Total Cost" +totalcost);
}
}

OUTPUT:
RESULT:
Thus the above program has been successfully created and verified.

You might also like