public void CalculateBill(ElectricityBill ebill) { int un = ebill.UnitsConsumed; double amt = ebill.BillAmount; if (un <= 100) { amt = 0; } else if (un > 100 && un <= 300) { amt = amt + 100 * 0 + (un - 100) * 1.5; } else if (un > 300 && un <= 600) { amt = amt + 100 * 0 + 200 * 1.5 + (un - 300) * 3.5; } else if (un > 600 && un <= 1000) { amt = amt + 100 * 0 + 200 * 1.5 + 300 * 3.5 + (un - 600) * 5.5; } else if (un > 1000) { amt = amt + 100 * 0 + 200 * 1.5 + 300 * 3.5 + 600 * 5.5 + (un - 1000) * 7.5; } ebill.BillAmount = amt; }
public void AddBill(ElectricityBill ebill) { try { SqlCommand cmd = new SqlCommand("insert into ElectricityBill values(@cno,@cname,@u,@bill)", SqlCon); cmd.Parameters.AddWithValue("@cno", ebill.ConsumerNumber); cmd.Parameters.AddWithValue("@cname", ebill.ConsumerName); cmd.Parameters.AddWithValue("@u", ebill.UnitsConsumed); CalculateBill(ebill); cmd.Parameters.AddWithValue("@bill", ebill.BillAmount); // SqlCon.Open(); int r = cmd.ExecuteNonQuery(); } catch (SqlException e) { Console.WriteLine(e.Message); } finally { SqlCon.Close(); } }
public List <ElectricityBill> Generate_N_BillDetails(int num) { List <ElectricityBill> list = new List <ElectricityBill>(); SqlCommand cmd = new SqlCommand(); try { // SqlCon.Open(); // ElectricityBill eb = new ElectricityBill(); cmd = new SqlCommand("select top " + num + " * from ElectricityBill order by consumer_number desc", SqlCon); SqlDataReader r = cmd.ExecuteReader(); // DataSet d = new DataSet(); // d.Tables.Add("e"); // d.Tables["e"].Load(r); for (int i = 0; i < num; i++) { ElectricityBill eb = new ElectricityBill(); r.Read(); eb.ConsumerNumber = r[0].ToString(); eb.ConsumerName = r[1].ToString(); eb.UnitsConsumed = int.Parse(r[2].ToString()); eb.BillAmount = double.Parse(r[3].ToString()); list.Add(eb); } } finally { SqlCon.Close(); } return(list); }
static void Main(string[] args) { BillValidator bv = new BillValidator(); ElectricityBoard ebd = new ElectricityBoard(); ElectricityBill eb = null; List <ElectricityBill> l = new List <ElectricityBill>(); string pattern = @"(^[E][B][0-9]{5}$)"; Console.WriteLine("Enter Number of bills to be added:"); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { try { eb = new ElectricityBill(); Console.WriteLine("Enter Consumer Number:"); string cno = Console.ReadLine(); eb.ConsumerNumber = cno; bool check = Regex.IsMatch(cno, pattern); if (check == false) { throw new FormatException("Invalid Consumer Number"); } } catch (FormatException f) { Console.WriteLine(f.Message); break; } Console.WriteLine("Enter Consumer Name:"); string cname = Console.ReadLine(); eb.ConsumerName = cname; Console.WriteLine("Enter Units Consumed:"); int u = int.Parse(Console.ReadLine()); string s = bv.ValidateUnitsConsumed(u); if (s != null) { Console.WriteLine(s); Console.WriteLine("Enter Units Consumed:"); u = int.Parse(Console.ReadLine()); s = bv.ValidateUnitsConsumed(u); } else { eb.UnitsConsumed = u; ebd.AddBill(eb); } l.Add(eb); } Console.WriteLine("Enter Last 'N' Number of Bills To Generate:"); int N = int.Parse(Console.ReadLine()); List <ElectricityBill> list = ebd.Generate_N_BillDetails(N); Console.WriteLine(); for (int i = 0; i < l.Count; i++) { Console.WriteLine(l[i].ConsumerNumber + "\n" + l[i].ConsumerName + "\n" + l[i].UnitsConsumed + "\n" + "Bill amount:" + l[i].BillAmount); } Console.WriteLine(); Console.WriteLine("Details of last " + N + " bills:"); for (int j = 0; j < N; j++) { Console.WriteLine("Ebill for " + list[j].ConsumerName + " is " + list[j].BillAmount); //Console.WriteLine(); } Console.Read(); }