-1

I have to make a string which consists a string like - AAA0009, and once it reaches AAA0009, it will generate AA0010 to AAA0019 and so on.... till AAA9999 and when it will reach to AAA9999, it will give AAB0000 to AAB9999 and so on till ZZZ9999.

I want to use static class and static variables so that it can auto increment by itself on every hit.

I have tried some but not even close, so help me out thanks.

Thanks for being instructive I was trying as I Said already but anyways you already want to put negatives over there without even knowing the thing:

Code:

 public class GenerateTicketNumber
    {
        private static int num1 = 0;
        public static string ToBase36()
        {

            const string base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var sb = new StringBuilder(9);
            do
            {
                sb.Insert(0, base36[(byte)(num1 % 36)]);
                num1 /= 36;
            } while (num1 != 0);

            var paddedString = "#T" + sb.ToString().PadLeft(8, '0');
            num1 = num1 + 1;
            return paddedString;
        }
    }

above is the code. this will generate a sequence but not the way I want anyways will use it and thanks for help.

raghav
  • 185
  • 1
  • 2
  • 13
  • 5
    Can you post some code from your attempt? – Nisarg Shah Sep 26 '17 at 05:19
  • 4
    [so] is *not* a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour]. If you have indeed tried something, post your code. How can we help you with code without any? – ProgrammingLlama Sep 26 '17 at 05:20
  • Also, [Google](https://www.google.com) is your friend. See [this](https://stackoverflow.com/questions/38209985/alphanumeric-increment-algorithm-in-java) for example. It's Java, but you could convert it easily. I found it with Google. Try it sometime. – ProgrammingLlama Sep 26 '17 at 05:26
  • @raghav is this only for a simple application? You sure you don't want to keep track of your running ticket value using a database? – jegtugado Sep 26 '17 at 05:43
  • @Ephraim ya I want to keep track and also want to keep the last sequence as if someone cancel the ticket then also sequence will get incremented which is quite an issue,I am working on it. – raghav Sep 26 '17 at 05:47

3 Answers3

2

Though there's already an accepted answer, I would like to share this one.

P.S. I do not claim that this is the best approach, but in my previous work we made something similar using Azure Table Storage which is a no sql database (FYI) and it works.

1.) Create a table to store your running ticket number.

public class TicketNumber
{
    public string Type { get; set; } // Maybe you want to have different types of ticket?
    public string AlphaPrefix { get; set; }
    public string NumericPrefix { get; set; }

    public TicketNumber()
    {
        this.AlphaPrefix = "AAA";
        this.NumericPrefix = "0001";
    }

    public void Increment()
    {
        int num = int.Parse(this.NumericPrefix);

        if (num + 1 >= 9999)
        {
            num = 1;

            int i = 2; // We are assuming that there are only 3 characters
            bool isMax = this.AlphaPrefix == "ZZZ";

            if (isMax)
            {
                this.AlphaPrefix = "AAA"; // reset
            }
            else
            {
                while (this.AlphaPrefix[i] == 'Z')
                {
                    i--;
                }

                char iChar = this.AlphaPrefix[i];

                StringBuilder sb = new StringBuilder(this.AlphaPrefix);

                sb[i] = (char)(iChar + 1);

                this.AlphaPrefix = sb.ToString();
            }
        }
        else
        {
            num++;
        }

        this.NumericPrefix = num.ToString().PadLeft(4, '0');
    }

    public override string ToString()
    {
        return this.AlphaPrefix + this.NumericPrefix;
    }
}

2.) Make sure you perform row-level locking and issue an error when it fails.

Here's an oracle syntax:

SELECT * FROM TICKETNUMBER WHERE TYPE = 'TYPE' FOR UPDATE NOWAIT;

This query locks the row and returns an error if the row is currently locked by another session.

We need this to make sure that even if you have millions of users generating a ticket number, it will not mess up the sequence.

Just make sure to save the new ticket number before you perform a COMMIT.

I forgot the MSSQL version of this but I recall using WITH (ROWLOCK) or something. Just google it.

3.) Working example:

    static void Main()
    {

        TicketNumber ticketNumber = new TicketNumber();

        ticketNumber.AlphaPrefix = "ZZZ";

        ticketNumber.NumericPrefix = "9999";

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(ticketNumber);
            ticketNumber.Increment();
        }

        Console.Read();
    }

Output:

enter image description here

jegtugado
  • 5,081
  • 1
  • 12
  • 35
1

Here's an example of how you could go about implementing it

void Main()
{
    string template = @"AAAA00";

    var templateChars = template.ToCharArray();

    for (int i = 0; i < 100000; i++)
    {
        templateChars = IncrementCharArray(templateChars);
        Console.WriteLine(string.Join("",templateChars ));
    }
}

public static char Increment(char val)
{
    if(val == '9') return 'A';
    if(val == 'Z') return '0';

    return ++val;
}

public static char[] IncrementCharArray(char[] val)
{
    if (val.All(chr => chr == 'Z'))
    {
        var newArray = new char[val.Length + 1];
        for (int i = 0; i < newArray.Length; i++)
        {
            newArray[i] = '0';
        }

        return newArray;
    }

    int length = val.Length;

    while (length > -1)
    {
        char lastVal = val[--length];

        val[length] = Increment(lastVal);

        if ( val[length] != '0') break;
    }

    return val;
}
Aydin
  • 15,016
  • 4
  • 32
  • 42
1

Looking at your code that you've provided, it seems that you're backing this with a number and just want to convert that to a more user-friendly text representation.

You could try something like this:

private static string ValueToId(int value)
{
    var parts = new List<string>();
    int numberPart = value % 10000;
    parts.Add(numberPart.ToString("0000"));
    value /= 10000;

    for (int i = 0; i < 3 || value > 0; ++i)
    {
        parts.Add(((char)(65 + (value % 26))).ToString());
        value /= 26;
    }

    return string.Join(string.Empty, parts.AsEnumerable().Reverse().ToArray());
}

It will take the first 4 characters and use them as is, and then for the remainder of the value if will convert it into characters A-Z.

So 9999 becomes AAA9999, 10000 becomes AAB0000, and 270000 becomes ABB0000.

If the number is big enough that it exceeds 3 characters, it will add more letters at the start.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86