Building a C# application - Cash register - Part 3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    Building a C# application - Cash register - Part 3

    UNDER CONSTRUCTION

    Upgrades and fixes.

    Our Keypad doesn't have a decimal point. Let's add one.
    • Resize '00' to a single button width.
    • Move '0' to the left one column
    • Duplicate the '0' and make it btnPoint with text of a period.
    • Double-Click btnPoint to make a handler and have it raise our event, sending a period.

    [IMGNOTHUMB]http://files.me.com/tlhintoq/iqznjq[/IMGNOTHUMB]
    Code:
            private void btnPoint_Click(object sender, EventArgs e)
            {
                RaiseButtonPressed('.');
            }
    Our [Enter] sends an 'E' instead of a proper [Enter]. That was fine for testing in step 1, but not any longer.
    Code:
            private void btnEnter_Click(object sender, EventArgs e)
            {
                RaiseButtonPressed((char)Keys.Enter);
            }
    A cash register might need to enter a SKU or a price. We need buttons for those. Add them in the same way we did the others, giving them names of btnSKU and btnPrice.
    [IMGNOTHUMB]http://files.me.com/tlhintoq/no748h[/IMGNOTHUMB]
    They will raise values of 's' and 'p' respectively.
    Code:
            private void btnSKU_Click(object sender, EventArgs e)
            {
                RaiseButtonPressed('s');
            }
    
            private void btnPrice_Click(object sender, EventArgs e)
            {
                RaiseButtonPressed('p');
            }
    Now our Form1 needs to not just react blindly as if all received keys are the same. The enter key is not exactly something you can just type. But it has a numeric value of 13. So we define a constant to represent it, to make our coding easier to read. Then we switch to different lines of code depending on what key we received from our Keypad.
    Code:
    private const char ENTER = (char) 13;
    
    private void registerKeypad1_ButtonPressed(object sender, KeyPressEventArgs e)
    {
        Char incoming = e.KeyChar;
        switch (incoming)
        {
            case 's':
                break;
            case ENTER:
                break;
            case '+':
                break;
            case '-':
                break;
            default:
                // If we fall through this far then it must be a number or decimal point
                break;
        }
    }
    Its time to decide on some behavior rules for our register. Does the clerk have to hit 'SKU' or 'Price' before every entry? Is every entry automatically a SKU unless 'Price' was hit first? Etc.

    I'm thinking that the less keystrokes the faster the customer is served and the easier for the clerk.
    [Price] means all entries are price until [SKU] is hit.
    [SKU] means all entries are sku until [Price] is hit.
    So a clerk can hit...
    [SKU]
    123 [Enter]
    987 [Enter]
    456 [Enter]
    [Price]
    10.50 [Enter] For an item with no SKU
    [SKU]
    continue ringing up.

    So we need a small ENUM to track our mode of entering.
    Code:
            enum EnteringMode
            {
                SKU,
                PRICE
            }
    And a variable to hold our instance of this ENUM
    Code:
            private EnteringMode CurrentMode = EnteringMode.SKU;
    When the clerk hits the [SKU] key which switch mode. Same with the [Price] key.
    Code:
                    case 's':
                        CurrentMode = EnteringMode.SKU;
                        break;
                    case 'p':
                        CurrentMode = EnteringMode.PRICE;
                        break;


    In order to keep a running total we need to add a couple variables: NewValue and RunningTotal. Plus we know that when a register has a new value entered, that is added to the running total and the clerk's screen is cleared in anticipation of the next new value.
    Code:
            private decimal RunningTotal = 0;
            private decimal NewValue
            { 
                set
                {
                        RunningTotal = RunningTotal + (value);
                        textBox1.Text = "0";
                }
            }
    Think back to basic math: Subtracting is really just adding a negative version of your value. 1 times a value is the original value. -1 times a value is the negative value. So we need to keep track of a sign for the entered value.
    Code:
    private int Sign = 1;
    So when we hit "+" we want to send the current value in the clerk's textbox to the NewValue, that will in turn add it to the running total, clear the clerk's textbox and set the sign for the next value to positive.
    Code:
    case '+':
        NewValue = Sign * Convert.ToDecimal(textBox1.Text);
        Sign = 1;
        break;
    Hitting '-' does nearly the same thing, except it sets the sign for the next entered value to negative
    Code:
    case '-':
        NewValue = Sign * Convert.ToDecimal(textBox1.Text);
        Sign = -1;
        break;
Working...