Advertisement
Nakumas

Egzamin PP&JP: 2017, Zad.1 B

Feb 10th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zad1b2017;
  7.  
  8. import java.io.RandomAccessFile;
  9.  
  10. /**
  11.  *
  12.  * @author Szymek
  13.  */
  14. public class Zad1B2017 {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args)
  20.     {
  21.        
  22.     }
  23.    
  24.     double funckja(String nazwaPliku, String nazwaTowaru)
  25.     {
  26.         double cena = 0;
  27.        
  28.        try(RandomAccessFile raf = new RandomAccessFile(nazwaPliku, "rw");)
  29.        {
  30.            int iloscTowarow = 0;
  31.            long wielkoscPliku = raf.length();
  32.            while(raf.getFilePointer() < wielkoscPliku)
  33.            {
  34.                 String nazwaTowaruPlik = raf.readUTF();
  35.                 double cenaPlik = raf.readDouble();
  36.                 int liczbaSztuk = raf.readInt();
  37.                 iloscTowarow++;
  38.            }
  39.            
  40.            String[] nazwaTab = new String[iloscTowarow];
  41.            double[] cenaTab = new double[iloscTowarow];
  42.            int[] lSztukTab = new int[iloscTowarow];
  43.            
  44.            int i = 0;
  45.            while(raf.getFilePointer() < wielkoscPliku)
  46.            {
  47.             nazwaTab[i] = raf.readUTF();
  48.             cenaTab[i] = raf.readDouble();
  49.             lSztukTab[i] = raf.readInt();
  50.             i++;
  51.            }
  52.            
  53.            for(int j = 0; j<iloscTowarow; j++)
  54.            {
  55.                if(lSztukTab[j]>=100)
  56.                    cenaTab[j] = cena*0.90;
  57.                if(nazwaTowaru == nazwaTab[j])
  58.                    cena = cenaTab[j];
  59.            }
  60.            
  61.            raf.setLength(0);
  62.            for(int k = 0; k<iloscTowarow; k++)
  63.            {
  64.                raf.writeUTF(nazwaTab[k]);
  65.                raf.writeDouble(cenaTab[k]);
  66.                raf.writeInt(lSztukTab[k]);
  67.            }
  68.        }
  69.        catch(Exception e)
  70.        { System.out.println("Wyjątek: " + e.getLocalizedMessage()); }
  71.        return cena;
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement