Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Soulstorm50/d791fa97991a5e85a52ca58e93386511 to your computer and use it in GitHub Desktop.
Save Soulstorm50/d791fa97991a5e85a52ca58e93386511 to your computer and use it in GitHub Desktop.
класс-коллекцию логинов и паролей/пример с магазином
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
public class NoRecordsInCollection : Exception
{
public NoRecordsInCollection() : base() { }
}
public class LoginAlreadyAdded : Exception
{
public LoginAlreadyAdded() : base() { }
}
public class LoginNotFound : Exception
{
public LoginNotFound() : base() { }
}
public class CEncryption
{
public static string Encrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i++)
{
sbOut += String.Format("{0:00}", strIn[i] ^ strKey[i % strKey.Length]);
}
return sbOut;
}
public static string Decrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i += 2)
{
byte code = Convert.ToByte(strIn.Substring(i, 2));
sbOut += (char)(code ^ strKey[(i / 2) % strKey.Length]);
}
return sbOut;
}
}
public class LoginPasswordCollection
{
struct Record
{
public String login;
public String password;
}
private Record[] recordContainer;
public int Count
{
get
{
if (recordContainer == null)
{
return 0;
}
else
{
return recordContainer.Length;
}
}
}
public void ShowLogins()
{
for (int i = 0; i < recordContainer.Length; i++)
{
Console.WriteLine(recordContainer[i]);
}
}
public String this[int ind]
{
get
{
if (ind >= recordContainer.Length)
throw new NoRecordsInCollection();
return recordContainer[ind].login;
}
}
public String this[String login]
{
get
{
if (recordContainer.Length == 0)
throw new NoRecordsInCollection();
for (int i = 0; i < recordContainer.Length; i++)
{
if (recordContainer[i].login.Equals(login))
{
return DecryptPassword(recordContainer[i].password);
}
}
return "";
}
}
public Boolean IsLoginAdded(String login)
{
if (recordContainer == null)
return false;
for (int i = 0; i < recordContainer.Length; i++)
{
if (recordContainer[i].Equals(login))
return true;
}
return false;
}
public void AddUser(String login, String password)
{
if(recordContainer == null)
{
recordContainer = new Record[1];
recordContainer[0].login = login;
recordContainer[0].password = EncryptPassword(password);
}
else
{
if (IsLoginAdded(login))
throw new LoginAlreadyAdded();
Record[] buffer = new Record[recordContainer.Length + 1];
for (int i = 0; i < recordContainer.Length; i++)
{
buffer[i] = recordContainer[i];
}
buffer[recordContainer.Length].login = login;
buffer[recordContainer.Length].password = EncryptPassword(password);
recordContainer = buffer;
}
}
public void DeleteUserData(String login)
{
}
private String EncryptPassword(String originalPassword)
{
return CEncryption.Encrypt(originalPassword, "dotnet");
}
private String DecryptPassword(String cryptedPassword)
{
return CEncryption.Decrypt(cryptedPassword, "dotnet");
}
}
}
////////////////////////////////////////////////////////
namespace ConsoleApplication14
{
public abstract class Device
{
public Device(String category)
{
Category = category;
}
public Double Price
{
get;
set;
}
public String Vendor
{
get;
set;
}
public String Category
{
get;
}
public DateTime ProduceYear
{
get;
set;
}
public Boolean Warranty
{
get;
set;
}
public String Model
{
get;
set;
}
}
public class Laptop : Device
{
public Laptop() : base("Laptop") { }
}
public class Recharge : Device
{
public Recharge() : base("Recharge") { }
}
public class Tablet : Device
{
public Tablet() : base("Tablet") { }
}
public class MobilePhone : Device
{
public MobilePhone() : base("MobilePhone")
{
}
}
public class Cover : Device
{
public Cover() : base("Cover") { }
}
public class NoMorePlaceInStore : Exception
{
public NoMorePlaceInStore() : base() { }
}
public class Store
{
private Device[] devicesInStore;
private int count;
public Store(uint size)
{
devicesInStore = new Device[size];
}
public int Size
{
get { return devicesInStore.Length; }
}
public int Count
{
get { return count; }
}
public void addDevice(Device newDevice)
{
if (Count == Size)
{
throw new NoMorePlaceInStore();
}
devicesInStore[Count] = newDevice;
count++;
}
public void findByPrice(int minPrice, int maxPrice)
{
for (int i = 0; i < Count; i++)
{
if (devicesInStore[i].Price >= minPrice && devicesInStore[i].Price <= maxPrice)
{
showDevice(devicesInStore[i]);
}
}
}
public Device this[String model]
{
get
{
for (int i = 0; i < Count; i++)
{
if (devicesInStore[i].Model.Equals(model))
{
return devicesInStore[i];
}
}
return null;
}
}
public Device this[DateTime produceYear]
{
get
{
for (int i = 0; i < Count; i++)
{
if (devicesInStore[i].ProduceYear == produceYear)
{
return devicesInStore[i];
}
}
return null;
}
}
public void findByCategory(String category)
{
for (int i = 0; i < Count; i++)
{
if (devicesInStore[i].Category == category)
{
showDevice(devicesInStore[i]);
}
}
}
private void showDevice(Device dev)
{
Console.WriteLine(dev.Model + " | " + dev.Price);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment