martinkotevski

APS / Lab 6 / Problem 1 - Passwords

Nov 23rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * Lab 6 / Problem 1 - Passwords
  7.  * Potrebno e da se simulira najava na eden sistem. Pritoa korisnikot vnesuva
  8.  * korisnicko ime i lozinka. Dokolku korisnickoto ime odgovara so lozinkata
  9.  * togas se pecati "Najaven", dokoklu ne, se pecati "Nenajaven" i na
  10.  * kotisnikot mu se dava povtorno sansa da vnese korisnicko ime i lozinka.
  11.  * Vo momentot koga korisnikot ke bide najaven prestanuvaat obidite za najava.
  12.  *
  13.  * Vlez: Prvo se dava broj N na korisnicki iminja i lozinki koi ke bidat
  14.  * vneseni vo sistemot. Vo narednite N reda se dadeni korisnickite iminja
  15.  * i lozinki razdeleni so edno prazno mesto.
  16.  * Potoa se davaat redovi so korisnicki iminja i lozinki na korisnici koi se
  17.  * obiduvaat da se najavat (Pr. ana banana).
  18.  * Za oznacuvanje kraj na obidite vo redicata se dava zborot KRAJ
  19.  *
  20.  * Izlez: Za sekoj od vlezovite koi se obid za najava se pecati "Nenajaven"
  21.  * se dodeka ne dobieme "Najaven" ili dodeka imame obidi za najava.
  22.  *
  23.  * Zabeleska: Rabotete so hash tabela so zatvoreni koficki, sami resavate za
  24.  * goleminata na hash tabelata
  25.  */
  26. public class Lozinki {
  27.     public static void main (String[] args) throws IOException {
  28.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  29.         int N = Integer.parseInt(br.readLine());
  30.         ClosedBucketHashTable<String, String> table = new ClosedBucketHashTable<>(51);
  31.  
  32.         for(int i=1;i<=N;i++){
  33.             String imelozinka = br.readLine();
  34.             String[] parts = imelozinka.split(" ");
  35.             table.insert(parts[0], parts[1]);
  36.         }
  37.  
  38.         String line;
  39.         while (!(line = br.readLine()).equals("KRAJ")) {
  40.             String[] parts = line.split("\\s+");
  41.             String username = parts[0];
  42.             String tryPass = parts[1];
  43.             String pass = table.search(username);
  44.             if (pass != null&&pass.equals(tryPass)) {
  45.                 System.out.println("Najaven");
  46.                 return;
  47.             }
  48.             System.out.println("Nenajaven");
  49.         }
  50.     }
  51. }
  52.  
  53. class ClosedBucketHashTable<KEY extends Comparable<KEY>, VALUE> {
  54.     private SLNode<Entry<KEY, VALUE>>[] buckets;
  55.     private int capacity; // hash table capacity
  56.     private int count; // number of key-value pairs
  57.  
  58.     @SuppressWarnings("unchecked")
  59.     public ClosedBucketHashTable(int m) {
  60.         buckets = (SLNode<Entry<KEY, VALUE>>[]) new SLNode[m];
  61.         capacity = m;
  62.         count = 0;
  63.     }
  64.  
  65.     public int size() {
  66.         return count;
  67.     }
  68.  
  69.     public boolean isEmpty() {
  70.         return size() == 0;
  71.     }
  72.  
  73.     public int hash(KEY key) {
  74.         char[] letters = ((String) key).toCharArray();
  75.         int sum = 0;
  76.         for (char letter : letters)
  77.             sum += (Character.toUpperCase(letter) - 'A');
  78.         return sum % capacity;
  79.     }
  80.  
  81.     public VALUE search(KEY key) {
  82.         if (key  == null)
  83.             throw new IllegalArgumentException("null argument");
  84.         int i = hash(key);
  85.         SLNode<Entry<KEY, VALUE>> node = buckets[i];
  86.         if (node == null)
  87.             return null;
  88.         while (node != null) {
  89.             Entry<KEY, VALUE> entry = node.data;
  90.             if (entry.key.equals(key))
  91.                 return entry.value;
  92.             node = node.link;
  93.         }
  94.         return null;
  95.     }
  96.  
  97.     public void insert(KEY key, VALUE value) {
  98.         if (key == null || value == null)
  99.             throw new IllegalArgumentException("null argument");
  100.         // TODO implement resizable (if count >= capacity)
  101.         Entry<KEY, VALUE> newEntry = new Entry<>(key, value);
  102.         int i = hash(key);
  103.         SLNode<Entry<KEY, VALUE>> node = buckets[i];
  104.         while (node != null) {
  105.             if (node.data.equals(newEntry)) {
  106.                 node.data = newEntry;
  107.                 return;
  108.             }
  109.             node = node.link;
  110.         }
  111.         buckets[i] = new SLNode<>(newEntry, buckets[i]);
  112.         count++;
  113.     }
  114.  
  115.     public boolean delete(KEY key) {
  116.         if (key  == null)
  117.             throw new IllegalArgumentException("null argument");
  118.         int i = hash(key);
  119.         if (i < 0 || i >= capacity)
  120.             return false;
  121.         SLNode<Entry<KEY, VALUE>> node = buckets[i];
  122.         if (node.data.key.equals(key)) {
  123.             node = null;
  124.             count--;
  125.         }
  126.         while (node.link != null) {
  127.             if (node.link.data.key.equals(key)) {
  128.                 node.link = node.link.link;
  129.                 count--;
  130.                 return true;
  131.             }
  132.             node = node.link;
  133.         }
  134.         return false;
  135.     }
  136.  
  137.     public String toString() {
  138.         StringBuilder sb = new StringBuilder();
  139.         for (SLNode<Entry<KEY, VALUE>> node : buckets) {
  140.             if (node != null) {
  141.                 while (node != null) {
  142.                     sb.append(node.data);
  143.                     node = node.link;
  144.                 }
  145.             } else
  146.                 sb.append("null");
  147.             sb.append("\n");
  148.         }
  149.         return sb.toString();
  150.     }
  151. }
  152.  
  153. class Entry<KEY extends Comparable<KEY>, VALUE> {
  154.     public KEY key;
  155.     public VALUE value;
  156.  
  157.     public Entry(KEY key, VALUE value) {
  158.         this.key = key;
  159.         this.value = value;
  160.     }
  161.  
  162.     public String toString() {
  163.         return String.format("<%s, %s>", key.toString(), value.toString());
  164.     }
  165.  
  166.     public boolean equals(Entry<KEY, VALUE> other) {
  167.         return this.key.equals(other.key);
  168.     }
  169.  
  170. }
  171.  
  172. class SLNode<E> {
  173.     public E data;
  174.     public SLNode<E> link;
  175.  
  176.     public SLNode(E data, SLNode<E> link) {
  177.         this.data = data;
  178.         this.link = link;
  179.     }
  180. }
Add Comment
Please, Sign In to add comment