Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
user data have 2 variables (name, id), category can be like personal, official,etc.(with name and id of category), and notes will also have name and id and content. There's one to many relationship between user and notes and user and category, and category and notes and user.

I tried with making 3 different classes and then making objects of them where I want to use them.
(I am just a beginner so please excuse, if I've done any small mistakes.)

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Noteonetomany
{
    public class UserData
    {
        List<Note> Lst = new List<Note>();
        public string UserName { get; set; }
        public string UserId { get; set; }

        public List<Note> GetSetNote
        {
            get { return Lst; }
            set { Lst = value; }
        }
        public void PrintwithNote()
        {
            Console.WriteLine("UserName: "+this.UserName);
            Console.WriteLine("User ID :"+this.UserId);
            Console.WriteLine("Your Notes: ");
            foreach (var note in this.GetSetNote)
            {
                Console.WriteLine("Note Name:"+note.NoteName+"Note ID: "+note.NoteId);
            }
        }
        List<Category> Lst1 = new List<Category>();
        public string CategoryName { get; set; }
        public string CategoryId { get; set; }
        public List<Category> GetSetCategory
        {
            get { return Lst1; }
            set { Lst1 = value; }
        }
        public void PrintwithCategory()
        {
            Console.WriteLine("Name: "+this.UserName);
            Console.WriteLine("UserId : " + this.UserId);
            foreach (Category category in this.GetSetCategory)
            {
                Console.WriteLine("CategryName: " + category.CategoryName + " " + category.CategoryId);
            }
        }
    }

    public class Note
    {
        public string NoteName { get; set; }
        public string NoteId { get; set; }

        

        public void ChangeNoteName(List<Note> notes)
        {
            Console.WriteLine("Enter the existing note name : ");
            string existingnoteName = Console.ReadLine();
            Console.WriteLine("Enter the new name for this note:");
            string newnoteName = Console.ReadLine();
            for (int l = 0; l < notes.Count(); l++)
            {
                if (notes[l].NoteName == existingnoteName)
                {
                    notes[l].NoteName = newnoteName;
                }
            }
        }

        public void ChangeNoteID(List<Note> notes)
        {
            Console.WriteLine("Enter the existing note ID: ");
            string existingnoteID = Console.ReadLine();
            Console.WriteLine("Enter the new id for this note:");
            string newnoteID = Console.ReadLine();
            for (int j = 0; j < notes.Count(); j++)
            {
                if (notes[j].NoteId == existingnoteID)
                {
                    notes[j].NoteId = newnoteID;
                }
            }

        }
    }
    public class Category
    {
        UserData userData = new UserData();
        List<Note> Lst = new List<Note>();
        public List<Note> Lst2 = new List<Note>();
        public List<Note> GetSetNote
        {
            get { return Lst2; }
            set { Lst2 = value; }
        }
        public void PrintwithCategory()
        {
            Console.WriteLine("UserName: " + userData.UserName);
            Console.WriteLine("User ID :" + userData.UserId);
            Console.WriteLine("Your Notes: ");
            foreach (Note note in this.GetSetNote)
            {
                Console.WriteLine("Note Name:" + note.NoteName + "Note ID: " + note.NoteId);
            }
        }

        public string CategoryName { get; set; }
        public string CategoryId { get; set; }

        public void ChangeCategoryName(List<Category> categories)
        {
            Console.WriteLine("Enter the existing category name : ");
            string existingcategoryName = Console.ReadLine();
            Console.WriteLine("Enter the new name for this category:");
            string newcategoryName = Console.ReadLine();
            for (int k = 0; k < categories.Count(); k++)
            {
                if (categories[k].CategoryName == existingcategoryName)
                {
                    categories[k].CategoryName = newcategoryName;
                }
            }
        }
        public void ChangeCategoryID(List<Category> categories)
        {
            Console.WriteLine("Enter the existing category ID: ");
            string existingcategoryID = Console.ReadLine();
            Console.WriteLine("Enter the new id for this category:");
            string newcategoryID = Console.ReadLine();
            for (int j = 0; j < categories.Count(); j++)
            {
                if(categories[j].CategoryId == existingcategoryID)
                {
                    categories[j].CategoryId = newcategoryID;
                }
            }
            
        }
        
    }
    internal class Program
    {
        static void Main(string[] args)
        {   
            List<UserData> users = new List<UserData>();
            List<Category> categories = new List<Category>();
            List<Note> notes = new List<Note>();
            UserData userDataobj = new UserData();
            //Category categoryobj = new Category();
            Console.WriteLine("Press 'a' to add notes");
            Console.WriteLine("Press 'e' to Edit data");
            //Console.WriteLine("Press 's' to Search data");
            //Console.WriteLine("Press 'S' to Show all details");
            //Console.WriteLine("Press 'D' to Delete all data");
            //Console.WriteLine("Press 'u' to delete username:");
            //Console.WriteLine("Press 'n' to delete note by note name:");
            //Console.WriteLine("Press 'c' to delete categeory");
            Console.WriteLine("Press 'X' to exit.");
            while (true)
            {
                var userinput = Console.ReadLine();
                switch (userinput)
                {
                    case "a":
                        Console.WriteLine("Enter user name: ");
                        var username = Console.ReadLine();
                        Console.WriteLine("Enter user id: ");
                        var userid = Console.ReadLine();
                        users.Add(new UserData
                        {
                            UserName = username,
                            UserId = userid
                        });
                        Console.WriteLine("Enter category name: ");
                        var categoryname  = Console.ReadLine();
                        Console.WriteLine("Enter category id: ");
                        var categoryid = Console.ReadLine();
                        categories.Add(new Category
                        {
                            CategoryId = categoryid,
                            CategoryName = categoryname
                        });
                        Console.WriteLine("Enter notes name: ");
                        var notename = Console.ReadLine();
                        Console.WriteLine("Enter Notes ID: ");
                        var noteid = Console.ReadLine();
                        notes.Add(new Note
                        {
                            NoteName = notename,
                            NoteId = noteid
                        });
                        break;
                    case "s":
                        userDataobj.PrintwithNote();
                        
                        break;
                    case "e":

                        break;
                    case "x":

                        return;
                }
            }
        }
    }
}
Posted
Updated 28-Apr-22 3:14am
v2
Comments
Ralf Meier 28-Apr-22 8:29am    
I don't understand ... try to explain the relationship and the maximum content.
I suppose that you will need a main-class, which holds the properties, and some sub-classes, corresponding to the properties ...

1 solution

Stop trying to write it all at once, and think about what you are doing and the data you need to store.

You have a User: so create a basic class for him.
C#
public class User
   {
   public string Name {get; set;}
   public string Id {get; set;}
   }
Now write code to hold a collection of users, and then write a method to add new new user name and ID to it.

Write code to print the user details.

Now write code to main to add a number of users by calling AddUser several times, then use your print details method to show it works.
Test it.

Then repeat the process for the Note and Category classes.

When they are all working, add the Note and Category collections to the User, and add the code to support them.

Do it a stage at a time, testing as you go so at each stage you know that what you are working with is correct.

This may help: help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900