/// <summary> /// method to get the selected building /// </summary> private void edit() { if (selectBuilding != null) { editBuilding = selectBuilding; txtName.Text = selectBuilding.Name; txtX.Text = selectBuilding.X_pos.ToString(); txtY.Text = selectBuilding.Y_pos.ToString(); switch (selectBuilding.Type) { case "Shop": rbShop.Checked = true; rbMall.Checked = false; rbTrainStation.Checked = false; Shop sp = (MVCPseudoGPS.Shop)selectBuilding; lblCusVal.Text = "Rating"; txtValue.Text = sp.Rating.ToString(); break; case "Mall": rbShop.Checked = false; rbMall.Checked = true; rbTrainStation.Checked = false; Mall ml = (MVCPseudoGPS.Mall)selectBuilding; lblCusVal.Text = "Capacity"; txtValue.Text = ml.Capacity.ToString(); break; case "Train Station": rbShop.Checked = false; rbMall.Checked = false; rbTrainStation.Checked = true; TrainStation ts = (MVCPseudoGPS.TrainStation)selectBuilding; lblCusVal.Text = "Line"; txtValue.Text = ts.Line; break; } } }
/// <summary> /// Same as ViewForm 1 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ctxLoad_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { string name, type, line; int xL, yL, capacity; double rating; // create arrayList from model and convert to array of shapes ArrayList theBuildingList = myModel.BuildingList; Base[] theBuildings = (Base[])theBuildingList.ToArray(typeof(Base)); // Ask to Save First if (theBuildingList.Count > 0) { DialogResult res = MessageBox.Show("There are existing Buildings, Saving is reccomened otherwise data will be lost.", "Warning: Loss of Data", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (res == DialogResult.OK) { saveToolStripMenuItem.PerformClick(); } else { MessageBox.Show("Your Data was not saved.", "Data not Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); } } // clear panel clearPanel(); // draw all shapes in array foreach (Base b in theBuildings) { theBuildingList.Remove(b); } //load data StreamReader sr = new StreamReader(openFileDialog.FileName); string theObjects = sr.ReadToEnd(); sr.Close(); string[] theLines = theObjects.Split('#'); foreach (string sO in theLines) { if (sO != "") { string[] building = sO.Split(','); type = building[0]; name = building[1]; string px = building[3]; string py = building[4]; string x = px.Replace("(", ""); string y = px.Replace("(", ""); xL = Convert.ToInt32(x); yL = Convert.ToInt32(y); switch (type) { case "Shop": rating = Convert.ToDouble(building[2]); Shop sBuild = new Shop(name, xL, yL, type, Color.Black, rating); myModel.AddBuilding(sBuild); break; case "Mall": capacity = Convert.ToInt32(building[2]); Mall mBuild = new Mall(name, xL, yL, type, Color.Red, capacity); myModel.AddBuilding(mBuild); break; case "Train Station": line = building[2]; TrainStation tsBuild = new TrainStation(name, xL, yL, type, Color.Blue, line); myModel.AddBuilding(tsBuild); break; } } } this.Invalidate(); } }
/// <summary> /// updates the selected building with new data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click(object sender, EventArgs e) { int nX, nY; string nName; if (string.IsNullOrWhiteSpace(txtName.Text) || string.IsNullOrWhiteSpace(txtX.Text) || string.IsNullOrWhiteSpace(txtY.Text)) { MessageBox.Show("Textboxes cannot be empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (!rbMall.Checked && !rbShop.Checked && !rbTrainStation.Checked) { MessageBox.Show("Building Type must be selected.", "No Building Type Selected.", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { Base selectedBuilding = (MVCPseudoGPS.Base)editBuilding; if (selectedBuilding.Type == "Shop") { if (!Double.TryParse(txtValue.Text, out double parse)) { MessageBox.Show("Capacity is limited to Integer's only", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { double nRating = Convert.ToDouble(txtValue.Text); Shop sB = (MVCPseudoGPS.Shop)editBuilding; if (nRating > 10.0 || nRating < 0.0) { MessageBox.Show("Rating must be between 0.0 - 10.0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { sB.Rating = nRating; } } } else if (selectedBuilding.Type == "Mall") { if (!int.TryParse(txtValue.Text, out int parse)) { MessageBox.Show("Capacity is limited to Integer's only", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { int nCapacity = Convert.ToInt32(txtValue.Text); Mall sB = (MVCPseudoGPS.Mall)editBuilding; if (nCapacity < 0) { MessageBox.Show("Capacity cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { sB.Capacity = nCapacity; } } } else if (selectedBuilding.Type == "Train Station") { string nLine = txtValue.Text; TrainStation sB = (MVCPseudoGPS.TrainStation)editBuilding; sB.Line = nLine; } else { MessageBox.Show("Wait, What this error shouldn't happen?", "Building Type doesn't Exist", MessageBoxButtons.OK, MessageBoxIcon.Error); } nX = Convert.ToInt32(txtX.Text); nY = Convert.ToInt32(txtY.Text); nName = txtName.Text; if (nX > max_x) { MessageBox.Show("X value cannot be greater than " + max_x, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (nX < 0) { MessageBox.Show("X value cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (nY > max_y) { MessageBox.Show("Y value cannot be greater than " + max_y, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (nY < 0) { MessageBox.Show("Y value cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { selectedBuilding.X_pos = nX; selectedBuilding.Y_pos = nY; selectedBuilding.Name = nName; } } myModel.UpdateViews(); }
/// <summary> /// This method is exected with the btnAdd is clicked. /// It creates an Building based on the selected type and adds it the Model's arraylist and display it in the listbox. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAdd_Click(object sender, EventArgs e) { int X, Y; string name; Color aColor; Base aBuilding; if (string.IsNullOrWhiteSpace(txtName.Text) || string.IsNullOrWhiteSpace(txtX.Text) || string.IsNullOrWhiteSpace(txtY.Text)) { MessageBox.Show("Textboxes cannot be empty", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (!rbMall.Checked && !rbShop.Checked && !rbTrainStation.Checked) { MessageBox.Show("Building Type must be selected.", "No Building Type Selected.", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { try { X = Convert.ToInt32(txtX.Text); Y = Convert.ToInt32(txtY.Text); if (X > max_x) { MessageBox.Show("X value cannot be greater than " + max_x, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (X < 0) { MessageBox.Show("X value cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Y > max_y) { MessageBox.Show("Y value cannot be greater than " + max_y, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Y < 0) { MessageBox.Show("Y value cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { name = txtName.Text; if (rbShop.Checked) { if (!Double.TryParse(txtValue.Text, out double parse)) { MessageBox.Show("Rating is limited to Double value's only", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string type = "Shop"; double rating = Convert.ToDouble(txtValue.Text); aColor = Color.Black; if (rating > 10.0 || rating < 0.0) { MessageBox.Show("Rating must be between 0.0 - 10.0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { aBuilding = new Shop(name, X, Y, type, aColor, rating); myModel.AddBuilding(aBuilding); } } } else if (rbMall.Checked) { if (!int.TryParse(txtValue.Text, out int parse)) { MessageBox.Show("Capacity is limited to Integer's only", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string type = "Mall"; int capacity = Convert.ToInt32(txtValue.Text); aColor = Color.Red; if (capacity < 0) { MessageBox.Show("Capacity cannot be less than 0", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { aBuilding = new Mall(name, X, Y, type, aColor, capacity); myModel.AddBuilding(aBuilding); } } } else if (rbTrainStation.Checked) { string type = "Train Station"; string line = txtValue.Text; aColor = Color.Blue; aBuilding = new TrainStation(name, X, Y, type, aColor, line); myModel.AddBuilding(aBuilding); } else { MessageBox.Show("Please Select a Building Type", "No Building Type Selected!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + "\r\n" + ex.ToString(), "Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }