Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there I am new at C#. what I have to do is to read text file and creat an object of Book class in one class and save all the book class objects in the Queue data structure in another class. I am not sure If I did right. Can anyone please help me. Please and thank you.

What I have tried:

Class to read from the text file
C#
public Queue<book> readBookData()
        {
               Queue<book> books = new Queue<book>();
               StreamReader sr = new StreamReader("Books.txt");
          
            string line = "";
            while((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                Book bk = new Book();
                bk.Author = words[0];
                bk.Book_Name = words[1];
                bk.Publisher = words[2];
                bk.Year = int.Parse(words[3]);
                bk.Category = words[4];

                books.Enqueue(bk);
            }
            return books;
        }

//-----------------------------------------------------------------------
// The data Structure class

        static Queue<book> books = new Queue<book>();
        public Queue<book> readBooks()
        {
            return books;
        }

        public void addBook(Book b)
        {
            books.Enqueue(b);
        }

        public Book removeBook()
        {
            return books.Dequeue();
        }
Posted
Updated 16-Nov-18 22:09pm
v2

I would recommend using a List<>, something like this:
IList<Book> bookList = new List<Book>() { 
                new Book(){ ID=1, Author="Bill Gates"},
                new Book(){ ID=2, Author="Steve Jobs"},
                new Book(){ ID=3, Author="Ronald McDonald"},
                new Book(){ ID=4, Author="John Doe"}
            };
A List<> is more versatile than a Queue, you can Add, Insert and Delete etc.
Also see examples here: https://www.dotnetperls.com/initialize-list[^]
 
Share this answer
 
v4
Comments
Ron23111 16-Nov-18 16:46pm    
yes but I have to do in the principle of FIRST-COME-FIRST-SERVE basis.
Here is an example with all code in a Form, it would be cleaner though to split up the classes into separate files, this is also recommended by Microsoft.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonFill_Click(object sender, EventArgs e)
        {
            BooksHelper.Books = readBookData();
            Debug.Print("Total books = " + BooksHelper.Books.Count);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            AnotherBookClass.WriteAllBooks("books2.txt");
        }

        public Queue<Book> readBookData()
        {
            Queue<Book> books = new Queue<Book>();
            StreamReader sr = new StreamReader("Books.txt");
            string line = string.Empty;

            while ((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                Book bk = new Book();
                bk.Author = words[0];
                bk.Book_Name = words[1];
                bk.Publisher = words[2];
                bk.Year = int.Parse(words[3]);
                bk.Category = words[4];
                books.Enqueue(bk);
            }

            return books;
        }
    }  // Form1 class ends here.

    public class Book
    {
        public string Author { get; set; }
        public string Book_Name { get; set; }
        public string Publisher { get; set; }
        public int Year { get; set; }
        public string Category { get; set; }

        override public string ToString()
        {
            return Author + "," + Book_Name + "," + Publisher + "," + Year + "," + Category + Environment.NewLine;
        }
    }

    public static class BooksHelper
    {
        public static Queue<Book> Books = new Queue<Book>();
    }

    public static class AnotherBookClass
    {
        public static void WriteAllBooks(string fileName)
        {
            foreach (Book item in BooksHelper.Books)
            {
                File.AppendAllText(fileName, item.ToString());
            }
        }
    }
}
 
Share this answer
 
Comments
Ron23111 18-Nov-18 15:40pm    
So my data structure class is not right
RickZeeland 18-Nov-18 16:07pm    
Not as shown in your question, the class keyword and it's surrounding accolades are missing :)
Ron23111 18-Nov-18 17:02pm    
Okkk so in your code the BooksHelper and AnotherBookClass does what. Sorry I am just new to C# that is why don't understand much. Thank you ☺️
RickZeeland 19-Nov-18 2:40am    
The keyword static is most important for the BooksHelper and AnotherBookClass, this way they can be called easily from other classes.
RickZeeland 19-Nov-18 2:46am    
BTW it would be better to use a using() block for the StreamReader, see: https://www.dotnetperls.com/streamreader

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