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.

HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

2 Answers2

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.

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()

Polity
  • 14,734
  • 2
  • 40
  • 40
  • 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. – HelpNeeder Oct 20 '11 at 02:13
  • One comment on naming convention for methods. My professor insist that we use camel case for methods and classes with capitalization. – HelpNeeder Oct 20 '11 at 02:18
  • 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 – Polity Oct 20 '11 at 02:20
  • Well, I can't loose points because I'll do otherwise. People did point out in my previous questions that he might have some weird practices, but I am just following his directions not to loose any points. Besides that. Can you see the last edit and explain why I am not being showed the price? – HelpNeeder Oct 20 '11 at 02:50
  • 1
    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! – Polity Oct 20 '11 at 02:57
  • I have added Publication.cs so you can see. The price is defined as float. I know, price should be a double because float might loose some digits in calculations. – HelpNeeder Oct 20 '11 at 03:01
  • 1
    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();" – Polity Oct 20 '11 at 03:03
  • When I did the output in display() method my price came up to be: 1.09356E+09. Super Weird. – HelpNeeder Oct 20 '11 at 03:06
  • Never mind, didn't see your last comment. Great! Thank you. I think I am done :) – HelpNeeder Oct 20 '11 at 03:08
  • You probably have floats and doubles mixed together now. Make sure that everything is of the same type! better use double if in doubt. That would make Price a double and reading would become r.ReadDouble(). Make sure you save a new copy before reading it when testing, else you might read old data – Polity Oct 20 '11 at 03:09
  • One more thing. I'm so sorry to bother you. What's the posibility that my display isn't just reading the content of the properties as they were saved from write filestream? When I did "book.readDataFrom(read);" that will overwrite original values. Or no? – HelpNeeder Oct 20 '11 at 03:10
  • Depends, it will overwrite the values in your object but it wont overwrite the values in your file – Polity Oct 20 '11 at 03:20