public List <ElectricityBill> Generate_N_BillDetails(int num) { List <ElectricityBill> L = new List <ElectricityBill>(); try { SqlCommand cmd = new SqlCommand("select top " + num + " * from ElectricityBill order by Consumer_number desc", SqlCon); //SqlCon.Open(); SqlDataReader R = cmd.ExecuteReader(); ElectricityBill E = null; while (R.Read()) { E = new ElectricityBill(); E.ConsumerNumber = R[0].ToString(); E.ConsumerName = R[1].ToString(); E.UnitsConsumed = int.Parse(R[2].ToString()); E.BillAmount = float.Parse(R[3].ToString()); L.Add(E); } } catch (SqlException E) { Console.WriteLine(E.Message); } finally { SqlCon.Close(); } return(L); }
static void Main(string[] args) { BillValidator bb = new BillValidator(); ElectricityBill ebill = new ElectricityBill(); bb.ValidateUnitsConsumed(ebill.UnitsConsumed); Console.WriteLine("Enter Number of Bills To Be Added :"); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { // ElectricityBill ebill = new ElectricityBill(); Console.WriteLine("Enter Consumer Number:"); string c = Console.ReadLine(); try { string temp = @"^[E][B][0-9]{5}$"; Regex R = new Regex(temp); if (!R.IsMatch(c)) { throw new Exception("Invalid Consumer Number"); } else { ebill.ConsumerNumber = c; Console.WriteLine("Enter Consumer Name:"); string s = Console.ReadLine(); ebill.ConsumerName = s; Console.WriteLine("Enter Units Consumed:"); int u = int.Parse(Console.ReadLine()); ebill.UnitsConsumed = u; } } catch (FormatException e) { Console.WriteLine(e.Message); } } Console.WriteLine("Enter Last 'N' Number of Bills To Generate"); int num = int.Parse(Console.ReadLine()); ElectricityBoard eb = new ElectricityBoard(); eb.CalculateBill(ebill); eb.AddBill(ebill); List <ElectricityBill> El = eb.Generate_N_BillDetails(num); }
public void AddBill(ElectricityBill ebill) { try { SqlCommand cmd = new SqlCommand("insert into ElectricityBill values(@cno,@cn,@un,@ba)", SqlCon); // SqlCon.Open(); cmd.Parameters.AddWithValue("@cno", ebill.ConsumerNumber); cmd.Parameters.AddWithValue("@cn", ebill.ConsumerName); cmd.Parameters.AddWithValue("@un", ebill.UnitsConsumed); CalculateBill(ebill); cmd.Parameters.AddWithValue("@ba", ebill.BillAmount); cmd.ExecuteNonQuery(); } catch (SqlException E) { Console.WriteLine(E.Message); } finally { SqlCon.Close(); } }
public void CalculateBill(ElectricityBill ebill) { if (ebill.UnitsConsumed <= 100) { ebill.BillAmount = 0; } else if (ebill.UnitsConsumed > 100 && ebill.UnitsConsumed <= 300) { ebill.BillAmount = 100 * 0 + (ebill.UnitsConsumed - 100) * 1.5; } else if (ebill.UnitsConsumed > 300 && ebill.UnitsConsumed <= 600) { ebill.BillAmount = 100 * 0 + 200 * 1.5 + (ebill.UnitsConsumed - 300) * 3.5; } else if (ebill.UnitsConsumed > 600 && ebill.UnitsConsumed <= 1000) { ebill.BillAmount = 100 * 0 + 200 * 1.5 + 300 * 3.5 + (ebill.UnitsConsumed - 600) * 5.5; } else { ebill.BillAmount = 100 * 0 + 200 * 1.5 + 300 * 3.5 + 400 * 5.5 + (ebill.UnitsConsumed - 100) * 7.5; } }
static void Main(string[] args) { Console.WriteLine("Number of bills to be added"); int n = int.Parse(Console.ReadLine()); string number; string name; int units; ElectricityBill[] ebill = new ElectricityBill[n]; BillValidator bvalidate = new BillValidator(); ElectricityBoard eboard = new ElectricityBoard(); for (int i = 0; i < n; i++) { Console.WriteLine(); ebill[i] = new ElectricityBill(); try { Console.WriteLine("Enter Consumer Number:"); number = Console.ReadLine(); string p = @"^[E][B][0-9]{5}$"; Regex r = new Regex(p); if (r.IsMatch(number)) { ebill[i].ConsumerNumber = number; Console.WriteLine("Enter Consumer Name:"); name = Console.ReadLine(); ebill[i].ConsumerName = name; Console.WriteLine("Enter Units Consumed:"); units = int.Parse(Console.ReadLine()); while (bvalidate.ValidateUnitsConsumed(units) != null) { Console.WriteLine(bvalidate.ValidateUnitsConsumed(units)); Console.WriteLine("Enter Units Consumed:"); units = int.Parse(Console.ReadLine()); } ebill[i].UnitsConsumed = units; //Console.WriteLine(ebill[i].ConsumerNumber); eboard.AddBill(ebill[i]); } else { throw new FormatException("Invalid Consumer Number"); } } catch (FormatException e) { Console.WriteLine(e.Message); } } Console.WriteLine(); Console.WriteLine("Enter Last 'N' Number of Bills To Generate"); int num = int.Parse(Console.ReadLine()); List <ElectricityBill> list = eboard.Generate_N_BillDetails(num); Console.WriteLine(); for (int i = 0; i < n; i++) { Console.WriteLine(ebill[i].ConsumerNumber + "\n" + ebill[i].ConsumerName + "\n" + ebill[i].UnitsConsumed + "\n" + ebill[i].BillAmount); Console.WriteLine(); } Console.WriteLine("Details of last ‘N’ bills:"); for (int i = 0; i < list.Count; i++) { Console.WriteLine("EB bill for " + list[i].ConsumerName + " is " + list[i].BillAmount); } Console.Read(); }