Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Assessment4
- {
- class Program
- {
- static List<Publication> publications = new List<Publication>();
- static void Main(string[] args)
- {
- string filename = "catalogue.txt";
- string newfilename = "saved_catalogue.txt";
- LoadPublications(filename);
- foreach (var p in publications)
- Console.WriteLine(p);
- SavePublications(newfilename, publications);
- Book book1 = new Book("Home", "Jess Carroll", 11.90m, "Hardcover", 456, "Drama", "DAW Books", "09/09/2015", "123456789012");
- publications.Add(book1);
- AudioBook audioBook1 = new AudioBook("The Attack of the Killer Programming Assignment", "Lenor Shepherd", "John Cena", 10.00m, "audioCD", "416 minutes and 18 seconds", "Autobiography", "ISIT114", "10/10/2015");
- publications.Add(audioBook1);
- publications.Sort();
- }
- //Q2
- public static List<Publication> LoadPublications(string list)
- {
- try
- {
- List<Publication> publicationslist = new List<Publication>();
- StreamReader file = new StreamReader(list);
- char pType;
- while (!file.EndOfStream)
- {
- pType = (char)file.Read();
- if (pType == 'a')
- {
- AudioBook x = new AudioBook();
- x.Read(file);
- publicationslist.Add(x);
- }
- if (pType == 'b')
- {
- Book x = new Book();
- x.Read(file);
- publicationslist.Add(x);
- }
- }
- file.Close();
- Console.WriteLine("Number of publications saved to the list: {0}", publicationslist.Count);
- }
- catch (IOException ex)
- {
- Console.WriteLine("We had trouble reading the file:\n {0}", ex.Message);
- Console.ReadLine();
- }
- return publications;
- }
- //Q4
- public static void SavePublications(string newfilename, List<Publication> publications)
- {
- StreamWriter w = new StreamWriter(newfilename);
- foreach (Publication p in publications)
- {
- string contents = p.Write(w);
- w.WriteLine(contents);
- }
- w.Close();
- }
- }
- class Publication : IComparable<Publication>
- {
- //Constructors
- public Publication ()
- {
- }
- public Publication(string title_, string author_, decimal price_, string genre_, string publisher_, string published_)
- {
- this.title = title_;
- this.author = author_;
- this.price = price_;
- this.genre = genre_;
- this.publisher = publisher_;
- this.published = published_;
- }
- public string title { get; set; }
- public string author { get; set; }
- public decimal price { get; set; }
- public string genre { get; set; }
- public string publisher { get; set; }
- public string published { get; set; }
- public virtual string Write(StreamWriter w)
- {
- string contents = "\t" + title + "\t" + author + "\t ";
- return contents;
- }
- //Q6
- public int CompareTo(Publication other)
- {
- return this.title.CompareTo(other.title);
- }
- }
- class Book : Publication
- {
- //Constructors
- public Book()
- {
- }
- public Book(string title_, string author_, decimal price_, string type_, int pages_, string genre_, string publisher_, string published_, string ISBN_)
- : base(title_, author_, price_, genre_, publisher_, published_)
- {
- this.type = type_;
- this.pages = pages_;
- this.ISBN = ISBN_;
- }
- public string type { get; set; } // auto-implemented properties
- public int pages { get; set; }
- public string ISBN { get; set; }
- //Q1
- public void Read(StreamReader readFile)
- {
- if (readFile != null)
- {
- try
- {
- string readLine = readFile.ReadLine();
- string[] str = readLine.Split('\t'); //read from (tab delimited) file
- title = str[1];
- author = str[2];
- price = decimal.Parse(str[3]);
- type = str[4];
- pages = int.Parse(str[5]);
- genre = str[6];
- publisher = str[7];
- published = str[8];
- ISBN = str[9];
- }
- catch
- {
- Console.WriteLine("ERROR: File could not be read"); //Q5
- return;
- }
- }
- }
- //Q3
- public override string Write(StreamWriter w)
- {
- //string contents = title + " by " + author + " $" + price + "\n" + type + ": " + pages + " pages\nGenre: " + genre + "\nPublisher: " + publisher + "\nPublished: " + published + "\nISBN-13: " + ISBN;
- string basecontents = base.Write(w);
- string pubType = "a";
- string format = price + "\t" + type + "\t" + pages + "\t" + genre + "\t" + publisher + "\t" + published + "\t" + ISBN;
- return pubType + basecontents + format;
- }
- //Q7
- public long CheckISBN()
- {
- int total1 = 0;
- int total2 = 0;
- char[] array1 = ISBN.ToCharArray();
- int[] stuff = new int[13];
- for (int i = 0; i < array1.Length; i++)
- stuff[i] = (int)Char.GetNumericValue(array1[i]);
- for (int i = 1; i < stuff.Length; i = i + 2)
- total1 = total1 + stuff[i];
- for (int i = 0; i < stuff.Length; i = i + 2)
- total2 = total2 + stuff[i];
- if ((total1 * 3 + total2) % 10 == 0)
- return long.Parse(ISBN);
- else
- {
- throw new Exception("ERROR: Invalid ISBN number");
- }
- }
- }
- class AudioBook : Publication
- {
- //Constructors
- public AudioBook ()
- {
- }
- public AudioBook(string title_, string author_, string narrator_, decimal price_, string format_, string playlength_, string genre_, string publisher_, string published_)
- : base(title_, author_, price_, genre_, publisher_, published_)
- {
- this.format = format_;
- this.playlength = playlength_;
- this.narrator = narrator_;
- }
- public string narrator { get; set; } // auto-implemeneted properties
- public string format { get; set; }
- public string playlength { get; set; }
- //Q1
- public void Read(StreamReader readFile)
- {
- if (readFile != null)
- {
- try
- {
- string readLine = readFile.ReadLine();
- string[] str = readLine.Split('\t'); //read from (tab delimited) file
- title = str[1];
- author = str[2];
- narrator = str[3];
- price = decimal.Parse(str[4]);
- format = str[5];
- playlength = str[6];
- genre = str[7];
- publisher = str[8];
- published = str[9];
- }
- catch
- {
- Console.WriteLine("ERROR: File could not be read"); //Q5
- return;
- }
- }
- }
- //Q3
- public override string Write(StreamWriter w)
- {
- //string contents = title + " by " + author + ", Narrated by " + narrator + " $" + price + "\nFormat : " + format + "\nPlay Length: " + playlength + "\nGenre: " + genre + "\nPublisher: " + publisher + "\nPublished: " + published;
- string basecontents = base.Write(w);
- string pubType = "a";
- string stuff = narrator + "\t" + price + "\t" + format + "\t" + playlength + "\t" + genre + "\t" + publisher + "\t" + published;
- return pubType + basecontents + stuff;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement