Skip to content

Instantly share code, notes, and snippets.

@ProjectMoon
Created September 18, 2012 19:01
Show Gist options
  • Save ProjectMoon/3745095 to your computer and use it in GitHub Desktop.
Save ProjectMoon/3745095 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using CRBM2.Domain.Core;
using System.Linq;
using Framework.Serialization;
using CRBM2.Domain.Prepaid;
using System.Runtime.Serialization;
namespace CRBM2.Domain.Prepaid
{
[Serializable]
public class PrepaidReseller : Reseller, ISerializable, IEquatable<Reseller>
{
public DateTime StartupDate { get; set; }
public PrepaidContactInformation PrepaidContactInformation { get; set; }
public decimal? InternationalFlatRate { get; set; }
public string CustomerAccountNumberFormat { get; set; }
public SmsBlastInfo SmsBlastConfig { get; set; }
public DepletedBalance DepletedBalanceConfig { get; set; }
public string CustomReceiptMessage { get; set; }
public PrepaidReseller()
{
StartupDate = new DateTime();
PrepaidContactInformation = new PrepaidContactInformation();
SmsBlastConfig = new SmsBlastInfo();
DepletedBalanceConfig = new DepletedBalance();
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
SerializationWriter writer = new SerializationWriter();
if (InternationalFlatRate != null) writer.Write(InternationalFlatRate.Value);
else writer.Write(0);
writer.Write(CustomerAccountNumberFormat);
writer.Write(CustomReceiptMessage);
info.AddValue("data2", writer.ToArray());
info.AddValue("StartupDate", StartupDate);
info.AddValue("PrepaidContactInformation", PrepaidContactInformation);
info.AddValue("SmsBlastConfig", SmsBlastConfig);
info.AddValue("DepletedBalanceConfig", DepletedBalanceConfig);
}
protected PrepaidReseller(SerializationInfo info, StreamingContext context):base(info, context)
{
SerializationReader reader = new SerializationReader((byte[])info.GetValue("data2", typeof(byte[])));
InternationalFlatRate = reader.ReadDecimal();
if (InternationalFlatRate == 0) InternationalFlatRate = null;
CustomerAccountNumberFormat = reader.ReadString();
CustomReceiptMessage = reader.ReadString();
StartupDate = (DateTime)info.GetValue("StartupDate", typeof(DateTime));
PrepaidContactInformation = (PrepaidContactInformation)info.GetValue("PrepaidContactInformation", typeof(PrepaidContactInformation));
SmsBlastConfig = (SmsBlastInfo)info.GetValue("SmsBlastConfig", typeof(SmsBlastInfo));
DepletedBalanceConfig = (DepletedBalance)info.GetValue("DepletedBalanceConfig", typeof(DepletedBalance));
}
/// <summary>
/// The customer account number format string, minus the ending digit (number of static starting digits).
/// </summary>
public string TemplateString
{
get
{
char[] output = CustomerAccountNumberFormat.ToCharArray();
return new string(output).Remove(output.Count() - 1);
}
}
/// <summary>
/// The number of digits at the start of the customer account number string to keep static.
/// </summary>
public int StaticNum
{
get
{
char[] output = CustomerAccountNumberFormat.ToCharArray();
return int.Parse(output[output.Count() - 1].ToString());
}
}
/// <summary>
/// Method to generate a random customer number that is based on the CustomerAccountNumberFormat string
/// </summary>
/// <returns>a randomly generated string</returns>
public string CreateCustomerNumber()
{
int random;
Random rand = new Random();
char[] input = CustomerAccountNumberFormat.ToCharArray();
int freeze = this.StaticNum; //gets the number in which to make first numbers 1-5 static
string output = "";
if (input.Count() == 1)
input = ("5555555555" + freeze.ToString()).ToCharArray(); //if nothing has been typed the default is a 10-digit number string
for (int i = 0; i < input.Count(); i++)
{
if (i < freeze)
{
output += input[i]; //if there is a value in freeze then the number is copied into string unchanged
}
else
{
int AsciiVal = (int)input[i];
if (int.TryParse(input[i].ToString(), out random) == true) //it's a number generate a random number
{
random = rand.Next(0, 10);
output += ((char)(random + 48)).ToString();
}
else if (((AsciiVal >= 65) && (AsciiVal <= 90)) || ((AsciiVal >= 97) && (AsciiVal <= 122))) // It's a Letter generate a random letter
{
random = rand.Next(0, 26);
char outchar;
if (AsciiVal <= 90)
outchar = (char)('A' + random);
else
outchar = (char)('a' + random);
output += outchar.ToString();
}
else
output += input[i]; //It's a Special character do nothing
}
}
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment