Ejemplo n.º 1
0
        /// <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();
            }
        }
Ejemplo n.º 2
0
        /// <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);
                }
            }
        }