Need to comapre a string and a number to find the Max Value
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need to comapre a string and a number to find the Max Value

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Need to comapre a string and a number to find the Max Value

    Hello everyone, Im working on a piecework program. I have everything working exactly the way its supposed to except for the one part where I am trying to find which employee made the most pieces. What I need to do- hopefully this makes sense, is take the input of pieces completed and employee name and at the end be able to see which employee make the most pieces and how many pieces they made. I already got the part working for how many pieces but I cant get the who made them part to work. This is for class so i dont expect answere- I have tried the Math.Max but that does not seem to work with a string- I tried setting the name field as a double but it then only gives an output if I put a number in for an employee name Below is the code

    "Include in the summary information the name and the number of pieces for the person who has processed the most pieces of all persons processed. If there are one or more ties, the first person entered who had the most pieces will remain the winner."



    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Piecework
    {
        public partial class Form1 : Form
        {
    
    
            private decimal accumTotalPieces=0;
            private decimal accumTotalPay=0;
            private decimal accumAvgPay=0;
            private string accumEmployeeWithMost;
            private decimal accumMostPiecesMade=0;
            private decimal buttonClickCounter = 0;
            private int pieces = 0;
            private string name;
    
            public Form1()
               
            {
                InitializeComponent();
            }
    
            private void exitButton_Click(object sender, EventArgs e)
            {
                this.Close();
    
            }
    
    
    
            private void clearButton_Click(object sender, EventArgs e)
            {
    
                employeeNameBox.Text = "";
                piecesCompletedBox.Text = "";
                amountEarnedBox.Text = "";
            }
    
            private void calculateButton_Click(object sender, EventArgs e)
            {
    
                buttonClickCounter++;
    
                
                decimal rate = 0;
                decimal amountEarned= 0;
    
                
    
                if (string.IsNullOrEmpty(this.employeeNameBox.Text))
                    MessageBox.Show("Invalid Name Entered (" + employeeNameBox.Text + ") Please Enter A Valid Name");
    
    
                if (!int.TryParse(piecesCompletedBox.Text, out pieces) || pieces <= 0 || pieces >= 20001)
                    MessageBox.Show("Invalid Numeric Value (" + piecesCompletedBox.Text + ") -Please enter a value between 0 and 20,000");
    
    
                if (pieces >= 1 && pieces <= 199)
                {
                   rate= 0.50m; 
                }
    
    
                if (pieces >= 200 && pieces <= 399)
                {
                   rate = 0.55m;
                }
    
    
                if (pieces >= 400 && pieces <= 599)
                {
                   rate = 0.60m;
                }
    
    
                if (pieces >= 600)
                {
                    rate = 0.65m;
                }
    
    
                amountEarned = rate * pieces;
                amountEarnedBox.Text = amountEarned.ToString("c");
    
        
    
                accumTotalPieces = accumTotalPieces + pieces;
                accumTotalPay = accumTotalPay + amountEarned;
                accumAvgPay = accumTotalPay /buttonClickCounter ;
    
    
     
    
                grandTotalPiecesBox.Text = accumTotalPieces.ToString();
                grandTotalPayBox.Text = accumTotalPay.ToString("c");
                avgPayPerEmployeeBox.Text = accumAvgPay.ToString("c");
    
              
                accumMostPiecesMade = Math.Max(accumMostPiecesMade, pieces);
                mostPiecesProducedBox.Text = accumMostPiecesMade.ToString();

    My thoughts are I need to somehow take the output from accumMostPiecesMade and pair it up with accumEmployeeWithMost


    thanks for any help/ direction!

  2. #2
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    51

    Re: Need to comapre a string and a number to find the Max Value

    cast the string to a numeric value type then...


    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  3. #3
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,363

    Re: Need to comapre a string and a number to find the Max Value

    Where are you storing each one of the employee entries? Based on your description, the form will allow the user to enter the info for more than one employee, and then you'll have a calculate button that cycles through the stored entries and decide who has the most pieces made. Looks like you've got a great start to the program, you just need to store each employee submission into a collection (List<>, array, etc.).

    To do this, create an Employee class with some properties like Name, AmountEarned, TotalPieces, PiecesMade and so on. In the forms class, create a List<Employee> _employeeList collection. Then create an AddEmployee button. This button will let the user enter a new employee entry. Take the data out of the form and then new up a Employee class instance and set the properties from the class with the data in the form. Next, call _employeeList.Add(...) to add the employee to the collection.

    When the user clicks the calculate button, use a foreach loop to cycle through the _employeeList and use the logic you've already written to determine the max user (as you cycle through the _employeeList keep a maxEmployee variable to store the employee with the highest count - use this variable later to display this employee.

    Take a look at bing or google to search for List<> to find out how to use this collection.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,363

    Re: Need to comapre a string and a number to find the Max Value

    Where are you storing each one of the employee entries? Based on your description, the form will allow the user to enter the info for more than one employee, and then you'll have a calculate button that cycles through the stored entries and decide who has the most pieces made. Looks like you've got a great start to the program, you just need to store each employee submission into a collection (List<>, array, etc.).

    To do this, create an Employee class with some properties like Name, AmountEarned, TotalPieces, PiecesMade and so on. In the forms class, create a List<Employee> _employeeList collection. Then create an AddEmployee button. This button will let the user enter a new employee entry. Take the data out of the form and then new up a Employee class instance and set the properties from the class with the data in the form. Next, call _employeeList.Add(...) to add the employee to the collection.

    When the user clicks the calculate button, use a foreach loop to cycle through the _employeeList and use the logic you've already written to determine the max user (as you cycle through the _employeeList keep a maxEmployee variable to store the employee with the highest count - use this variable later to display this employee.

    Take a look at bing or google to search for List<> to find out how to use this collection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center