
stockManage.java
By:
ADoyle4 on
Apr 30th, 2012 | syntax:
None | size: 1.50 KB | hits: 15 | expires: Never
import java.io.*;
import java.util.*;
class StockManage{
private String author;//these are my instance variables
private String name;
private String ISBN;
private int stockLevel;
private String transactionISBN;
private String transactionLevel;
void putBooks(){//prints the books.dat file in a readable format
System.out.printf("%-12s%-17s%-30s%12d\n",ISBN, author, name, stockLevel );
}
void putTransactions(){//prints The transactions.txt file
System.out.printf("%-15s %-7s\n", transactionISBN, transactionLevel);
}
public static void main(String [] args)
{
StockManage b = new StockManage();// created new object of type book
try{
RandomAccessFile books = new RandomAccessFile("books.dat", "rw");
Scanner transactions =new Scanner( new File("transactions.txt"));
while(books.getFilePointer()<books.length()){//this loop runs through and reads the binary file
b.author = books.readUTF();
b.name = books.readUTF();
b.ISBN = books.readUTF();
b.stockLevel = books.readInt();
b.putBooks();
}
books.close();//closes book file
System.out.printf("%15s", "TRANSACTIONS");
System.out.println(" ");
while(transactions.hasNext()){//this loop reads the text file
b.transactionISBN = transactions.next();
b.transactionLevel = transactions.next();
b.putTransactions();
}
transactions.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}