i have a simple Library management program in C#,can anyone please correct the errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gladiator69
    New Member
    • Apr 2012
    • 5

    i have a simple Library management program in C#,can anyone please correct the errors

    This is a program of maintaining the various work carried out in a library. My syntax and use of classes is wrong. Could anyone please correct the errors in the program and also handle the exceptions for me. I need it ASAP. Thanx a lot...

    Code:
        class Program
        {
            static public int[] student = new int[100];
            static public int[] i = new int[5];
            static public int[] b = new int[5];
            static public int issuelimit = 2;
            static public string bookname;
            static public int id; 
            static public int bookcount;
            static public int bookid;
            static public int choice=0;
            static public char Repeat;
    
            static void Main(string[] args)
            {
                int choice=1;
                Console.WriteLine(" What do u want to do?\n");
                Console.WriteLine("1.Add a book to the Library");
                Console.WriteLine("2.Register a user ");
                Console.WriteLine("3.Issue a book");
                Console.WriteLine("4.Display list of Registered user");
                Console.WriteLine("5.Display list of books in library");
                Console.WriteLine("6.Exit");
    
                choice = Convert.ToInt16(Console.ReadLine());        
                           
                LibMng lb = new LibMng();
                switch (choice)
                {
                    case 1:
                        {   
                            do
                            {
                            Console.WriteLine("Enter the name of the book and the author's name: ");
                            string bookname = Convert.ToString(Console.ReadLine());
                            string author = Convert.ToString(Console.ReadLine());
                            bookcount++;
                            bookid++;
                                Console.WriteLine("\nDo you wish return more from the same card??(y/n)");
                                    Repeat = Convert.ToChar(Console.ReadLine());
                            } 
                                while (Repeat == 'y' || Repeat == 'Y');
                                break;                      
                        }
                    case 2:
                        {
                            Console.WriteLine("Enter the name and address of user: ");
                            string studentname = Convert.ToString(Console.ReadLine());
                            string studentadd = Convert.ToString(Console.ReadLine());
                            Console.WriteLine("Enter the card ID");
                            id = Convert.ToInt16(Console.ReadLine());
                                do
                                {
                                    if (student[id] != 0)
                                    {
                                        lb.bookReturn();
                                    }
                                    else
                                    {
                                        Console.WriteLine("Till no book is issued to this card");
                                        break;
                                    }
                                    Console.WriteLine("\nDo you wish return more from the same card??(y/n)");
                                    Repeat = Convert.ToChar(Console.ReadLine());
                                } 
                                while (Repeat == 'y' || Repeat == 'Y');
                                break;                      
                        }
                    case 3:
                        {
                            Console.WriteLine("Enter the Student id");
                            id = Convert.ToInt16(Console.ReadLine());
                            do
                            {
                                if (student[id] < 2)
                                {
                                    lb.Issue();
                                }
                                else
                                {
                                    Console.WriteLine("Sorry no more books can be issued");
                                    break;
                                }
    
                                Console.WriteLine("\nDo you wish to issue more to the same card??(y/n)");
                                Repeat = Convert.ToChar(Console.ReadLine());
                            }
                            while (Repeat == 'y' || Repeat == 'Y');
                            break;
                        }
                    case 4:
                        {
                        do
                                    {
                                        Console.WriteLine("the list of registered student is:");
                                        foreach (int a in Student)
                                        {
                                            Console.WriteLine("Name: ", a.studentname);
                                            Console.WriteLine("StudentId: {0}", a.studentid);
                                            Console.WriteLine("Number of books issued: {0}", a.NoOfBooksIssues);
                                        }
                                        Console.WriteLine("do you wish to continue??(y/n)");
                                        
                                    } while (Repeat == 'y' || Repeat == 'Y');
                            break;
                        }
                    case 5:
                        {
                                    do
                                    {
                                        Console.WriteLine("the list of books in library:");
                                        foreach (int b in BookLibrary)
                                        {
                                            Console.WriteLine("Book Title: {0}", b.bookname);
                                            Console.WriteLine("Book Id: {0}", b.bookid);
                                            Console.WriteLine("Author name: {0}", b.author);
                                        }
                                        Console.WriteLine("do you wish to continue??(y/n)");
                                                                         
                                    } while (Repeat == 'y' || Repeat == 'Y');
                            break;
                        }                
                    case 6:
                        {
                            break;
                        }
    
                }
                        Console.ReadLine();
                }
            }
            class LibMng : Program
            {
                public void Issue()
                {                
                    Console.WriteLine("Enter The Book id ");
                    int reqbook = Convert.ToInt32(Console.ReadLine());
                    if (reqbook == bookid)
                    {
                        if (i[0] != 0)
                        {
                            i[0]--;
                            Console.WriteLine("The Book is issued to Card{0}", id);
                            student[id]++;
                        }
                        else
                            Console.WriteLine("Book is not available");
                    }
                }
                public void display()
                {
                    Console.WriteLine("the books taken are: ");
                }
    
                public void chckAvail()
                {
                    Console.WriteLine("Enter The Book id");
                    int req = Convert.ToInt32(Console.ReadLine());
                    if (req == bookid)
                    {
                        if (i[0] != 0)
                        {
                            Console.WriteLine("The number of copy of the Book is::{0}", i[0]);
                        }
                        else
                            Console.WriteLine("Book is not available");
                    }
                }
            }
    
                class BookLibrary: Program
                {
                    public int bookcount;
                    public int bookid;
                    public string bookname;
                    public string booktitle;
    
                    public BookLibrary(int bookcount, int bookid, string bookname, string booktitle)
                    {
                        this.bookcount = bookcount;
                        this.bookid = bookid;
                        this.bookname = bookname;
                        this.booktitle = booktitle;
                    }               
                    
                }
                class Student
                {
                    public int studentcount;
                    public int studentid;
                    public string studentname;
                    public int noOfbookissues = 0;
    
                    public Student(int studentcount, int studentid, string studentname, int noOfbooksissued)
                    {
                        this.studentcount = studentcount;
                        this.studentid = studentid;
                        this.studentname = studentname;
                        this.noOfbookissues = noOfbookissues;
                    }
                }
                class IssueReturn: Program
                {            
                    public void bookReturn()
                    {
                    Console.WriteLine("Enter The Book ID");
                    int req1 = Convert.ToInt32(Console.ReadLine());
    
                    if (req1==bookid)
                    {
                        if (i[0] != 1)
                        {
                        i[0]++;
                        Console.WriteLine("The Book is returned from Card {0}", id);
                        student[id]--;
                        }
                    else
                        Console.WriteLine("Till the Book is not issued to this Card");
                    }
                    }
                }
            }
        }
  • RhysW
    New Member
    • Mar 2012
    • 70

    #2
    There is so much wrong here that the intellisense picks up on and tells you about, have you even tried doing this yourself?
    have you looked up about error catching? Were here to help solve problems that you explain to us, not to just give a lump of code and correct for you,

    this is a learning environment, not a give us your code and we do it for you environment.

    Now i don't like purely cynical posts so i'll help you out a bit here, first of all, in your student constructor youre assigning the properties to itself, you dont need to use this. on everything and youre variable names are so similar that youre getting confused and assigning things to the wrong places,

    on the exception front i just answered in depth a question about error trapping and how to deal with it in this topic, you might want to check it out

    but again, we prefer the motto "Give a man a fish and he feeds for a day, Teach a man to fish he feeds for the rest of his life" So im not being deliberately awkward, im trying to teach you to deal with your own problems, so if you come across the same one again you can do it yourself instead of having to rely on us. Hope you can see the advantage in what im doing!

    RhysW

    Comment

    • gladiator69
      New Member
      • Apr 2012
      • 5

      #3
      thank you for the suggestion...am doing C# for the first time and have very less knowledge about classes and functions...so. .. thank you for the help

      Comment

      • RhysW
        New Member
        • Mar 2012
        • 70

        #4
        thats fine, were all beginners at one point! im still very very early on in my c# knowledge, only just over 2 months, youll pick it up fast if you stick to it and try to work through your problems!

        Comment

        • gladiator69
          New Member
          • Apr 2012
          • 5

          #5
          The thing is that our mentor has completed teaching us C# in less than 2 weeks and now from tomorrow itself we are starting with html.....so its very hard to cope up and the particular program was part of a diagnostics.... so i want to get myself clear with the classes and exception stuffs so wanted a correct version of the program. Never the less both ur suggestions were of great help to me. Hope to get more knowledge from u in the future. Thank u once again.

          Comment

          • rekedtechie
            New Member
            • Feb 2012
            • 51

            #6
            maybe try to use the get; set; ..?

            public string stud_name { get; set; }
            Last edited by rekedtechie; Apr 20 '12, 04:42 AM. Reason: shorten reply. :)

            Comment

            Working...