Hakunin

OBP2_V5_licence

Nov 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class app {
  6.     public static Settings glavni;
  7.     public static void main(String[] args) {
  8.  
  9.         File f = new File("licence.bin");
  10.         ArrayList<String> lista_licenci = new ArrayList<>();
  11.         lista_licenci.add("AAAA-AAAA-AAAA-AAAA");
  12.         lista_licenci.add("BBBB-BBBB-BBBB-BBBB");
  13.         lista_licenci.add("CCCC-CCCC-CCCC-CCCC");
  14.  
  15.  
  16.         if(!f.exists()){
  17.             try {
  18.                 Settings def = new Settings();
  19.                 f.createNewFile();
  20.                 def.upisi("licence.bin");
  21.                 System.out.println("Datoteka kreirana!");
  22.             } catch (IOException e) {
  23.                 e.printStackTrace();
  24.             }
  25.         }else {
  26.             System.out.println("Datoteka vec postoji");
  27.             Settings cit;
  28.             cit = Settings.procitaj("licence.bin");
  29.             System.out.println(cit);
  30.         }
  31.  
  32.         System.out.println("DA LI IMATE LICENCU ZA PROGRAM?? (DA/NE)");
  33.         Scanner input = new Scanner(System.in);
  34.         input.useDelimiter("\\n");
  35.         String unos = input.next();
  36.         if(unos.equals("DA")){
  37.             String unos_licence = null;
  38.             while (!lista_licenci.contains(unos_licence)){
  39.                 System.out.println("UNESITE SVOJU LICENCU (FORMAT: XXXX-XXXX-XXXX-XXXX");
  40.                 unos_licence = input.next();
  41.             }
  42.             if(lista_licenci.contains(unos_licence)){
  43.                 System.out.println("UNESITE ZELJENO KORISNICKO IME: ");
  44.                 unos = input.next();
  45.                 Settings novi = new Settings(unos,unos_licence,15);
  46.                 novi.upisi("licence.bin");
  47.                 glavni = novi;
  48.             }
  49.         }else {
  50.             System.exit(0);
  51.         }
  52.         Nit nit = new Nit();
  53.         nit.start();
  54.     }
  55. }
  56.  
  57. ////////
  58.  
  59. /**
  60.  * Created by guest-Mt2hFp on 21.11.17..
  61.  */
  62. public class Nit extends Thread {
  63.     public void run(){
  64.         while (app.glavni.getVreme()>0){
  65.             System.out.println(app.glavni.getKorisnickoime());
  66.             try {
  67.                 this.sleep(1000);
  68.             } catch (InterruptedException e) {
  69.                 e.printStackTrace();
  70.             }
  71.             app.glavni.smanjiVreme();
  72.             app.glavni.upisi("licence.bin");
  73.         }
  74.     }
  75. }
  76.  
  77. ///////////
  78.  
  79. import java.io.*;
  80.  
  81. /**
  82.  * Created by markort2815 on 21.11.17..
  83.  */
  84. public class Settings implements Serializable {
  85.     private String korisnickoime;
  86.     private String licenca;
  87.     private int vreme;
  88.  
  89.     public Settings(String korisnickoime, String licenca, int vreme) {
  90.         this.korisnickoime = korisnickoime;
  91.         this.licenca = licenca;
  92.         this.vreme = vreme;
  93.     }
  94.  
  95.     public Settings() {
  96.         this.korisnickoime = "evaluate";
  97.         this.licenca = "0000-0000-0000-0000";
  98.         this.vreme = 1;
  99.     }
  100.  
  101.     public static Settings procitaj(String path){
  102.         try {
  103.             FileInputStream fileInputStream = new FileInputStream(path);
  104.             ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
  105.             Settings load = (Settings)objectInputStream.readObject();
  106.             fileInputStream.close();
  107.             objectInputStream.close();
  108.             return load;
  109.         }catch (IOException e){
  110.             e.printStackTrace();
  111.             return null;
  112.         }catch (ClassNotFoundException e){
  113.             e.printStackTrace();
  114.             return null;
  115.         }
  116.     }
  117.  
  118.     public void upisi(String path){
  119.         try {
  120.             FileOutputStream fileOutputStream = new FileOutputStream(path);
  121.             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
  122.             objectOutputStream.writeObject(this);
  123.             objectOutputStream.close();
  124.             fileOutputStream.close();
  125.         }catch (IOException e){
  126.             e.printStackTrace();
  127.         }
  128.     }
  129.  
  130.     @Override
  131.     public String toString() {
  132.         return "Settings{" +
  133.                 "korisnickoime='" + korisnickoime + '\'' +
  134.                 ", licenca='" + licenca + '\'' +
  135.                 ", vreme=" + vreme +
  136.                 '}';
  137.     }
  138.  
  139.     public String getLicenca() {
  140.         return licenca;
  141.     }
  142.  
  143.     public String getKorisnickoime() {
  144.         return korisnickoime;
  145.     }
  146.  
  147.     public int getVreme() {
  148.         return vreme;
  149.     }
  150.  
  151.     public void smanjiVreme(){
  152.         vreme=vreme-1;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment