// load dynamic data and update the view public void UpdateView() { parkingPlaceData = SQLiteDataAccess.LoadParkingPlace(); ParkingCarsData = SQLiteDataAccess.LoadParkingCars(); dataGridView1.Rows[0].Cells[1].Value = ParkingPlaceModel.nextAvailable(parkingPlaceData); var space = ParkingPlaceModel.spaceLeft(parkingPlaceData); dataGridView1.Rows[0].Cells[0].Value = space; dataGridView1.Rows[0].Cells[2].Value = ParkingCarsModel.TotalRevenue(ParkingCarsData); dataGridView1.Rows[0].Cells[3].Value = ParkingCarsModel.Last30Days(ParkingCarsData); if (space == 0) { label1.Visible = true; label1.Text = "Parking Lot is Full"; label1.ForeColor = Color.DarkRed; } else if (space == 100) { label1.Visible = true; label1.Text = "Parking Lot is Empty"; label1.ForeColor = Color.DarkGreen; } else { label1.Visible = false; } }
//when a car enters the parking lot private void CarIn_Click(object sender, EventArgs e) { int userCarId; if (!Int32.TryParse(carId.Text, out userCarId)) { Error("Invalid input, Please enter a number", "Wrong input value"); return; } if (userCarId < 101 && userCarId > 0) { Error("Invalid input, Input must be positive and above the number 100 (1-100 are preserved for test cases)", "Wrong input range"); return; } //Check if the car is already at the parking lot var alreadyExists = ParkingCarsData.Where(x => x.CarId == userCarId && String.IsNullOrEmpty(x.DroveAwayDate) == true).ToList(); if (alreadyExists.Count > 0) { Error(userCarId + " is already parking at the parking place: " + alreadyExists[0].ParkingPlaceId + ".", "Error - Invalid Action"); return; } //check if the car is registered for that date var duplicate = ParkingCarsData.Any(x => x.ParkingDate == GetDay() && userCarId == x.CarId); if (duplicate == true) { //var obj = alreadyExists[0]; Error("This car plate is already registered for that date: a car can park only once per date.", "Error - Logic out of boundary"); return; } // if parking lot is full exit the function if (ParkingPlaceModel.nextAvailable(parkingPlaceData) == -1) { Error("Parking lot is full.", "Error - Parking-Lot is full"); return; } //Checks if carId has memberShip on Cars Table var res = SQLiteDataAccess.LoadCars(); var isMember = res.Any(x => x.ID == userCarId && x.HasMembership == 1); //checks if the month on the user date picker is one of winter's months var isWinter = new[] { 12, 1, 2, 3 }.Contains(dateTimePicker1.Value.Month); var pay = isWinter == true || isMember == true ? 20 : 40; //Creates an obj row to insert to DB var carEnters = new ParkingCarsModel(userCarId, pay, ParkingPlaceModel.nextAvailable(parkingPlaceData), GetDay()); SQLiteDataAccess.EnterParkingLot(carEnters); UpdateView(); }