Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace OOP_7_pswGenerator
- {
- class MyRandom
- {
- private Random r = new Random();
- public MyRandom() { Console.WriteLine("oggetto MyRandom creato"); }
- public int Next(int min, int max) => r.Next(min, max);
- }
- class GeneraPsw
- {
- //private static readonly Random r = new Random();
- private readonly MyRandom r = new MyRandom();
- private int lunghezzaMinima = 3;
- public int LunghezzaMinima
- {
- get => lunghezzaMinima;
- set
- {
- if (value < 3)
- throw new ArgumentOutOfRangeException("Almeno 3 caratteri");
- else
- lunghezzaMinima = value;
- }
- }
- private string caratteriValidi = "abcdefghiABCDEFGHI1234567890!-_$";
- public string CaratteriValidi
- {
- get => caratteriValidi;
- set
- {
- if (value.Length<2)
- throw new ArgumentOutOfRangeException("Almeno 2 carattere tra cui scegliere");
- else
- caratteriValidi = value;
- }
- }
- public string NuovaPsw(int lunghezza = 0)
- {
- if (lunghezza == 0) lunghezza = lunghezzaMinima;
- if (lunghezza<LunghezzaMinima) throw new ArgumentOutOfRangeException($"Almeno {lunghezzaMinima} caratteri");
- string risultato = "";
- for (int i = 0; i < lunghezza; i++)
- risultato += caratteriValidi[r.Next(0, caratteriValidi.Length)];
- return risultato;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- GeneraPsw[] genera = new GeneraPsw[5];
- for (int i=0; i<5; i++)
- genera[i] = new GeneraPsw() { LunghezzaMinima=3+i};
- Console.WriteLine(genera[3].NuovaPsw(10));
- //Math.PI
- //Math m = new Math();
- //m.PI
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement