import java.io.IOException;
import java.io.RandomAccessFile;

public class Database
{  public static void main(String[] args)
   {  ConsoleReader console = new ConsoleReader(System.in);
      System.out.println
         ("Please enter the data file name:");
      String filename = console.readLine();
      try
      {  RandomAccessFile file 
            = new RandomAccessFile(filename, "rw");
         long nrecord = file.length() / RECORD_SIZE;
         boolean done = false;
         while (!done)
         {  System.out.println
               ("Please enter the record to update (1 - "
               + nrecord + "), new record (0), quit (-1)");
            int pos = console.readInt();

            if (1 <= pos && pos <= nrecord) // update record
            {  file.seek((pos - 1) * RECORD_SIZE);
               Product p = readProduct(file);
               System.out.println("Found " + p.getName() 
                  + " " + p.getPrice() + " " + p.getScore());
               System.out.println
                  ("Enter the new price:");
               double newPrice = console.readDouble();
               p.setPrice(newPrice);
               file.seek((pos - 1) * RECORD_SIZE);
               writeProduct(file, p);
            }
            else if (pos == 0) // add record
            {  System.out.println("Enter new product:");
               String name = console.readLine();
               System.out.println("Enter price:");
               double price = console.readDouble();
               System.out.println("Enter score:");
               int score = console.readInt();
               Product p = new Product(name, price, score);
               file.seek(nrecord * RECORD_SIZE);
               writeProduct(file, p);
               nrecord++;
            }
            else if (pos == -1)
               done = true;
         }
         file.close();
      }
      catch(IOException e)
      {  System.out.println("Input/Output Exception " + e);
      }
   }

   /**
      Reads a fixed width string.
      @param f the file to read from
      @param size the number of characters to read
      @return the string with trailing spaces removed
   */
   public static String readFixedString(RandomAccessFile f, 
      int size) throws IOException
   {  String b = "";
      for (int i = 0; i < size; i++)
         b += f.readChar();
      return b.trim();
   }
   
   /**
      Writes a fixed width string.
      @param f the file to write to
      @param size the number of characters to write
   */
   public static void writeFixedString(RandomAccessFile f, 
      String s, int size) throws IOException
   {  if (s.length() <= size)
      {  f.writeChars(s);
         for (int i = s.length(); i < size; i++)
            f.writeChar(' ');
      }
      else
         f.writeChars(s.substring(0, size));
   }

   /**
      Reads a product record.
      @param f the file to read from
      @return the next product stored in the file.
   */
   public static Product readProduct(RandomAccessFile f)
      throws IOException
   {  String name = readFixedString(f, NAME_SIZE);
      double price = f.readDouble();
      int score = f.readInt();
      return new Product(name, price, score);
   }

   /**
      Writes a product record.
      @param f the file to write to
   */
   public static void writeProduct(RandomAccessFile f,
      Product p) throws IOException
   {  writeFixedString(f, p.getName(), NAME_SIZE);
      f.writeDouble(p.getPrice());
      f.writeInt(p.getScore());
   }

   public static final int NAME_SIZE = 30;

   public static final int CHAR_SIZE = 2;
   public static final int INT_SIZE = 4;
   public static final int DOUBLE_SIZE = 8;
  
   public static final int RECORD_SIZE 
      = CHAR_SIZE * NAME_SIZE + DOUBLE_SIZE + INT_SIZE;
}
