Page 1 of 1

Description In this exercise we are going to create an in-memory repository of "User" and "User Profile" objects in a li

Posted: Sun May 15, 2022 8:18 am
by answerhappygod
Description In This Exercise We Are Going To Create An In Memory Repository Of User And User Profile Objects In A Li 1
Description In This Exercise We Are Going To Create An In Memory Repository Of User And User Profile Objects In A Li 1 (30.14 KiB) Viewed 37 times
Description In this exercise we are going to create an in-memory repository of "User" and "User Profile" objects in a list based database which allows us to create a user and their profile, search for a user, update their profile, and delete them completely Components The application consists of 3 components, the User class which defines the user, the UserProfile class which contains a user's profile detail, and the UserDatabase which consists of a list of users. These files can be found under the "Database" directory Details of each class User The user class consists of 3 public fields, id Username, and Password. These properties should correctly use the et and init methods. The id should be autogenerated and cannot be changed later. This class and its method overrides is provided to you. Properties: • id: int • Username: string • Password: string User Profile

User Profile This class contains the detailed information about a user. The fields are, Id, Userld, FirstName, LastName, DOB. The id should be autogenerated and cannot be changed later. Similar to the user class, the fields need to be set by the get and init methods. This class and its properties has been provided to you. Properties • Id: int • Userld: int • FirstName: string LastName: string DOB: string Override the tostrine and Equats method similar to what's in the User class. UserDatabase This class consists of a list of Users and a list of User Profiles. The database will provide the CRUD (Create, Read, Update, and Delete) operations to add, search for, update, and delete a user from the list it maintains. The client can Create an instance of UserDatabase and add users and profiles to it. It can also, retrieve, update, and delete the data ONLY if the requester can provide the correct username and password. The example later will show how the whole process should work. The fields and methods of UserDatabase are listed below Properties • Users: List<Users get only • User Profiles List User Profile-get only Methods

Methods • AddUser(string username, string password); int returns the added user's id - throws UserAlreadyExistsException if a user with the username already exists in the list . AddUser Profile string username, string password, string firstName, string lastName, string DOB): int returns the newly added user profile's id-throws User ProfileAlreadyExistsException if a user with the user name in the profile exists in the list of user profiles and throws UserNotFoundException if the username/password pair don't match with any user in the database. Find UsersWithFirstNamel string): List<User returns a list of users with the first name provided. Returns an empty list if nothing is found Find UsersWithLastNamel strine): List<User returns a list of users with last name provided. Empty if nothing found • Update Profile( string username, string password, string firstName, string lastName, string DOB) updates the users's name and DOBIFF the user with username password pair exist otherwise throws a User NotFoundException If the User Profile does not exist for that user, ProfileNotFoundException is thrown. The ID of the profile increments by one even in the case of update • Deletel string username, string password) deletes a user and its corresponding profile IFF the username-password pair matches a user in the database otherwise it throws a UserNotFoundException

deletes a user and its corresponding profile IFF the username-password pair matches a user in the database otherwise it throws a UsertlotFoundException Immutability The properties of User and User Profile can only be set with a constructor or with init. This means that the properties look like this: FirstName (eet; init;} D In order to update a profile, the best course of action is to create a new one and delete the existing one Internal ID fields The id of users and user profiles should auto increment every time a new one is added to the database. Make private variables in the database class to keep track of those. Both IDs should start from 1. You may add any additional field or private method you need to implement the classes but the interface of the class should remain the same Fail safety Watchout for method arguments that aren't useful eg empty string for username and password. In case of unacceptable input simply return from the method and don't add any records to the database, Usage examples

Usage examples The following is a simple usage example of the database in the main method. UserDatabase db = new UserDatabase(); db.Adduser("user", "1234"); db.AdduserProfile("user1","1234", "John Doe", 1998/01/10); db. Adduser("user?". "1234"): db.AddUser Profile("user", "1234", "Jane", "Doe", "1997/11/12"); List<Users allusers - db.GetAllusers(); foreach (User u in allusers) Console.WriteLine(): 2 D List User Profile profiles - db.GetAllUser Profiles): foreach (User Profile profile in profiles) Console.Writeline(profile) > foreach(User u in db FindUsersWithFirsthame("on">) Console.WriteLine();

1 2 3 4 namespace Database; public class UserProfile { public int Id (get; init;} public int UserId (get; init;} public string? FirstName {get; init;} public string? LastName {get; init;) public string? DOB (get; Init;} 5 6 2 B 9 10 11 public override string ToString() { return $"{1d)/(UserId): {FirstName), (LastName} born (DOB)"; ) 12 13 14 }

5 lines (4 sloc) 54 Bytes 1 2 namespace Database; public class UserDatabase { 3 4 5 }

19 lines (17 sloc) 427 Bytes 1 2 3 namespace Database; public class User 4 5 6 public int Id (get; init;} public string Username (Bet; init;} public string Password (get; init;} 7 8 9 1e public User(int Id, string Username, string Password) { this. IdId; this.Username - Username this.Password - Password; } 11 12 13 14 15 public override string ToString() 16 17 return $"id: {Ia), un:{Username), pw: (Password)" 18 19 3