Click here to Skip to main content
15,908,909 members
Home / Discussions / C#
   

C#

 
GeneralRe: Application service with C#. Pin
Pete O'Hanlon16-Oct-18 23:59
mvePete O'Hanlon16-Oct-18 23:59 
AnswerRe: Application service with C#. Pin
#realJSOP22-Oct-18 9:03
professional#realJSOP22-Oct-18 9:03 
QuestionHow can I make my code modular? Pin
George Bradley16-Oct-18 12:43
George Bradley16-Oct-18 12:43 
AnswerRe: How can I make my code modular? Pin
V.16-Oct-18 18:44
professionalV.16-Oct-18 18:44 
GeneralRe: How can I make my code modular? Pin
henrydevid127-Oct-18 11:56
henrydevid127-Oct-18 11:56 
AnswerRe: How can I make my code modular? Pin
#realJSOP24-Oct-18 1:42
professional#realJSOP24-Oct-18 1:42 
AnswerRe: How can I make my code modular? Pin
josda100026-Oct-18 18:29
josda100026-Oct-18 18:29 
GeneralRe: How can I make my code modular? Pin
josda100027-Oct-18 2:48
josda100027-Oct-18 2:48 
It was late last night when I was coding that up, but I noticed a lot that could be broken down into that subclass. I also agree with earlier responses... some of this calculation logic could really be in another project altogether, separating the algorithm from the UI wiring. However, the original question asked how to make it modular... I believe that question has to do with the repetition in control logic. I assume the controls look like they're in a grid, having three columns or rows, each of which containing StartTimes, EndTimes, and the like. That's what the subclass was for.

I worked a lot more on the logic this morning, and this is what I have now:

C#
public class UIForm
{
    public class TimeSet
    {
        private TextBox _txtAllottedTime;
        private TextBox _txtStartTime;
        private TextBox _txtEndTime;
        private Label _lblOverTime;
        private Label _lblOverTimePercentage;
        private int _overTime;
        private int _completeTime;

        public TimeSpan AllottedTime =>
            TimeSpan.Parse(_txtAllottedTime.Text);
        private DateTime StartTime =>
            DateTime.Parse(_txtStartTime.Text);
        private DateTime EndTime =>
            DateTime.Parse(_txtEndTime.Text);

        public TimeSet(
            TextBox txtAllottedTime,
            TextBox txtStartTime,
            TextBox txtEndTime,
            Label lblOverTime,
            Label lblOverTimePercentage,
            int overTime,
            int completeTime)
        {
            _txtAllottedTime = txtAllottedTime;
            _txtStartTime = txtStartTime;
            _txtEndTime = txtEndTime;
            _lblOverTime = lblOverTime;
            _lblOverTimePercentage = lblOverTimePercentage;
            OverTime = overTime;
            _completeTime = completeTime;
        }

        public int OverTime
        {
            get => _overTime;
            private set
            {
                _overTime = value;
                _lblOverTime.Text = value.ToString();
            }
        }

        public bool TextWasEnteredByUser =>
            string.IsNullOrEmpty(AllottedTime.Text) ||
            string.IsNullOrEmpty(StartTime.Text) ||
            string.IsNullOrEmpty(EndTime.Text);

        public TimeSpan Duration
        {
            get
            {
                DateTime end = EndTime;

                if (StartTime > end)
                {
                    end = end.AddDays(1);
                }

                return end.Subtract(StartTime);
            }
        }

        private int Percentage
        {
            get
            {
                _completeTime++;
                int percentage = (OverTime * 100) / _completeTime;
                _lblOverTimePercentage.Text = percentage.ToString();
                return percentage;
            }
        }

        public bool ShouldWarnWarden() =>
            (Percentage > 50);

        public void CheckForOverTime()
        {
            //If the start time is greater than end that means the diff is above 12 hours
            //So subtracting the hours from a day to get the number of hours used if (TimeSpan.Compare(alloted, duration) == -1)
            if (TimeSpan.Compare(AllottedTime, Duration) == -1)
            {
                OverTime++;
            }
        }
    }

    private readonly List<TimeSet> TimeSets = new List<TimeSet>
    {
        // initialize parameters here.  I don't know what you want for these integers.
        new TimeSet(txtTimeLimit1, txtEntryTime1, txtExitTime1, lblOverTime1, _lblOverTimePercentage1, 0, 0),
        new TimeSet(txtTimeLimit2, txtEntryTime2, txtExitTime2, lblOverTime2, _lblOverTimePercentage2, 0, 0),
        new TimeSet(txtTimeLimit3, txtEntryTime3, txtExitTime3, lblOverTime3, _lblOverTimePercentage3, 0, 0)
    };

    public void park1() =>
        park(TimeSets[0]);

    public void park2() =>
        park(TimeSets[1]);

    public void park3() =>
        park(TimeSets[2]);

    private void park(TimeSet timeSet)
    {
        if (!timeSet.TextWasEnteredByUser)
        {
            MessageBox.Show("Enter all time values");
        }
        else
        {
            timeSet.CheckForOverTime();

            if (timeSet.ShouldWarnWarden())
            {
                warnWarden();
            }
        }
    }
}


So the idea is to break up that if/else block all the way down to a simple method, instead of having park() do all of the work. TimeSet does most of the heavy lifting, but in small chunks. It'd probably be easier to debug. But as others have stated, that percentage logic really should be in another layer altogether if you're making this a full blown program. Also if making this a full-blown program, I'd suggest making park() and its counterparts look like Park() as a matter of convention, just to keep new developers on the same page.

I hope that helps.Thumbs Up | :thumbsup:

QuestionImagelist in Tabcontrol - Image selection Pin
RAFENS15-Oct-18 23:47
RAFENS15-Oct-18 23:47 
AnswerRe: Imagelist in Tabcontrol - Image selection Pin
BillWoodruff19-Oct-18 2:49
professionalBillWoodruff19-Oct-18 2:49 
QuestionImage processing in c# Pin
Member 1401942314-Oct-18 10:51
Member 1401942314-Oct-18 10:51 
AnswerRe: Image processing in c# Pin
Dave Kreskowiak14-Oct-18 11:56
mveDave Kreskowiak14-Oct-18 11:56 
GeneralRe: Image processing in c# Pin
Member 1401942314-Oct-18 14:44
Member 1401942314-Oct-18 14:44 
GeneralRe: Image processing in c# Pin
Dave Kreskowiak14-Oct-18 18:28
mveDave Kreskowiak14-Oct-18 18:28 
QuestionUploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
PKSubudhi13-Oct-18 17:47
PKSubudhi13-Oct-18 17:47 
AnswerRe: Uploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
OriginalGriff13-Oct-18 19:56
mveOriginalGriff13-Oct-18 19:56 
AnswerRe: Uploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
Mycroft Holmes14-Oct-18 13:04
professionalMycroft Holmes14-Oct-18 13:04 
QuestionHow can we send SMS in C#.NET to any mobile number without API Gateway? Pin
PKSubudhi13-Oct-18 19:53
PKSubudhi13-Oct-18 19:53 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
Richard MacCutchan13-Oct-18 21:24
mveRichard MacCutchan13-Oct-18 21:24 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
Dave Kreskowiak14-Oct-18 4:53
mveDave Kreskowiak14-Oct-18 4:53 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
MadMyche15-Oct-18 6:25
professionalMadMyche15-Oct-18 6:25 
GeneralRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
MadMyche1-Aug-19 5:35
professionalMadMyche1-Aug-19 5:35 
QuestionaForge.net question Pin
Member 1345929011-Oct-18 4:46
Member 1345929011-Oct-18 4:46 
AnswerRe: aForge.net question Pin
Bernhard Hiller11-Oct-18 21:39
Bernhard Hiller11-Oct-18 21:39 
QuestionHow to connect a mobile phone camera into asp.net c# webapplication without using a usb cable. Pin
Member 119849599-Oct-18 0:27
Member 119849599-Oct-18 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.