Advertisement
fcamuso

Untitled

Sep 3rd, 2020
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2.  
  3. namespace OOP_7_pswGenerator
  4. {
  5.   class MyRandom
  6.   {
  7.     private Random r = new Random();
  8.    
  9.     public MyRandom() { Console.WriteLine("oggetto MyRandom creato"); }
  10.     public int Next(int min, int max) => r.Next(min, max);
  11.   }
  12.   class GeneraPsw
  13.   {
  14.  
  15.     //private static readonly Random r = new Random();
  16.     private readonly MyRandom r = new MyRandom();
  17.  
  18.     private int lunghezzaMinima = 3;
  19.     public int LunghezzaMinima
  20.     {
  21.       get => lunghezzaMinima;
  22.       set
  23.       {
  24.         if (value < 3)
  25.           throw new ArgumentOutOfRangeException("Almeno 3 caratteri");
  26.         else
  27.           lunghezzaMinima = value;
  28.       }
  29.     }
  30.  
  31.     private string caratteriValidi = "abcdefghiABCDEFGHI1234567890!-_$";
  32.     public string CaratteriValidi
  33.     {
  34.       get => caratteriValidi;
  35.       set
  36.       {
  37.         if (value.Length<2)
  38.           throw new ArgumentOutOfRangeException("Almeno 2 carattere tra cui scegliere");
  39.         else
  40.           caratteriValidi = value;
  41.       }
  42.     }
  43.  
  44.     public string NuovaPsw(int lunghezza = 0)
  45.     {
  46.       if (lunghezza == 0) lunghezza = lunghezzaMinima;
  47.  
  48.       if (lunghezza<LunghezzaMinima) throw new ArgumentOutOfRangeException($"Almeno {lunghezzaMinima} caratteri");
  49.       string risultato = "";
  50.  
  51.       for (int i = 0; i < lunghezza; i++)
  52.         risultato += caratteriValidi[r.Next(0, caratteriValidi.Length)];
  53.  
  54.  
  55.       return risultato;
  56.     }
  57.   }
  58.  
  59.   class Program
  60.   {
  61.     static void Main(string[] args)
  62.     {
  63.  
  64.       GeneraPsw[] genera = new GeneraPsw[5];
  65.      
  66.       for (int i=0; i<5; i++)
  67.         genera[i] = new GeneraPsw() { LunghezzaMinima=3+i};
  68.  
  69.       Console.WriteLine(genera[3].NuovaPsw(10));
  70.  
  71.       //Math.PI
  72.       //Math m = new Math();
  73.       //m.PI
  74.     }
  75.   }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement