Hey guys, I'm trying to make an application for vehicles in inventory. Well i'm using a generic list and have a class of Vehicle, of which a vehicle type like Car, Truck and SUV will inherit from, so far I've created a Car class. Now thats what Im having trouble with. From my understanding I need to do something along the lines of
Vehicle myVehicle;
myVehicle = new Car(); or
myVehicle = new Truck(); etc..
The cartype like Car will have specific attributes like being a v4, having 4 doors, and having say 4 passangers. A truck would have attributes like being a v8, 2 doors, 3 passangers, and well you get the idea.
The way i have the code set up below, I don't believe the Car class is inheriting the info that needs to because when I run it, I get all the "None" from the Car class as an output on the listBox - which is where only multiple entries with the same information should appear. So I am seeking some guidance as to how I could make each Vehicle type inherit the information properly for each vehicle type,and add more info to it (which the user will input as well) like the engine, # of doors and passengers and then display it.
I hope was clear enough...
Thanks in advance for any help.
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; using System.Collections; using System.IO; namespace Inventory { public partial class Form1 : Form { public string output; public string vtype; // ArrayList VehicleCollection = new ArrayList(); List<Vehicle> VehicleCollection = new List<Vehicle>(); Vehicle myVehicle; public Form1() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { try{ long num = Convert.ToInt64(txtStockNumber.Text); if (txtStockNumber.Text == "") { MessageBox.Show("Please enter all of the data for the Vehicle"); txtStockNumber.Focus(); } else if (txtYear.Text == "") { MessageBox.Show("Please enter all of the data for the Vehicle"); txtYear.Focus(); } else if (txtMake.Text == "") { MessageBox.Show("Please enter all of the data for the Vehicle"); txtMake.Focus(); } else if (txtModel.Text == "") { MessageBox.Show("Please enter all of the data for the Vehicle"); txtModel.Focus(); } else if((rdoCar.Checked == false) && (rdoTruck.Checked == false) && (rdoSUV.Checked == false) && (rdoCrossover.Checked==false)) { MessageBox.Show("Please select a vehicle type"); } else if (txtMSRP.Text == "") { MessageBox.Show("Please enter all of the data for the Vehicle"); txtMSRP.Focus(); } else if ((num < 10000) || (num > 99999)) { MessageBox.Show("Please Enter 5 Numbers for the Stock Number"); } else { myVehicle = new Car(); myVehicle.StockNumber = txtStockNumber.Text; myVehicle.Year = txtYear.Text; myVehicle.Make = txtMake.Text; myVehicle.Model = txtModel.Text; myVehicle.VehicleType = vtype; myVehicle.MSRP = txtMSRP.Text; MessageBox.Show("Vehicle Added"); txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; listList.Items.Clear(); VehicleCollection.Add(myVehicle); } }//ends try catch(OverflowException) { MessageBox.Show("Please Enter 5 Numbers for the Stock Number"); } } private void btnFind_Click(object sender, EventArgs e) { string stockNumber = txtStockNumber.Text; string year = txtYear.Text; string make = txtMake.Text; string model = txtModel.Text; stockNumber = stockNumber.Trim(); year = year.Trim(); make = make.Trim(); model = model.Trim(); Vehicle vehic = new Car(); if (stockNumber != "" || year != "" || (make != "" && model != "")) { for (int i = 0; i < VehicleCollection.Count; i++) { vehic = (Vehicle)VehicleCollection[i]; if ((vehic.StockNumber.ToUpper()).Equals(stockNumber.ToUpper())) { txtYear.Text = vehic.Year; txtMake.Text = vehic.Make; txtModel.Text = vehic.Model; vtype = vehic.VehicleType; txtMSRP.Text = vehic.MSRP; if (vtype == "CAR") { rdoCar.Checked = true; } else if (vtype == "TRUCK") { rdoTruck.Checked = true; } else if (vtype == "SUV") { rdoSUV.Checked = true; } else if (vtype == "CROSSOVER") { rdoCrossover.Checked = true; } listList.Items.Clear(); int listCounter = 0; for (int j = 0; j < VehicleCollection.Count; j++) { vehic = (Vehicle)VehicleCollection[j]; if ((vehic.StockNumber.ToUpper()).Equals(stockNumber.ToUpper())) { listList.Items.Add(vehic.ToString()); listCounter++; } } if (listCounter > 1) { txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; } else { listList.Items.Clear(); } break; } else if ((vehic.Year.ToUpper()).Equals(year.ToUpper())) { txtStockNumber.Text = vehic.StockNumber; txtMake.Text = vehic.Make; txtModel.Text = vehic.Model; txtMSRP.Text = vehic.MSRP; vtype = vehic.VehicleType; listList.Items.Clear(); int listCounter = 0; for (int j = 0; j < VehicleCollection.Count; j++) { vehic = (Vehicle)VehicleCollection[j]; if ((vehic.Year.ToUpper()).Equals(year.ToUpper())) { listList.Items.Add(vehic.ToString()); listCounter++; } } if (listCounter > 1) { txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; } else { listList.Items.Clear(); } break; } else if ((vehic.Make.ToUpper()).Equals(make.ToUpper()) && (vehic.Model.ToUpper().Equals(model.ToUpper()))) { txtStockNumber.Text = vehic.StockNumber; txtYear.Text = vehic.Year; txtMSRP.Text = vehic.MSRP; vtype = vehic.VehicleType; listList.Items.Clear(); int listCounter = 0; for (int j = 0; j < VehicleCollection.Count; j++) { vehic = (Vehicle)VehicleCollection[j]; if ((vehic.Make.ToUpper()).Equals(make.ToUpper()) && (vehic.Model.ToUpper().Equals(model.ToUpper()))) { listList.Items.Add(vehic.ToString()); listCounter++; } }//ends for loop // found = true; if (listCounter > 1) { txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; } else { listList.Items.Clear(); } break; } } //ends loop }///ends if } private void Form1_Load(object sender, EventArgs e) { } private void btnDelete_Click(object sender, EventArgs e) { string stockNumber = txtStockNumber.Text; stockNumber = stockNumber.Trim(); if (stockNumber != "") { foreach (Vehicle vehicle1 in VehicleCollection) { if ((vehicle1.StockNumber.ToUpper()).Equals(stockNumber.ToUpper())) { VehicleCollection.Remove(vehicle1); break; } }//end foreach MessageBox.Show("Vehicle Deleted"); txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; listList.Items.Clear(); } } private void btnSaveChanges_Click(object sender, EventArgs e) { bool found = false; String stockNumber = txtStockNumber.Text.Trim(); if (stockNumber != "") { foreach (Vehicle vehicle2 in VehicleCollection) { if ((vehicle2.StockNumber.ToUpper()).Equals(stockNumber.ToUpper())) { vehicle2.Year = txtYear.Text.Trim(); vehicle2.Make = txtMake.Text.Trim(); vehicle2.Model = txtModel.Text.Trim(); vehicle2.MSRP = txtMSRP.Text.Trim(); vehicle2.VehicleType = vtype.Trim(); MessageBox.Show("Changes Saved"); found = true; break; } } //ends foreach } //ends if if (!found) { MessageBox.Show("Can not find Vehicle entered"); } object o = new object(); EventArgs ea = new EventArgs(); txtStockNumber.Text = ""; txtYear.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtMSRP.Text = ""; rdoCar.Checked = false; rdoTruck.Checked = false; rdoSUV.Checked = false; rdoCrossover.Checked = false; listList.Items.Clear(); } private void btnSaveToFile_Click(object sender, EventArgs e) { String stockNumber = txtStockNumber.Text.Trim(); Vehicle ve = new Car(); if (stockNumber != "") { foreach (Vehicle vehicle2 in VehicleCollection) { if (vehicle2.StockNumber.ToUpper().Equals(stockNumber.ToUpper())) { vehicle2.Year = txtYear.Text.Trim(); vehicle2.Make = txtMake.Text.Trim(); vehicle2.Model = txtModel.Text.Trim(); vehicle2.MSRP = txtMSRP.Text.Trim(); vehicle2.VehicleType = vtype.Trim(); break; } } //ends foreach } //ends if string line = ""; for (int i = 0; i < VehicleCollection.Count; ++i) { line += VehicleCollection[i].ToString() + "@"; } string[] values = line.Split(new char[] { '@' }); System.IO.File.WriteAllLines(@"C:\Projects\vehicles.txt", values); MessageBox.Show("Saved"); } private void rdoCar_CheckedChanged(object sender, EventArgs e) { txtTrunkSpace.ReadOnly = false; txtTowing.ReadOnly = true; txtStorage.ReadOnly = true; vtype = "CAR"; } private void rdoTruck_CheckedChanged(object sender, EventArgs e) { txtStorage.ReadOnly = true; txtTrunkSpace.ReadOnly = true; txtTowing.ReadOnly = false; vtype = "TRUCK"; } private void rdoSUV_CheckedChanged(object sender, EventArgs e) { txtTrunkSpace.ReadOnly = true; txtTowing.ReadOnly = true; txtStorage.ReadOnly = false; vtype = "SUV"; } private void rdoCrossover_CheckedChanged(object sender, EventArgs e) { txtTrunkSpace.ReadOnly = true; txtTowing.ReadOnly = true; txtStorage.ReadOnly = false; vtype = "CROSSOVER"; } }//ends form class public abstract class Vehicle { private string stockNumber; private string year; private string make; private string model; private string vehicleType; private string msrp; public override string ToString() { return (stockNumber + "-" + year + "-" + make + "-" + model + "-" + vehicleType + "-" + msrp + System.Environment.NewLine); } public Vehicle(string s, string yr, string ma, string mo, string vt, string mp) { stockNumber = s; year = yr; make = ma; model = mo; vehicleType = vt; msrp = mp; } public Vehicle() { stockNumber = "None"; year = "None"; make = "None"; model = "None"; vehicleType = "None"; msrp = "None"; } public string StockNumber { get { return stockNumber; } set { stockNumber = value; } } public string Year { get { return year; } set { year = value; } } public string Make { get { return make; } set { make = value; } } public string Model { get { return model; } set { model = value; } } public string VehicleType { get { return vehicleType; } set { vehicleType = value; } } public string MSRP { get { return msrp; } set { msrp = value; } } } //ends Vehicle class } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Inventory { class Car : Vehicle { public string StockNumber { get; set; } public string Year { get; set; } public string Make { get; set; } public string Model { get; set; } public string MSRP { get; set; } public Car(string s, string yr, string ma, string mo, string mp)// : base(s, yr, ma, mo, mp) { StockNumber = s; Year = yr; Make = ma; Model = mo; // vehicleType = vt; MSRP = mp; } public Car() { StockNumber = "0"; Year = "None"; Make = "None"; Model = "None"; // vehicleType = "None"; MSRP = "None"; } public override string ToString() { return (StockNumber + "-" + Year + "-" + Make + "-" + Model + "-" + "CAR" + "-" + MSRP + System.Environment.NewLine); } } }



LinkBack URL
About LinkBacks


