Advertisement
Nakumas

Egzamin PP&JP: 2019, Zad.2 C

Feb 21st, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package egzamin2019c2;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5.  
  6. public class Egzamin2019C2
  7. {
  8.     static String zmniejszajaca(String plik)
  9.     {
  10.         String nazwaLeku;
  11.         int cenaLekuPlik;
  12.         int cenaLeku;
  13.        
  14.         boolean refundacja;
  15.        
  16.         long pointerPrzedCena;
  17.         long pointerPoCenie;
  18.        
  19.         int cenaNajmniejsza = Integer.MAX_VALUE;
  20.         String nazwaNajtanszegoLeku = "";
  21.        
  22.         try(RandomAccessFile raf = new RandomAccessFile(plik, "rw");)
  23.         {
  24.             while(raf.getFilePointer() < raf.length())
  25.             {
  26.                 nazwaLeku = raf.readUTF();
  27.                
  28.                 pointerPrzedCena = raf.getFilePointer();
  29.                 cenaLekuPlik = raf.readInt();
  30.                 pointerPoCenie = raf.getFilePointer();
  31.                
  32.                 refundacja = raf.readBoolean();
  33.                
  34.                 if(refundacja)
  35.                 {
  36.                     cenaLeku = (int)(cenaLekuPlik - cenaLekuPlik*0.15);
  37.                     raf.seek(pointerPrzedCena);
  38.                     raf.writeInt(cenaLeku);
  39.                     raf.seek(pointerPoCenie);
  40.                 }
  41.                 else
  42.                     cenaLeku = cenaLekuPlik;
  43.                
  44.                 if(cenaLeku<cenaNajmniejsza)
  45.                     nazwaNajtanszegoLeku = nazwaLeku;
  46.             }  
  47.         }
  48.         catch(IOException e)
  49.         { System.out.println("Wystąpił wyjątek" + e.getMessage());}
  50.        
  51.         return nazwaNajtanszegoLeku;
  52.     }
  53.     public static void main(String[] args)
  54.     {
  55.         // TODO code application logic here
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement