Friday, May 30, 2014
Mid Term Exam
//Car Class
namespace Zau_MidtermExam
{
//Question No. 1
public class Car
{
private string _make;
private string _model;
private int _year;
private int _mileage;
private decimal _price;
public Car(string make, string model, int year, int mileage, decimal price)
{
_make = make;
_model = model;
_year = year;
_mileage = mileage;
_price = price;
}
public string Make
{ get { return _make; } }
public string Model
{ get { return _model; } }
public int Year
{ get { return _year; } }
public int Mileage
{ get { return _mileage; } }
public decimal Price
{ get { return _price; } }
//============== methods ====================
public void LowerPriceBy(decimal percent)
{
_price = _price * (1 - percent);
}
}
}
//==============================================================
namespace Zau_MidtermExam
{
public partial class Form1 : Form
{
//Question No. 2
List<Car> carList = new List<Car>();
Bitmap bmap;
Graphics g;
public Form1()
{
InitializeComponent();
bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmap);
}
private void Form1_Load(object sender, EventArgs e)
{
Car car = new Car("Honda", "Civid", 1997, 1200, 18000m);
carList.Add(car);
car = new Car("Toyota", "Prius", 2000, 15000, 8600m);
carList.Add(car);
car = new Car("Ford", "F-150", 1999, 45000, 9780m);
carList.Add(car);
car = new Car("Subaru", "Forester", 1997, 135000, 3500m);
carList.Add(car);
car = new Car("Ford", "Focus", 2001, 95000, 2500m);
carList.Add(car);
car = new Car("Honda", "Civid", 2011, 900, 28000m);
carList.Add(car);
car = new Car("Toyota", "Prius", 2012, 0, 45000m);
carList.Add(car);
car = new Car("Ford", "F-150", 2013, 100, 15780m);
carList.Add(car);
car = new Car("Subaru", "Forester", 2009, 5000, 8500m);
carList.Add(car);
car = new Car("Ford", "Focus", 2008, 2000, 2500m);
carList.Add(car);
car = new Car("Honda", "Civid", 2007, 1200, 18500m);
carList.Add(car);
car = new Car("Toyota", "Prius", 2006, 15000, 13600m);
carList.Add(car);
car = new Car("Ford", "F-150", 2005, 45000, 7880m);
carList.Add(car);
car = new Car("Subaru", "Forester", 2004, 135000, 6500m);
carList.Add(car);
car = new Car("Ford", "Focus", 2003, 95000, 9500m);
carList.Add(car);
car = new Car("Honda", "Civid", 2002, 12000, 8000m);
carList.Add(car);
car = new Car("Toyota", "Prius", 2011, 15000, 8699m);
carList.Add(car);
car = new Car("Ford", "F-150", 1996, 45000, 9710m);
carList.Add(car);
car = new Car("Subaru", "Forester", 1997, 135000, 3595m);
carList.Add(car);
car = new Car("Ford", "Focus", 2004, 95000, 12595m);
carList.Add(car);
}
private void btnDisplayallCars_Click(object sender, EventArgs e)
{
carListView.Items.Clear();
DisplayCars();
}
//Question No. 3
private void DisplayCars()
{
foreach (Car car in carList)
{
ListViewItem lvi = new ListViewItem(car.Make);
lvi.SubItems.Add(car.Model);
lvi.SubItems.Add(car.Year.ToString());
lvi.SubItems.Add(car.Mileage.ToString());
lvi.SubItems.Add(car.Price.ToString("c"));
carListView.Items.Add(lvi);
}
}
private void btnMostExpensiveCar_Click(object sender, EventArgs e)
{
carListView.Items.Clear();
Car car = GetMostExpensiveCar();
ListViewItem lvi = new ListViewItem(car.Make);
lvi.SubItems.Add(car.Model);
lvi.SubItems.Add(car.Year.ToString());
lvi.SubItems.Add(car.Mileage.ToString());
lvi.SubItems.Add(car.Price.ToString("c"));
carListView.Items.Add(lvi);
}
//Question No. 4 (Method to get most expensive car)
private Car GetMostExpensiveCar()
{
carListView.Items.Clear();
Car highestprice = carList[0];
foreach (Car car in carList)
{
if (highestprice.Price < car.Price)
{
highestprice = car;
}
}
return highestprice;
}
private void getAvgCarPrice_Click(object sender, EventArgs e)
{
decimal averageCarPrice = GetAverageCarPrice();
txtAveragePrice.Text = averageCarPrice.ToString("c");
}
private decimal GetAverageCarPrice()
{
decimal avgPrice = 0;
decimal sum = 0;
foreach (Car car in carList)
{
sum += car.Price;
avgPrice = sum / carList.Count;
}
return avgPrice;
}
//For Extra Credit
private void btnFillEllipse_Click(object sender, EventArgs e)
{
Point p = new Point(1, 1);
int x = p.X;
int y = p.Y;
int diameter = 192;
SolidBrush brush = new SolidBrush(Color.Yellow);
g.FillEllipse(brush, x, y, diameter, diameter);
pictureBox1.Image = bmap;
}
private void btnClear_Click(object sender, EventArgs e)
{
carListView.Items.Clear();
txtAveragePrice.Clear();
bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmap);
pictureBox1.Image = bmap;
}
private void btnQuit_Click(object sender, EventArgs e)
{
Close();
}
}
}
Labels:
Quiz
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment