3

I am working on my homework assignment and I am completely stuck! What I am trying to do is to use already defined input and save it to the file by using saveDataTo() method and read the input by using readDataFrom() method.

I am stuck on the first part. I am not sure if I have to initialize the data in Program.cs file first?

I don't know and I am stuck. Here is code and hope for some tips how I can accomplish this.

-- EDIT --

I can add instructions for purpose of both saveDataTo() and readDataFrom() methods:

The saveDataTo( ) method takes a parameter of BinaryWriter. The method writes the values of all 5 properties of an book object to a file stream associated with the writer (the association is done in the Main( ) method of Program class). There is no need to open and close the file stream and binary writer inside this method.

The readDataFrom( ) method takes a parameter of BinaryReader. The method reads the values of all five properties of the Book object from a file stream associated with the reader (the association is done in the Main( ) method of Program class). There is no need to open and close the file stream and binary reader inside this method.

So that gives me a clue that I should use and assign the properties to be saved in the file there?

-- EDIT --

Updated the code there. I do have a problem with content that is being saved into the file. I am not being showed the price. Why is that?

ff.APublisherNameTitle  FirstNameLastName

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Program
    {
        private const string FILE_NAME = "lab07.dat";

        static void Main(string[] args)
        {
            //char ask;

            /*
            do
            {
                Console.Write("Enter Book Title: ");
                publication.Title = Console.ReadLine();
                Console.Write("Enter Author's First Name: ");
                book.AuthorFirstName = Console.ReadLine();
                Console.Write("Enter Author's Last Name: ");
                book.AuthorLastName = Console.ReadLine();
                Console.Write("Enter Publisher's Name: ");
                publication.PublisherName = Console.ReadLine();
                Console.Write("Enter Book Price: $");
                publication.Price = float.Parse(Console.ReadLine());
                Console.Write("Would like to enter another book? [Y or N] ");
                ask = char.Parse(Console.ReadLine().ToUpper());
            }
            while (ask == char.Parse("Y"));
            */

            Book book = new Book();

            book.Price = 10.9F;
            book.Title = "Title";
            book.PublisherName = "PublisherName";
            book.AuthorFirstName = "FirstName";
            book.AuthorLastName = "LastName";

            FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
            BinaryWriter write = new BinaryWriter(fileStream);
            book.saveDataTo(write);
            write.Close();

            fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            BinaryReader read = new BinaryReader(fileStream);
            book.readDataFrom(read);
            read.Close();
        }
    }
}

Publication.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Publication
    {
        private float price;
        private string publisherName, title;

        public float Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public string PublisherName
        {
            get
            {
                return publisherName;
            }
            set
            {
                publisherName = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public void display()
        {
            Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
        }
    }
}

Book.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Book : Publication
    {
        private string authorFirstName, authorLastName;

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }

        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }

        public new void display()
        {
        }

        public string getAuthorName()
        {
            return authorFirstName + " " + authorLastName;
        }

        public void readDataFrom(BinaryReader r)
        {
            Price = r.ReadInt32();
            PublisherName = r.ReadString();
            Title = r.ReadString();
            authorFirstName = r.ReadString();
            authorLastName = r.ReadString();
        }

        public void saveDataTo(BinaryWriter w)
        {
            w.Write(base.Price);
            w.Write(base.PublisherName);
            w.Write(base.Title);
            w.Write(AuthorFirstName);
            w.Write(AuthorLastName);
        }
    }
}

Regards.

HelpNeeder.

0

2 Answers 2

2

You're asking whether to define the values in Program.cs or Book.cs, right? Well, it is fine to define the values in Program.cs, you just need to make sure all the values are given before writing the data.

So, since the function takes a BinaryWriter parameter that is supposedly initialized beforehand, this should work:

public void saveDataTo(BinaryWriter w)
{
     w.Write(getAuthorName());
     //etc...
}

But, just remember that you do need to define all the info somewhere (anywhere) before calling save data.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok I though it would be something like this. Do I have to add something in Main() so BinaryReader() know the FileStream?
Look at line 47 in Program.cs, there is no need for it?
Line 47 is which? The one where you set up a new BinaryWriter?
I have added comment line right before it sorry, it's not line 47. It's in program.cs part.
You do need to declare a new BinaryWriter, so that the saveDateTo() function can actually write data to something.
|
1

You assign your parameters to 2 different objects, see:

Publication publication = new Publication(); 
Book book = new Book(); 

Both are individual instances residing in memory.

You either have to refer the publication to the book like:

Book book = new Book(); 
Publication publication = (Publication)book;

or just assign the values currently assigned to the publication directly to the book so:

publication.PublisherName = "PublisherName"; 

becomes

book.PublisherName = "PublisherName"; 

Apart from that, you're working in C#, not Java. By convention its normal to start your methods with a Capital (Pascal Case)

EDIT

Your now shown the price when reaidng since you write it as a floating field (or double, cant see the definition) and read it as an integer.

Change from r.ReadInt32(); to r.ReadDouble(); or r.ReadSingle()

12 Comments

Oh! Ok! I know what you are saying. Since I am inheriting the parameters I should use book object instead of publication. Great, it compiles. I will post more questions if I will have more problems.
One comment on naming convention for methods. My professor insist that we use camel case for methods and classes with capitalization.
your professor really teaches you a bad practice there... Not that i know whats best but i strongly believe that keeping things in line with the industry standard is important and apart from that, you as the developer should be the one who decides how he writes his code
Updated my answer to address your second question. Now regarding your teacher (offtopic). If you think you can improve upon someone's work, even if its Jon Skeet himself, then try to do so!
Well, if it is Float than the only change you have ot make is in readDataFrom on the first line from "Price = r.ReadIn32();" to "Price = r.ReadSingle();"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.