Specifically the if/else statements trying to match model and brand. The code works, but it's ugly. Any suggestions to clean it up?
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
{
class CellPhone
{
// Fields
private string _brand; // Phone brand
private string _model; // Phone model
private decimal _price; // Phone price
// Constructor
public CellPhone()
{
_brand = "";
_model = "";
_price = 0m;
}
// Brand property
public string Brand
{
get { return _brand; }
set { _brand = value; }
}
// Model property
public string Model
{
get { return _model; }
set { _model = value; }
}
// Price property
public decimal Price
{
get { return _price; }
set { _price = value; }
}
}
public partial class cellPhoneForm : Form
{
public cellPhoneForm()
{
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. Check that it is a valid brand.
if ((brandTextBox.Text.Contains("Google")) || (brandTextBox.Text.Contains("iPhone")) || (brandTextBox.Text.Contains("Samsung")))
{
phone.Brand = brandTextBox.Text;
}
else
{
MessageBox.Show("Please enter a valid brand. (Google, iPhone, or Samsung)");
}
// Get the phone's model. Check that it is a valid model and matches brand.
if ((modelTextBox.Text.Contains("Galaxy")) || (modelTextBox.Text.Contains("Note")) || (modelTextBox.Text.Contains("8")) || (modelTextBox.Text.Contains("X")) || (modelTextBox.Text.Contains("Pixel")))
{
if ((modelTextBox.Text.Contains("Galaxy")) || (modelTextBox.Text.Contains("Note")) == (brandTextBox.Text.Contains("Samsung"))) //
{
phone.Model = modelTextBox.Text;
if ((modelTextBox.Text.Contains("Pixel")) == (brandTextBox.Text.Contains("Google")))
{
phone.Model = modelTextBox.Text;
if ((modelTextBox.Text.Contains("8")) || (modelTextBox.Text.Contains("x")) == (brandTextBox.Text.Contains("iPhone")))
{
phone.Model = modelTextBox.Text;
}
else
{
MessageBox.Show("Please enter an iPhone model. (8 or X)");
}
}
else
{
MessageBox.Show("Please enter a Google model. (Pixel)");
}
}
else
{
MessageBox.Show("Please enter a Samsung model. (Galaxy or Note)");
}
}
else
{
MessageBox.Show("Please enter a valid model. (Galaxy, Note, 8, X, Pixel)");
}
// Get the phone's price. Check if it is within the range.
if (decimal.TryParse(priceTextBox.Text, out price))
{
if (price >= 0.01m & price <= 2000.00m)
{
phone.Price = price;
}
else
{
MessageBox.Show("Please enter a price between $0.01 and $2000.00.");
}
}
else
{
// Display an error message.
MessageBox.Show("Invalid price");
}
}
private void createObjectButton_Click(object sender, EventArgs e)
{
try
{
// Creat 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");
}
catch
{
// Display an error message.
MessageBox.Show("Invalid data was entered.");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
}
}
```