.net 5,6
Inheritence
using System;
using System.Collections.Generic;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void DisplayPersonInfo()
{
Console.WriteLine("\n------------------------------");
Console.WriteLine("Name: " + Name);
Console.WriteLine("Age: " + Age);
}
}
class Student : Person
{
public int StudentID { get; set; }
public string Course { get; set; }
public void DisplayStudentInfo()
{
DisplayPersonInfo();
Console.WriteLine("Student ID: " + StudentID);
Console.WriteLine("Course: " + Course);
Console.WriteLine("------------------------------");
}
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>();
string choice;
do
{
// Create a new Student object
Student student = new Student();
// Prompt user for input
Console.Write("\nEnter Student Name: ");
student.Name = Console.ReadLine();
Console.Write("Enter Student Age: ");
student.Age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Student ID: ");
student.StudentID = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Course: ");
student.Course = Console.ReadLine();
// Add student to the list
students.Add(student);
// Ask user if they want to continue
Console.Write("\nDo you want to enter another student? (yes/no): ");
choice = Console.ReadLine().ToLower();
} while (choice == "yes");
// Display all student details
Console.WriteLine("\n========== All Student Details ==========");
foreach (Student s in students)
{
s.DisplayStudentInfo();
}
Console.WriteLine("\nPress Enter to Exit...");
Console.ReadLine(); // Wait for user input before closing
}
}
Interfaces
using System;
using System.Collections.Generic;
interface IBill
{
void GenerateBill();
}
class SalesBill : IBill
{
private List<string> itemNames = new List<string>();
private List<int> quantities = new List<int>();
private List<double> prices = new List<double>();
private double totalAmount = 0;
public void AddItem(string itemName, int quantity, double price)
{
itemNames.Add(itemName);
quantities.Add(quantity);
prices.Add(price);
totalAmount += quantity * price;
}
public void GenerateBill()
{
Console.WriteLine("\n========== SALES BILL ==========");
Console.WriteLine("{0,-20} {1,-10} {2,-10} {3,-10}", "Item Name", "Quantity", "Price", "Total");
Console.WriteLine("---------------------------------------------");
for (int i = 0; i < itemNames.Count; i++)
{
double totalItemPrice = quantities[i] * prices[i];
Console.WriteLine("{0,-20} {1,-10} {2,-10} {3,-10}", itemNames[i], quantities[i], prices[i], totalItemPrice);
}
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Grand Total: {0:C}", totalAmount);
Console.WriteLine("=============================================");
}
}
class Program
{
static void Main()
{
SalesBill bill = new SalesBill();
string choice;
do
{
Console.Write("\nEnter Item Name: ");
string itemName = Console.ReadLine();
Console.Write("Enter Quantity: ");
int quantity = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Price per Unit: ");
double price = Convert.ToDouble(Console.ReadLine());
bill.AddItem(itemName, quantity, price);
Console.Write("\nDo you want to add another item? (yes/no): ");
choice = Console.ReadLine().ToLower();
} while (choice == "yes");
bill.GenerateBill(); // Display the final bill
Console.WriteLine("\nPress Enter to Exit...");
Console.ReadLine();
}
}
Comments
Post a Comment