0% found this document useful (0 votes)
9 views

cpp.net

Uploaded by

mprachi853
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

cpp.net

Uploaded by

mprachi853
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIVERSITY INSTITUTE OF ENGINEERING

Department of Computer Science & Engineering


(BE-CSE/IT-5th Sem)

Subject Name: C#.net


(In house Summer Training)
Assignment No-3

Submitted to: Submitted by:


Faculty Name: Er. Roop Sharma Name: Ashish
Verma
UID:22BCS12556
Section: iot-612
Group-B
Problem Statement 1: - Create a to-do list application in C# where users can add, remove, and manage
tasks. The application should allow users to mark tasks as completed, prioritize tasks, and view tasks based on
different criteria. • Requirements: • Users should be able to add tasks to the to-do list. • Users should be able to
remove tasks from the to-do list. • Users should be able to mark tasks as completed. • Users should be able to
prioritize tasks. • Users should be able to view tasks based on different criteria (e.g., all tasks, completed tasks, tasks
by priority). • The application should have a user-friendly interface for interacting with the to-do list.

SOURCE CODE: -
using System;
using
System.Collections.Generic;
using System.Linq;

class Task
{
public string Description { get; set;
} public bool IsCompleted { get;
set; } public int flriority { get; set; }

public Task(string description, int priority)


{
Description =
description;
IsCompleted = false;
flriority = priority;
}

public override string ToString()


{
return $"{Description} (flriority: {flriority}, Completed: {IsCompleted})";
}
}

class flrogram
{
static List<Task> tasks = new List<Task>();

static void Main(string[] args)


{
while (true)
{
ShowMenu();
string choice =
Console.ReadLine(); switch
(choice)
{
case "1":
AddTask();
break;
case "2":
RemoveTask();
break;
case "3":
MarkTaskAsCompleted();
break;
case "4":
ViewTasks();
break;
case "5":
Environment.Exit(0)
; break;
default:
Console.WriteLine("Invalid choice, please try
again."); break;
}
}
}

static void ShowMenu()


{
Console.WriteLine("\nTo-Do List
Application"); Console.WriteLine("1. Add
Task"); Console.WriteLine("2. Remove
Task"); Console.WriteLine("3. Mark Task as
Completed"); Console.WriteLine("4. View
Tasks"); Console.WriteLine("5. Exit");
Console.Write("Enter your choice: ");
}

static void AddTask()


{
Console.Write("Enter task description:
"); string description =
Console.ReadLine();
Console.Write("Enter task priority (1-5):
"); int priority;
while (!int.Tryflarse(Console.ReadLine(), out priority) || priority < 1 || priority
>
5)
{
Console.Write("Invalid priority. Enter task priority (1-5): ");
}
tasks.Add(new Task(description,
priority)); Console.WriteLine("Task added
successfully.");
}

static void RemoveTask()


{
Console.Write("Enter task description to
remove: "); string description =
Console.ReadLine();
Task taskToRemove = tasks.FirstOrDefault(t => t.Description ==
description); if (taskToRemove != null)
{
tasks.Remove(taskToRemove);
Console.WriteLine("Task removed
successfully.");
}
else
{
Console.WriteLine("Task not found.");
}
}

static void MarkTaskAsCompleted()


{
Console.Write("Enter task description to mark as completed:
"); string description = Console.ReadLine();
Task taskToMark = tasks.FirstOrDefault(t => t.Description ==
description); if (taskToMark != null)
{
taskToMark.IsCompleted = true;
Console.WriteLine("Task marked as completed.");
}
else
{
Console.WriteLine("Task not found.");
}
}

static void ViewTasks()


{
Console.WriteLine("\nView Tasks");
Console.WriteLine("1. All Tasks");
Console.WriteLine("2. Completed
Tasks"); Console.WriteLine("3.
flending Tasks");
Console.WriteLine("4. Tasks by
flriority"); Console.Write("Enter your
choice: "); string choice =
Console.ReadLine(); List<Task>
tasksToView = new List<Task>();
switch (choice)
{
case "1":
tasksToView = tasks;
break;
case "2":
tasksToView = tasks.Where(t =>
t.IsCompleted).ToList(); break;
case "3":
tasksToView = tasks.Where(t => !
t.IsCompleted).ToList(); break;
case "4":
tasksToView = tasks.OrderBy(t =>
t.flriority).ToList(); break;
default:
Console.WriteLine("Invalid choice, showing all
tasks."); tasksToView = tasks;
break;
}
Console.WriteLine("\
nTasks:"); foreach (var task in
tasksToView)
{
Console.WriteLine(task);
}
}
}
OUTPUT: -

▪ Problem Statement 2: - A University needs a system to manage student enrollments for


courses each semester. The system should allow administrators to add new students, add
courses, enroll students in courses, and view enrolled students for each course. Requirements: •
Add New Students: Administrators should be able to add new students to the system along with
their personal information such as name, ID, and contact details. • Add Courses: Administrators
should be able to add new courses to the system along with course details such as course code,
title, and maximum capacity. • Enroll Students: Administrators should be able to enroll students
in courses. Each student can be enrolled in multiple courses, and each course can have multiple
students enrolled. There should be checks to ensure that a course does not exceed its maximum
capacity. • View Enrolled Students: Administrators should be able to view a list of students
enrolled in each course.

SOURCE CODE:-
using System;
using
System.Collections.Generic;
public class Student
{
public string ame { get; set; }
public string ID { get; set; }
public string ContactDetails { get; set; }
}
public class Course
{
public string Code { get; set; }
public string Title { get; set; }
public int MaximumCapacity { get; set; }
public List<Student> EnrolledStudents { get; set; }
public Course(string code, string title, int maximumCapacity)
{
Code = code;
Title = title;
MaximumCapacity =
maximumCapacity; EnrolledStudents
= new List<Student>();
}
public void EnrollStudent(Student student)
{
if (EnrolledStudents.Count < MaximumCapacity)
{
EnrolledStudents.Add(student);
Console.WriteLine($"Student {student. ame} enrolled in course
{Code} successfully.");
}
else
{
Console.WriteLine($"Course {Code} is full. Cannot enroll student
{student. ame}.");
}
}
public void ViewEnrolledStudents()
{
Console.WriteLine($"Enrolled students in course {Code}:");
foreach (var student in EnrolledStudents)
{

Console.WriteLine($" ame: {student. ame}, ID: {student.ID}, Contact


Details:
{student.ContactDetails}");
}
}
}
public class flrogram
{
private static List<Student> students = new
List<Student>(); private static List<Course> courses
= new List<Course>(); public static void
Main(string[] args)
{
while (true)
{
Console.WriteLine("1. Add ew Student");
Console.WriteLine("2. Add ew Course");
Console.WriteLine("3. Enroll Student in Course");
Console.WriteLine("4. View Enrolled Students in
Course"); Console.WriteLine("5. Exit");
int option =
Convert.ToInt32(Console.ReadLine()); switch
(option)
{
case 1:
AddStudent();
break;
case 2:
AddCourse();
break;
case 3:
EnrollStudent();
break;
case 4:
ViewEnrolledStudents();
break;
case 5:
return;
default:
Console.WriteLine("Invalid option. fllease choose a valid
option."); break;
}
}
}
private static void AddStudent()
{
Console.Write("Enter student name:
"); string name =
Console.ReadLine();
Console.Write("Enter student ID:
"); string id = Console.ReadLine();
Console.Write("Enter student contact details: ");
string contactDetails = Console.ReadLine();
students.Add(new Student { ame = name, ID = id, ContactDetails =
contactDetails }); Console.WriteLine("Student added successfully.");
}
private static void AddCourse()
{
Console.Write("Enter course code:
"); string code =
Console.ReadLine();
Console.Write("Enter course title:
"); string title =
Console.ReadLine();
Console.Write("Enter course maximum capacity: ");
int maximumCapacity = Convert.ToInt32(Console.ReadLine());

courses.Add(new Course(code, title, maximumCapacity));


Console.WriteLine("Course added successfully.");
}
private static void EnrollStudent()
{
Console.Write("Enter student ID:
"); string studentId =
Console.ReadLine();
Console.Write("Enter course
code: "); string courseCode =
Console.ReadLine();
Student student = students.Find(s => s.ID ==
studentId); Course course = courses.Find(c =>
c.Code == courseCode); if (student != null fifi
course != null)
{
course.EnrollStudent(student);
}
else
{
Console.WriteLine("Student or course not found.");
}
}
private static void ViewEnrolledStudents()
{
Console.Write("Enter course code:
"); string courseCode =
Console.ReadLine();
Course course = courses.Find(c => c.Code ==
courseCode); if (course != null)
{
course.ViewEnrolledStudents();
}
else
{
Console.WriteLine("Course not found.");
}
}
}
OUTPUT:-

Problem Statement 3: - Implement the Student class with proper encapsulation. Ensure
the Grades property is protected from unauthorized access and modifications.

SOURCE CODE: -
using System;
using System.Collections.Generic;
namespace UniversityEnrollmentSystem
{
public class Student
{
private string id;
private string name;
private string
contact; private
List<int> grades;
public Student(string id, string name, string contact)
{
this.id = id;
this.name = name;
this.contact = contact;
this.grades = new
List<int>();
}
public string Id
{
get { return id; }
set { id = value;
}
}
public string ame
{
get { return name; }
set { name = value; }
}
public string Contact
{
get { return contact;
} set { contact =
value; }
}
public void AddGrade(int grade)
{
if (grade >= 0 fifi grade <= 100)
{
grades.Add(grade);
}
els
e
{ throw new ArgumentOutOfRangeException("Grade must be between 0 and
100.");
}

}
public IReadOnlyList<int> GetGrades()
{
return grades.AsReadOnly();
}
public double GetAverageGrade()
{
if (grades.Count == 0)
{
return 0;
}
double sum = 0;
foreach (int grade in grades)
{
sum += grade;
}
return sum / grades.Count;
}
public override string ToString()
{
return $"{ ame} ({Id}) - Contact: {Contact}";
}
}
class flrogram
{
static void Main(string[] args)
{
Student student = new Student("1", "Ashish",
"[email protected]"); student.AddGrade(90);
student.AddGrade(09);
student.AddGrade(98);
Console.WriteLine(student);
Console.WriteLine("Grades: " + string.Join(", ", student.GetGrades()));
Console.WriteLine("Average Grade: " + student.GetAverageGrade());
}
}
}
OUTPUT:-

Problem Statement 4: Imagine a company that handles sensitive customer data, including personal
information and payment details. The company must ensure this data is secure and only accessible to authorized
personnel. Additionally, the system must be designed to comply with data privacy regulations, such as GDPR. Key
Requirements: • Secure Data Storage: Customer data should be stored securely, with sensitive fields protected. •
Controlled Access: Only authorized personnel should be able to access or modify sensitive data. • Audit Logs: All
access and modifications should be logged for auditing purposes

SOURCE CODE: -
using System;
using
System.Collections.Generic;
using System.IO;
using
System.Security.Cryptography;
using System.Text;
namespace SecureData andling
{

public class Customer


{
public int Id { get; set; }
public string ame { get; set;
} private string
encryptedData;

public void SetSensitiveData(string sensitiveData, string encryptionKey)


{
encryptedData = Encrypt(sensitiveData, encryptionKey);
}

public string GetSensitiveData(string encryptionKey)


{
return Decrypt(encryptedData, encryptionKey);
}

private static string Encrypt(string data, string key)


{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Encoding.UTF8.GetBytes(key.fladRight(32));
aesAlg.IV = new byte[16];
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key,
aesAlg.IV); using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt,
encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(data);

}
return Convert.ToBase64String(msEncrypt.ToArray());
}
}
}

private static string Decrypt(string encryptedData, string key)


{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Encoding.UTF8.GetBytes(key.fladRight(32));
aesAlg.IV = new byte[16];
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key,
aesAlg.IV); using (MemoryStream msDecrypt = new
MemoryStream(Convert.FromBase64String(encryptedData)))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}

public class User


{
public string Username { get; set;
} public string Role { get; set; }
public class CustomerDataManager
{
private readonly string encryptionKey;
private readonly Dictionary<int, Customer> customers = new
Dictionary<int, Customer>();
private readonly List<string> auditLogs = new
List<string>(); public CustomerDataManager(string
encryptionKey)
{
this.encryptionKey = encryptionKey;
}

public void AddCustomer(User user, Customer customer)


{
if (user.Role == "Admin")
{
customers[customer.Id] = customer;
LogAccess(user, $"Added customer
} {customer.Id}");
els
e
{
throw new UnauthorizedAccessException("Only admins can add customers.");
}
}

public string GetCustomerSensitiveData(User user, int customerId)


{
if (user.Role == "Admin" || user.Role == "User")
{
if (customers.ContainsKey(customerId))
{
LogAccess(user, $"Accessed sensitive data for customer
{customerId}"); return
customers[customerId].GetSensitiveData(encryptionKey);
}
else
{
throw new Key otFoundException("Customer not found.");
}
}
else
{
throw new UnauthorizedAccessException("Access denied.");
}
}

private void LogAccess(User user, string action)


{
string logEntry = $"{DateTime. ow}: {user.Username} ({user.Role}) -
{action}"; auditLogs.Add(logEntry);
Console.WriteLine(logEntry);
}

public IReadOnlyList<string> GetAuditLogs()


{
return auditLogs.AsReadOnly();
}
}
class flrogram
{
static void Main(string[] args)
{
string encryptionKey = "your-encryption-key";
var dataManager = new
CustomerDataManager(encryptionKey); var admin = new
User { Username = "admin", Role = "Admin" }; var
user = new User { Username = "user", Role =
"User" }; var customer = new Customer { Id = 1,
ame = "John Doe" };
customer.SetSensitiveData("Sensitive Data: 1234-5678-9012-3456",
encryptionKey); dataManager.AddCustomer(admin, customer);
{
string sensitiveData =
customer.Id
); dataManager.GetCustomerSensitiveData(user,

} Console.WriteLine($"Sensitive Data: {sensitiveData}");

catch (UnauthorizedAccessException ex)


{

Console.WriteLine(ex.Message);
}

var auditLogs =
dataManager.GetAuditLogs(); foreach
(var log in auditLogs)
{
Console.WriteLine(log);
}
}
}
}

OUTPUT: -

Problem Statement 5: Consider a scenario where you have a base class Vehicle and a derived class Car.
Both classes have constructors, and you want to initialize properties of the base class in the derived class's
constructor. Challenge: • In the Car class, you have two constructors. One constructor initializes both the brand and
model, while the other initializes only the brand. How can you avoid duplicating the initialization of the brand
property in the second constructor while still ensuring proper initialization of both properties?

SOURCE CODE: -
using

System;

class

Vehicle
{
public string Brand { get; set; }

public Vehicle(string brand)


{
Brand = brand;
Console.WriteLine("Vehicle constructor called.");
}
}

class Car : Vehicle


{
public string Model { get; set; }

public Car(string brand, string model) : base(brand)


{
Model = model;
Console.WriteLine("Car constructor (brand and model) called.");
}
public Car(string brand) : this(brand, "Unknown Model")
{
Console.WriteLine("Car constructor (brand only) called.");
}
}

class flrogram
{
static void Main(string[] args)
{
Car car1 = new Car("Toyota", "Corolla");
Console.WriteLine($"Car 1: Brand = {car1.Brand}, Model = {car1.Model}");

Car car2 = new Car(" onda");


Console.WriteLine($"Car 2: Brand = {car2.Brand}, Model = {car2.Model}");
}
}

OUTPUT: -

You might also like