Question

Looking for some help here: Edit GetPhoneData to add more input validation by setting ranges for...

Looking for some help here: Edit GetPhoneData to add more input validation by setting ranges for values

  • Brands (Samsung, iPhone, Google)
  • Models (Galaxy, Note, 8, X, Pixel)
  • Price ($0.01-$2000)
  • Include a comment about your addition, you can be creative, add components (or not), individualize this.
  • Extra Credit (Samsung must be paired to Galaxy or Note) 2 points

Below is the 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 Cell_Phone_Test

{

  public partial class cellPhoneTutorial : Form

  {

    public cellPhoneTutorial()

    {

      InitializeComponent();

    }

    // The GetPhoneData method accepts a CellPhone object

    // as an argument. It assigns the data entered by the

    // user to the object's properties.

    private void GetPhoneData(CellPhone phone)

    {

      // Temporary variable to hold the price.

      decimal price;

      // Get the phone's brand.

      phone.Brand = brandTextBox.Text;

      // Get the phone's model.

      phone.Model = modelTextBox.Text;

      // Get the phone's price.

      if (decimal.TryParse(priceTextBox.Text, out price))

      {

        phone.Price = price;

      }

      else

      {

        // Display an error message.

        MessageBox.Show("Invalid price");

      }

    }

    private void CreateObjectButton_Click(object sender, EventArgs e)

    {

      // Create a CellPhone object.

      CellPhone myPhone = new CellPhone();

      // Get the phone data.

      GetPhoneData(myPhone);

      // Display the phone data.

      brandLabel.Text = myPhone.Brand;

      modelLabel.Text = myPhone.Model;

      priceLabel.Text = myPhone.Price.ToString("c");

    }

    private void exitButton_Click(object sender, EventArgs e)

    {

      // Close the form.

      this.Close();

    }

    

  }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate answer. Thanks

Note: Only providing .cs code as you already have form design. If you still need form Code do let me know.

Code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Globalization;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Cell_Phone_Test

{

    public partial class cellPhoneTutorial : Form

    {

        //List to hold valid ranges for Brands and models

        List<string> Brands = new List<string>() { "Samsung", "iPhone", "Google" };

        List<string> Models = new List<string>() { "Galaxy", "Note", "8", "X", "Pixel" };

        public cellPhoneTutorial()

        {

            InitializeComponent();

        }

        // The GetPhoneData method accepts a CellPhone object

        // as an argument. It assigns the data entered by the

        // user to the object's properties.

        private void GetPhoneData(CellPhone phone)

        {

            // Temporary variable to hold the price.

            decimal price;

            string brand, model;

            bool validBrand = false, validModel = false, validSamsungModel = true, validPrice = false;

            brand = brandTextBox.Text;

            model = modelTextBox.Text;

            //Find the entered brand in Brand ranges

            validBrand = Brands.Any(x => x.Equals(brand,StringComparison.OrdinalIgnoreCase));

           

               

            //find the entered model in model ranges

            validModel = Models.Any(x => x.Equals(model,StringComparison.OrdinalIgnoreCase));

            //If model and brand are valid then look for pair, currently only looking for Samsung with Note and Galaxy

            if (validBrand && validModel)

            {

                //if brand is Samsung

                if (brand.Equals("Samsung", StringComparison.OrdinalIgnoreCase))

                {

                    //Brand is Samsung and model is not Galaxy or Note then validModel is false

                    if (!(model.Equals("Galaxy", StringComparison.OrdinalIgnoreCase)

                         || model.Equals("Note", StringComparison.OrdinalIgnoreCase)))

                    {

                        validModel = false;

                        validSamsungModel = false;

                    }

                }

                //so if brand is not samsung and models are in Galaxy or Note then again invalid brand

                else if (model.Equals("Galaxy", StringComparison.OrdinalIgnoreCase)

                || model.Equals("Note", StringComparison.OrdinalIgnoreCase))

                {

                    validBrand = false;                   

                }

            }         

            // Get the phone's brand. show the message if invalid brand

            if(validBrand)

                phone.Brand = brand;

            else

                MessageBox.Show("Invalid brand", "Cell_Phone_Test");

            // Get the phone's model. show the message if invalid model

            if (validModel && validSamsungModel)

                phone.Model = model;

            else

            {

                if(!validSamsungModel)

                    MessageBox.Show("Invalid Samsung model", "Cell_Phone_Test");

                else

                    MessageBox.Show("Invalid model", "Cell_Phone_Test");

            }

           

            // Get the phone's price.show the message if invalid price

            if (decimal.TryParse(priceTextBox.Text, out price))

            {

                if (price >= .01m && price <= 200)

                {

                    phone.Price = price;

                    validPrice = true;

                }

            }          

               

            // Display an error message.

            if(!validPrice)

            MessageBox.Show("Invalid price", "Cell_Phone_Test");         

        }

        private void CreateObjectButton_Click(object sender, EventArgs e)

        {

            //clear the labels first

            brandLabel.Text = "";

            modelLabel.Text = "";

            priceLabel.Text = "";

            // Create a CellPhone object.

            CellPhone myPhone = new CellPhone();

            // Get the phone data.

            GetPhoneData(myPhone);

            // Display the phone data.

            brandLabel.Text = myPhone.Brand;

            modelLabel.Text = myPhone.Model;

            //priceLabel.Text = myPhone.Price.ToString("c");

            priceLabel.Text = string.Format(new CultureInfo("en-us"), "{0}", myPhone.Price);

        }

        private void exitButton_Click(object sender, EventArgs e)

        {

            // Close the form.

            this.Close();

        }       

    }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Looking for some help here: Edit GetPhoneData to add more input validation by setting ranges for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi everyone, For my Assignment I have to use C# in order to create a validation...

    Hi everyone, For my Assignment I have to use C# in order to create a validation form and include the following things to register and validate: Email Address, Username, Password, Retype Password, and a Submit Button. I have figured out some of the code for the Email Address validation: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Validation4 {     public partial class Form1 : Form     {        ...

  • In the "Go" button add 15 random numbers in the array using a random number generator....

    In the "Go" button add 15 random numbers in the array using a random number generator. Remove the call to "SetToZero" and the code following. Remove the "SetToZero" method. Add Access Keys of your choice Assign an accept button Assign cancel button (Exit) Add a Button "Find Max" Write an appropriately named method that Accepts the array Finds the largest int and returns the value "Find Max" button click Calls above method Opens a MessageBox to display the returned value...

  • how to add methods into thjs code using C# on visual studio? -validatetextbox should return a true false depending if input is valid -resetinput should clear all input and restart radiobuttons...

    how to add methods into thjs code using C# on visual studio? -validatetextbox should return a true false depending if input is valid -resetinput should clear all input and restart radiobuttons to the defult positions -displaymessge must display messge box to the user displaying such as “5+8=12” calculator.cs x Prearam S Miscellaneous Files ator.Designer.c s Calculato 1 曰using Systemi 2 using System.Collections.Generic; using System.ComponentModel; 4 using System.Data; s using System.Drawing; 6 using System.Linq 7using System.Text; 8 using System.Threading.Tasks; 9 using...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • C# Temperature Converter Application: I have most of the application complete I just need only help...

    C# Temperature Converter Application: I have most of the application complete I just need only help with error messages. My instructor wants me to show four error messages inside an error message label (not a message box) and all of the error messages should appear inside error message label. The four error messages should appear when: 1: User enters data inside both Celsius and fahrenheit text boxes: “You should enter data in only one textbox. Press Clear to start over.”...

  • C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have...

    C# Temp. Converter program. Create a Celsius and Fahrenheit Temperature Converter application. The application must have 2 Labels (each one of them assigned only to Celsius and Fahrenheit), 2 TextBoxes (each one of them assigned only to Celsius and Fahrenheit), only 1 Convert Button, 1 Clear Button, and 1 Exit Button. Also, 1 Error message label which will display the error messages. The application should only allow the user to enter the desired temperature just in one of the TextBoxes...

  • I started this with a windows form, however, based on the answer to this post yesterday,...

    I started this with a windows form, however, based on the answer to this post yesterday, they started it out differently than I did, is it with a console app? In need of direction as the small detail can make a big difference.   " //--------- Annuity.cs ---------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; [Missing] Using System.Windows.Forms; ** namespace assignment { public class Annuity { private double annuityNumber; private double depositAmount; private double term; private double currentBalanace;...

  • C#: I am working on this application and keep running into this error. Please help me fix it. Err...

    C#: I am working on this application and keep running into this error. Please help me fix it. Error: Design: Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SinghAjayProgram08 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double totalInserted = 0; double totalWon = 0; private void spinButton_Click(object sender, EventArgs e) { Random rand = new Random(); int index = rand.Next(fruitsImageList.Images.Count); firstPictureBox.Image =...

  • Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u...

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace FileWriter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCreate_Click(object sender, EventArgs e) { try { //write code that assigns the value in the textbox to an Integer variable if // write code that validates whether or not the data entered in the textbox is greater than or equal to 1 *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT