public static void editVechile(int vID) { string vType = getVType(vID); try { if (vType != null) { if (vType.Equals("MINI", StringComparison.InvariantCultureIgnoreCase)) { MiniVehicle mini = getInitializeObjMini("MINI"); updateMini(mini, vID); searchVehicle(vID); } else if (vType.Equals("MAXI", StringComparison.InvariantCultureIgnoreCase)) { MaxiVehicle maxi = getInitializeObjMaxi("MAXI"); updateMaxi(maxi, vID); searchVehicle(vID); } } else { throw new Exception("Vehile with Vehicle ID = " + vID + " not found."); } } catch (Exception e) { Console.WriteLine("\n" + e.Message); } }
static void updateMaxi(MaxiVehicle maxi, int vID) { try { maxi.validateEntry(); SqlCommand cmd = new SqlCommand("UPDATE Vehicle SET fuelType = @fuelType, vehicleMake = @vehicleMake, vehicleType = @vehicleType, noOfKiloMeters = @noOfKM WHERE vehicleID = @vehicleID;" + "UPDATE Maxi SET loadInKG = @loadInKG WHERE vehicleID = @vehicleID;" + "UPDATE Bill SET billAmount = @billAmount WHERE vehicleID = @vehicleID;", con); cmd.Parameters.AddWithValue("@vehicleID", vID); cmd.Parameters.AddWithValue("@fuelType", maxi.getFuelType()); cmd.Parameters.AddWithValue("@vehicleMake", maxi.getVehicleMake()); cmd.Parameters.AddWithValue("@vehicleType", maxi.getVehicleType()); cmd.Parameters.AddWithValue("@noOfKM", maxi.getNoOfKiloMeters()); cmd.Parameters.AddWithValue("@loadInKG", maxi.getLoadInKG()); cmd.Parameters.AddWithValue("@billAmount", maxi.calculateBill()); con.Open(); if (cmd.ExecuteNonQuery() == 3) { Console.WriteLine("\nThe Vehicle updated successfully."); } else { throw new Exception("\nVehicle details not updated."); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { con.Close(); } }
static void addToDBMaxi(MaxiVehicle maxi) { try { maxi.validateEntry(); SqlCommand cmd = new SqlCommand("INSERT INTO Vehicle VALUES (@fuelType, @vehicleMake, @vehicleType, @noOfKM);" + "INSERT INTO Maxi VALUES(IDENT_CURRENT('Vehicle'), @loadInKG);" + "INSERT INTO Bill(vehicleID, billAmount) VALUES(IDENT_CURRENT('Vehicle'), @billAmount);", con); cmd.Parameters.AddWithValue("@fuelType", maxi.getFuelType()); cmd.Parameters.AddWithValue("@vehicleMake", maxi.getVehicleMake()); cmd.Parameters.AddWithValue("@vehicleType", maxi.getVehicleType()); cmd.Parameters.AddWithValue("@noOfKm", maxi.getNoOfKiloMeters()); cmd.Parameters.AddWithValue("@loadInKG", maxi.getLoadInKG()); cmd.Parameters.AddWithValue("@billAmount", maxi.calculateBill()); con.Open(); if (cmd.ExecuteNonQuery() != 0) { Console.WriteLine("\nThe Vehicle added successfully."); } else { throw new Exception(); } } catch (Exception e) { Console.WriteLine("\nVehicle Not Added."); Console.WriteLine(e.Message); } finally { con.Close(); } }
public static void addVehicle() { Console.WriteLine("\nChoose Vehicle Type:"); Console.WriteLine("1. Mini Vehicle"); Console.WriteLine("2. Maxi Vehicle"); Console.Write("Enter Choice: "); int vType = Convert.ToInt32(Console.ReadLine()); switch (vType) { case 1: MiniVehicle mini = getInitializeObjMini("MINI"); addToDBMini(mini); break; case 2: MaxiVehicle maxi = getInitializeObjMaxi("MAXI"); addToDBMaxi(maxi); break; default: Console.WriteLine("\nInvalid Choice"); break; } }
static MaxiVehicle getInitializeObjMaxi(string vehicleType) { Console.WriteLine("\nEnter Details of Vehicle:"); Console.Write("{0,-20} : ", "Fuel Type['P'/'D']"); char fType = Convert.ToChar(Console.ReadLine().ToUpper()); Console.Write("{0,-20} : ", "Vehicle Make"); string veMake = Console.ReadLine().ToUpper(); Console.Write("{0,-20} : ", "No Of Kilo Meters"); int nKM = Convert.ToInt32(Console.ReadLine()); Console.Write("{0,-20} : ", "Load in KG"); float load = (float)Convert.ToDouble(Console.ReadLine()); MaxiVehicle maxi = new MaxiVehicle(fType, veMake, vehicleType, nKM, load); maxi.validateFuelType(); maxi.calculateRatePerKiloMeter(); maxi.calculateRatePerKG(); return(maxi); }