a little console app i wrote

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

Ok, here's an attempt at something. I figure I can use this to let me
know when my laundry's done! :)

I'm hoping you guys can spot ways to make it better/cleaner/more
efficient, etc. especially where the math is involved. Thanks.



using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
int iDelayMS = iDelay * 60 * 1000;
Thread.Sleep(iDelayMS);
Console.WriteLine("\a");
Console.WriteLine(iDelay + " minutes have elapsed.");
}
}
 
It's fine; my suggested improvements are very minor and not really worth the
effort for such a trivial app:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed",iDelay);
}
}
 
Bob said:
It's fine; my suggested improvements are very minor and not really worth the
effort for such a trivial app:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed",iDelay);
}
}

I like how you've condensed the calculation though. And I especially
like the final WriteLine method. That's exactly the kind of thing I was
looking for.

Thanks.
 
John said:
I like how you've condensed the calculation though. And I especially
like the final WriteLine method. That's exactly the kind of thing I was
looking for.

Thanks.

If I change it to this:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());
Thread.Sleep(iDelay * 60000);
Console.WriteLine("\a{0} minutes have elapsed", iDelay);
Thread.Sleep(2000);
Console.WriteLine("\a");
Thread.Sleep(2000);
Console.WriteLine("\a");
}
}

is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing that.
 
John Salerno said:
is there a more efficient way of repeating the sound, other than copying
and pasting code? It seems like there's always a better way than doing that.

How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)
 
C# Learner said:
How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)

i'd prefer Console.Write("\a"); instead of Console.WriteLine("\a");
Why go to new line, if you only want to beep?
 
C# Learner said:
How about:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

Thread.Sleep(iDelay * 60000);
Beep();

Console.WriteLine("{0} minutes have elapsed", iDelay);

Thread.Sleep(2000);
Beep();

Thread.Sleep(2000);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

One step further:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

SleepThenBeep(iDelay * 60000);
Console.WriteLine("{0} minutes have elapsed", iDelay);

SleepThenBeep(2000);
SleepThenBeep(2000);
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.WriteLine("\a");
}
}

Note that there are a couple of bugs in this program (and there have been
from the start). :-)

But there's still repetition of code. I suppose for a couple of times
it's no problem, and I could use a for loop if I wanted to do something
bigger.
 
John Salerno said:
But there's still repetition of code. I suppose for a couple of times
it's no problem, and I could use a for loop if I wanted to do something
bigger.

I didn't want to change the code too much at first, but, now that you
mention it, I think it's good practise to parameterize here; e.g.:

using System;
using System.Threading;

class Alarm
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iDelay = Int32.Parse(Console.ReadLine());

const int OneMinute = 60000;
SleepThenBeep(iDelay * OneMinute);
Console.WriteLine("{0} minutes have elapsed", iDelay);

const int PostAlarmBeepCount = 2;
for (int i = 0; i < PostAlarmBeepCount; ++i)
{
const int PostAlarmBeepDelay = 2000;
SleepThenBeep(PostAlarmBeepDelay);
}
}

static void SleepThenBeep(int sleepDuration)
{
Thread.Sleep(sleepDuration);
Beep();
}

static void Beep()
{
Console.Write('\a');
}
}
 
Back
Top