Don't like ads? PRO users don't see any ads ;-)

stockManage.java

By: ADoyle4 on Apr 30th, 2012  |  syntax: None  |  size: 1.50 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2. import java.util.*;
  3. class StockManage{
  4.         private String author;//these are my instance variables
  5.         private String name;
  6.         private String ISBN;
  7.         private int stockLevel;
  8.         private String transactionISBN;
  9.         private String transactionLevel;       
  10.                
  11.         void putBooks(){//prints the books.dat file in a readable format
  12.                  
  13.                 System.out.printf("%-12s%-17s%-30s%12d\n",ISBN, author, name, stockLevel );
  14.         }
  15.        
  16.         void putTransactions(){//prints The transactions.txt file      
  17.                 System.out.printf("%-15s %-7s\n", transactionISBN, transactionLevel);
  18.         }
  19.  
  20.         public static void main(String [] args)
  21.         {
  22.                 StockManage b = new StockManage();// created new object of type book
  23.                
  24.                 try{
  25.                         RandomAccessFile books = new RandomAccessFile("books.dat", "rw");
  26.                         Scanner transactions =new Scanner( new File("transactions.txt"));
  27.                        
  28.                        
  29.                         while(books.getFilePointer()<books.length()){//this loop runs through and reads the binary file
  30.                                        
  31.                                 b.author = books.readUTF();
  32.                                 b.name = books.readUTF();
  33.                                 b.ISBN = books.readUTF();
  34.                                 b.stockLevel = books.readInt();
  35.                                
  36.                                 b.putBooks();
  37.                                
  38.                         }
  39.                         books.close();//closes book file
  40.                        
  41.                         System.out.printf("%15s", "TRANSACTIONS");
  42.                         System.out.println(" ");
  43.                         while(transactions.hasNext()){//this loop reads the text file
  44.                                 b.transactionISBN = transactions.next();
  45.                                 b.transactionLevel = transactions.next();
  46.                                 b.putTransactions();   
  47.                                
  48.                         }
  49.                         transactions.close();
  50.                        
  51.                 }
  52.                 catch(Exception e){
  53.                         e.printStackTrace();
  54.                 }
  55.         }
  56. }