Monday, June 1, 2009

Car Object, Using array of 10 objects

The Enum is as follows:




public enum Built
{
Hatch=0,
Sedan=1,
Wagon=2,
Coupe=3
}



The Car Class is as follows:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCarProject
{
public class MyCar
{
//fields
#region fields
private string model;
private int year;
private string color;
private Built built;
#endregion

//properties
#region properties
public string Model
{
get { return model; }
set { model = value; }
}
public int Year
{
get { return year; }
set { year = value; }
}
public string Color
{
get { return color; }
set { color = value; }
}
public Built Built
{
get { return built; }
set { built = value; }
}
#endregion

//constructors
#region constructors
public MyCar()
{
}

public MyCar(string _model, int _year, string _color, Built _built)
{
this.model = _model;
this.year = _year;
this.color = _color;
this.built = _built;
}
#endregion

//methods
#region methods

public override string ToString()
{
string msg = "";
msg = "Model : " + this.model + "\n" +
"Year : " + this.year + "\n" +
"Color : " + this.color + "\n" +
"Built : " + this.built;
return msg;
}
#endregion

}
}


The Form is as follows:




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 MyCarProject
{
public partial class Form1 : Form
{
//fields
#region fields
private MyCar[] carArray = new MyCar[10];
#endregion

//properties
#region properties
public MyCar[] CarArray
{
get { return carArray; }
set { carArray = value; }
}

#endregion

//constructors
#region constructors
public Form1()
{
InitializeComponent();
}
#endregion

//methods-events
#region methods-events
private void Form1_Load(object sender, EventArgs e)
{
//add Built items into combobox
cmbBuilt.Items.Add(Built.Coupe);
cmbBuilt.Items.Add(Built.Hatch);
cmbBuilt.Items.Add(Built.Sedan);
cmbBuilt.Items.Add(Built.Wagon);
}

public bool addCar(MyCar obj)
{
for (int i = 0; i < carArray.Length; i++)
{
if (carArray[i] == null)
{
carArray[i] = obj;
return true;
}
}

return false;
}

public MyCar searchByModel(string _model)
{
for (int i = 0; i < carArray.Length; i++)
{
if (_model == carArray[i].Model)
{
return carArray[i];
}
}
return null;
}

public bool updateCar(string _model, MyCar obj)
{
for (int i = 0; i < carArray.Length; i++)
{
if ((_model == carArray[i].Model) && (carArray[i] != null))
{
carArray[i] = obj; //update car object
return true;
}
}
return false;
}

public bool deleteCar(string _model)
{
for (int i = 0; i < carArray.Length; i++)
{
if ((carArray[i].Model == _model) && (carArray[i] != null))
{
carArray[i] = null;
return true;
}
}
return false;
}

public string displayAllCars()
{
string msg = "";
for (int i = 0; i < carArray.Length; i++)
{
if (carArray[i] != null)
{
msg += carArray[i].ToString() + "\n\n";
}
}
return msg;
}

private void btnAdd_Click(object sender, EventArgs e)
{
//read gui
MyCar carObj = new MyCar();

try
{
carObj.Model = txtModel.Text;
carObj.Year = int.Parse(txtYear.Text);
carObj.Color = txtColor.Text;
carObj.Built = (Built)cmbBuilt.SelectedItem;

bool boolAdd = addCar(carObj);
if (boolAdd == true)
{
MessageBox.Show("Car successfully added.");
}
else
{
MessageBox.Show("Car NOT added. Car Array might be full.");
}
}
catch //parsing error
{
MessageBox.Show("Please fill all appropriate fields with correct values");
}

}

private void btnSearch_Click(object sender, EventArgs e)
{
try
{
MyCar obj = searchByModel(txtModel.Text);

if (obj != null) //car found
{
txtYear.Text = "" + obj.Year;
txtColor.Text = "" + obj.Color;
cmbBuilt.SelectedItem = obj.Built;
MessageBox.Show("Car found");
}
else
{
MessageBox.Show("Car not found");
}

}
catch
{
MessageBox.Show("Please enter a valid model name");
txtModel.Focus();
}
}

private void btnUpdate_Click(object sender, EventArgs e)
{

try
{
MyCar car = new MyCar();
car.Model = txtModel.Text;
car.Year = int.Parse(txtYear.Text);
car.Color = txtColor.Text;
car.Built = (Built)cmbBuilt.SelectedItem;

bool boolUpdate = updateCar(car.Model, car);

if (boolUpdate == true)
{
MessageBox.Show("Update Completed");
}
else
{
MessageBox.Show("Update Failed");
}
}
catch
{
MessageBox.Show("Could not perform update");
}

}

private void btnDelete_Click(object sender, EventArgs e)
{
try
{
bool boolDelete = deleteCar(txtModel.Text);

if (boolDelete == true)
MessageBox.Show("Delete Completed");
else
MessageBox.Show("Delete Failed");
}
catch
{
MessageBox.Show("Could not perform delete");
}



}

private void btnDisplayAll_Click(object sender, EventArgs e)
{
string carList = displayAllCars();

if (carList != "")
{
MessageBox.Show(carList);
}
else
{
MessageBox.Show("No cars available to display");
}
}
#endregion
}
}

No comments:

Post a Comment