Thursday, June 4, 2009

Pretest - Allan's Store

Class as follows:



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

namespace AllanStore
{
public class ComputerSystem
{
//fields
private int barcode;
private string description;
private string model;
private double price;
private Brand brand;

//properties
public int Barcode
{
get { return barcode; }
set { barcode = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
public double Price
{
get { return price; }
set { price = value; }
}
public Brand Brand
{
get { return brand; }
set { brand = value; }
}

//constructors
public ComputerSystem()
{
}

public ComputerSystem(int _barcode, string _description, Brand _brand, string _model, double _price)
{
this.barcode = _barcode;
this.description = _description;
this.brand = _brand;
this.model = _model;
this.price = _price;
}

//methods
public override string ToString()
{
return "" + barcode.ToString() + ", " + description.ToString() + ", " + brand.ToString() + ", " + model.ToString() + ", $" + price.ToString() + "\n";
}


}
}


Enum as follow:


public enum Brand
{
Asus=0,
Dell=1,
Hp=2,
Acer=3
}


Form 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 AllanStore
{
public partial class Form1 : Form
{
//fields
private ComputerSystem[] computerArray = new ComputerSystem[10];

//properties
public ComputerSystem[] ComputerArray
{
get { return computerArray; }
set { computerArray = value; }
}

//constructors
public Form1()
{
InitializeComponent();
}

//methods - EVENTS
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult msg = MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo);
if (msg == DialogResult.Yes)
{
Close();
}
}

private void btnExit_Click(object sender, EventArgs e)
{
exitToolStripMenuItem_Click(sender, e);
}

private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (checkAllFields() == true) //valid
{
ComputerSystem addObj = new ComputerSystem();
addObj.Barcode = int.Parse(txtBarcode.Text);
addObj.Price = double.Parse(txtPrice.Text);
addObj.Brand = (Brand)cmbBrand.SelectedItem;
addObj.Description = txtDescription.Text;
addObj.Model = txtModel.Text;

bool result = addSystem(addObj);

if (result == true)
{
MessageBox.Show("Computer System added!", "Added");
}
else
{
MessageBox.Show("Cannot Add, Array full!", "Cannot Add");
}

}
}

//methods - User defined
public void clearAllFields()
{
txtBarcode.Text = "";
cmbBrand.Text = "";
txtDescription.Text = "";
txtModel.Text = "";
txtPrice.Text = "";
}

public bool checkAllFields()
{
try
{
//fields errors
string errormessage = "";

if ((txtBarcode.Text == "") || (cmbBrand.Text == "") || (txtDescription.Text == "") || (txtModel.Text == "") || (txtPrice.Text == ""))
{
errormessage = "Please enter all fields";
}

//type errors
ComputerSystem testObj = new ComputerSystem();

testObj.Barcode = int.Parse(txtBarcode.Text);
testObj.Price = double.Parse(txtPrice.Text);
testObj.Brand = (Brand)cmbBrand.SelectedItem;

if (errormessage != "")
{
MessageBox.Show(errormessage, "Error occurred");
return false;
}
else
return true;


}
catch //catch type errors
{
MessageBox.Show("Please enter correct values in fields", "Error occurred");
return false;
}
}
public bool addSystem(ComputerSystem addObj)
{
for (int i = 0; i < computerArray.Length; i++)
{
if (computerArray[i] == null)
{
//perform add
computerArray[i] = addObj;
return true;
}
}
return false;
}

private void btnAdd_Click(object sender, EventArgs e)
{
addToolStripMenuItem_Click(sender, e);
}

private void Form1_Load(object sender, EventArgs e)
{
cmbBrand.Items.Add(Brand.Acer);
cmbBrand.Items.Add(Brand.Asus);
cmbBrand.Items.Add(Brand.Dell);
cmbBrand.Items.Add(Brand.Hp);
}

private void btnSearch_Click(object sender, EventArgs e)
{
searchToolStripMenuItem_Click(sender, e);
}

private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
ComputerSystem foundObj = new ComputerSystem();
foundObj = searchSystem(txtBarcode.Text);

if (foundObj == null)
{
MessageBox.Show("Cannot find Computer System", "No search found");
}
else
{
MessageBox.Show("System found");
txtBarcode.Text = foundObj.Barcode.ToString();
txtDescription.Text = foundObj.Description;
txtModel.Text = foundObj.Model;
txtPrice.Text = foundObj.Price.ToString();
cmbBrand.Text = foundObj.Brand.ToString();
}


}
catch
{
MessageBox.Show ("Barcode not valid", "Error");
}

}

public ComputerSystem searchSystem(string sbarcode)
{
try
{
int barcode = int.Parse(sbarcode);

for (int i = 0; i < computerArray.Length; i++)
{
if ((computerArray[i] != null) && (computerArray[i].Barcode == barcode)) //found match
{
return computerArray[i];
}

}
return null;
}
catch
{
return null;

}
}

private void btnUpdate_Click(object sender, EventArgs e)
{
updateToolStripMenuItem_Click(sender, e);
}

public bool updateSystem(string sbarcode, ComputerSystem updateObj)
{
try
{
int barcode = int.Parse(sbarcode);
for (int i = 0; i < computerArray.Length; i++)
{
if ((computerArray[i] != null) && (computerArray[i].Barcode == barcode))
{
computerArray[i] = updateObj;
return true;
}
}
return false;
}
catch
{
return false;
}
}

private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
if (checkAllFields() == true)
{
ComputerSystem updateObj = new ComputerSystem();
updateObj.Barcode = int.Parse(txtBarcode.Text);
updateObj.Price = double.Parse(txtPrice.Text);
updateObj.Brand = (Brand)cmbBrand.SelectedItem;
updateObj.Description = txtDescription.Text;
updateObj.Model = txtModel.Text;

bool updateresult = updateSystem(txtBarcode.Text, updateObj);

if (updateresult == true)
{
MessageBox.Show("System successfully updated");
}
else
{
MessageBox.Show("Cannot update system", "Error");
}
}
}

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
bool deleteresult = deleteSystem(txtBarcode.Text);
if (deleteresult == true)
{
MessageBox.Show("System deleted");
clearAllFields();
}
else
{
MessageBox.Show("Cannot find system", "Delete failed");
}
}

public bool deleteSystem(string sbarcode)
{
try
{
int barcode = int.Parse(sbarcode);

for (int i = 0; i < computerArray.Length; i++)
{
if ((computerArray[i] != null) && (computerArray[i].Barcode == barcode))
{
computerArray[i] = null;
return true;
}
}
return false;
}
catch
{
return false;
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
deleteToolStripMenuItem_Click(sender, e);
}

private void btnDisplayAll_Click(object sender, EventArgs e)
{
displayAllToolStripMenuItem_Click(sender, e);
}

private void displayAllToolStripMenuItem_Click(object sender, EventArgs e)
{
string myComputers = "";
for (int i = 0; i < computerArray.Length; i++)
{
if (computerArray[i] != null)
{
myComputers += computerArray[i].ToString();
}
}

MessageBox.Show(myComputers, "Computer Lists");
}

private void btnAbout_Click(object sender, EventArgs e)
{
AboutBox1 about = new AboutBox1();
about.Show();
}

private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
btnAbout_Click(sender, e);
}

}
}

No comments:

Post a Comment