5
\$\begingroup\$

GetBootstrap v2.7.0 Update from GetBootstrap v2.5

I add a progress bar feature in my console application customizer and update the #region grouping and convert the switch statement to Dictionary that suggested in the last version.

v2.7.0

For now I need help in reviewing my codes in ProgressBar.cs. You can also review my source code and contribute in my GitHub if you want. DOWNLOAD GetBootstrap v2.7.0

My objective is to create a progress bar that can be modify to 4 different size (small, normal, medium, and large) and change color.

This is the best code I can make. Any Suggestion?

ProgressBar.cs

public class ProgressBar
{
    public int Max { get; set; }
    public int Value { get; set; }
    public ConsoleColor Color { get; set; }
    public ProgressBarSize Size { get; set; }
    float _Calculation { get; set; }

    public ProgressBar()
    {
        Max = 100;
        Value = 0;
        Color = ConsoleColor.Blue;
        Size = ProgressBarSize.Normal;
    }

    public void Increment(int value)
    {
        Value += value;

        if (Value <= Max)
        {  
            SetUpProgressBar();

            WriteProgressBar();
        }

    }

    private void WriteProgressBar()
    {
        ProgressBarSize size = Size;
        object valueSize = Convert.ChangeType(size, size.GetTypeCode());

        int position = 1;
        for (int i = 0; i <= _Calculation * Value; i++)
        {
            Console.BackgroundColor = Color;
            Console.CursorLeft = position++;
            Console.Write(" ");
        }

        for (int i = Value; i <= (int)valueSize + 1; i++)
        {
            Console.BackgroundColor = ConsoleColor.Gray;
            Console.CursorLeft = position++;
            Console.Write(" ");
        }

        WriteProgressBarStatus((int)valueSize);
    }

    private void WriteProgressBarStatus(int p)
    {
        Console.CursorLeft = p + 5;
        Console.ResetColor();
        Console.Write(Value + " of " + Max);
    }

    private void SetUpProgressBar()
    {
        ProgressBarSize size = Size;
        object valueSize = Convert.ChangeType(size , size.GetTypeCode());

        Console.CursorLeft = 0;
        Console.Write("|");
        Console.CursorLeft = (int)valueSize + 2;
        Console.Write("|");
        Console.CursorLeft = 1;
        _Calculation = ((int)valueSize + 0.0f) / Max;
    }
}

public enum ProgressBarSize
{
    Small = 20,
    Normal = 30,
    Medium = 40,
    Large = 60
}
\$\endgroup\$
4
  • \$\begingroup\$ It looks like this project isn't available anymore? I think this was a good effort, any thoughts on why you aren't sharing this any longer? \$\endgroup\$ Jan 25, 2016 at 13:59
  • 1
    \$\begingroup\$ @YvesSchelpe I am sad to say that I lost the source code of this version, but I have plan to rebuild this soon. \$\endgroup\$ Jan 26, 2016 at 6:18
  • 1
    \$\begingroup\$ oh ok, thx - be sure to post it on git or any other site :) \$\endgroup\$ Jan 28, 2016 at 8:10
  • \$\begingroup\$ @YvesSchelpe I just updated the link. \$\endgroup\$ Jan 17, 2018 at 7:06

2 Answers 2

3
\$\begingroup\$

It is normally a good idea to separate data from behavior as this way we follow SOLID guidelines.

I recommend creating a ProgressBar object and maybe a ProgressBarOperationService or the like with the behavior.

The ProgressBarOperationService would receive the ProgressBar that needs the operation in all its methods.

\$\endgroup\$
3
\$\begingroup\$

I think what you need here is a Constructor that assigns values to private properties.

public int Max { get; set; }
public int Value { get; set; }
public ConsoleColor Color { get; set; }
public ProgressBarSize Size { get; set; }
float _Calculation { get; set; }

public ProgressBar()
{
    Max = 100;
    Value = 0;
    Color = ConsoleColor.Blue;
    Size = ProgressBarSize.Normal;
}

Your Max variable shouldn't be public, and should be a constant variable. assuming that you are creating this progress bar from scratch you want 0 as the min and 100 as the max meaning 0% and 100% but you don't have a Min you have Value and that is fine.


Another thing is your structure for this class.

Personally I would only have 3 outward facing methods

  1. Constructor
  2. Start
  3. Stop

and then some Events

  1. On_Start
  2. On_Stop
  3. On_Finish
  4. {Some Event based on progression}
\$\endgroup\$
2
  • \$\begingroup\$ Hmn... if i set the Maxto private the user will not able to change it's value? -"The objective of the ProgressBar is to get the count the process and increment every process that finished." \$\endgroup\$ Jan 13, 2015 at 23:33
  • \$\begingroup\$ you shouldn't Create the Progress bar until you know what you are tracking the progress of, so you should just pass that value in through the constructor, that is the right way to do it. \$\endgroup\$
    – Malachi
    Jan 15, 2015 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.