Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Assessment4
  9. {
  10.     class Program
  11.     {
  12.         static List<Publication> publications = new List<Publication>();
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string filename = "catalogue.txt";
  17.             string newfilename = "saved_catalogue.txt";
  18.  
  19.             LoadPublications(filename);
  20.  
  21.             foreach (var p in publications)
  22.                 Console.WriteLine(p);
  23.  
  24.             SavePublications(newfilename, publications);
  25.  
  26.             Book book1 = new Book("Home", "Jess Carroll", 11.90m, "Hardcover", 456, "Drama", "DAW Books", "09/09/2015", "123456789012");
  27.             publications.Add(book1);
  28.             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");
  29.             publications.Add(audioBook1);
  30.  
  31.             publications.Sort();
  32.         }
  33.  
  34.         //Q2
  35.         public static List<Publication> LoadPublications(string list)
  36.         {
  37.             try
  38.             {
  39.                 List<Publication> publicationslist = new List<Publication>();
  40.                 StreamReader file = new StreamReader(list);
  41.                 char pType;
  42.                 while (!file.EndOfStream)
  43.                 {
  44.                     pType = (char)file.Read();
  45.  
  46.                     if (pType == 'a')
  47.                     {
  48.                         AudioBook x = new AudioBook();
  49.                         x.Read(file);
  50.                         publicationslist.Add(x);
  51.                     }
  52.  
  53.                     if (pType == 'b')
  54.                     {
  55.                         Book x = new Book();
  56.                         x.Read(file);
  57.                         publicationslist.Add(x);
  58.                     }
  59.  
  60.                 }
  61.                 file.Close();
  62.                 Console.WriteLine("Number of publications saved to the list: {0}", publicationslist.Count);
  63.             }
  64.             catch (IOException ex)
  65.             {
  66.                 Console.WriteLine("We had trouble reading the file:\n {0}", ex.Message);
  67.                 Console.ReadLine();
  68.             }
  69.             return publications;
  70.         }
  71.  
  72.         //Q4
  73.         public static void SavePublications(string newfilename, List<Publication> publications)
  74.         {
  75.             StreamWriter w = new StreamWriter(newfilename);
  76.  
  77.             foreach (Publication p in publications)
  78.             {
  79.                 string contents = p.Write(w);
  80.                 w.WriteLine(contents);
  81.             }
  82.             w.Close();
  83.         }
  84.     }
  85.  
  86.     class Publication : IComparable<Publication>
  87.     {
  88.         //Constructors
  89.         public Publication ()
  90.         {
  91.  
  92.         }
  93.  
  94.         public Publication(string title_, string author_, decimal price_, string genre_, string publisher_, string published_)
  95.         {
  96.             this.title = title_;
  97.             this.author = author_;
  98.             this.price = price_;
  99.             this.genre = genre_;
  100.             this.publisher = publisher_;
  101.             this.published = published_;
  102.  
  103.         }
  104.  
  105.         public string title { get; set; }
  106.         public string author { get; set; }
  107.         public decimal price { get; set; }
  108.         public string genre { get; set; }
  109.         public string publisher { get; set; }
  110.         public string published { get; set; }
  111.  
  112.         public virtual string Write(StreamWriter w)
  113.         {
  114.             string contents = "\t" + title + "\t" + author + "\t ";
  115.             return contents;        
  116.         }
  117.  
  118.         //Q6
  119.         public int CompareTo(Publication other)
  120.         {
  121.             return this.title.CompareTo(other.title);
  122.         }
  123.     }
  124.  
  125.     class Book : Publication
  126.     {
  127.         //Constructors
  128.  
  129.         public Book()
  130.         {
  131.  
  132.         }
  133.  
  134.         public Book(string title_, string author_, decimal price_, string type_, int pages_, string genre_, string publisher_, string published_, string ISBN_)
  135.             : base(title_, author_, price_, genre_, publisher_, published_)
  136.         {
  137.             this.type = type_;
  138.             this.pages = pages_;
  139.             this.ISBN = ISBN_;
  140.         }
  141.  
  142.         public string type { get; set; } // auto-implemented properties
  143.         public int pages { get; set; }
  144.         public string ISBN { get; set; }
  145.  
  146.         //Q1
  147.         public void Read(StreamReader readFile)
  148.         {
  149.             if (readFile != null)
  150.             {
  151.                 try
  152.                 {
  153.                     string readLine = readFile.ReadLine();
  154.                     string[] str = readLine.Split('\t'); //read from (tab delimited) file
  155.                     title = str[1];
  156.                     author = str[2];
  157.                     price = decimal.Parse(str[3]);
  158.                     type = str[4];
  159.                     pages = int.Parse(str[5]);
  160.                     genre = str[6];
  161.                     publisher = str[7];
  162.                     published = str[8];
  163.                     ISBN = str[9];
  164.                 }
  165.                 catch
  166.                 {
  167.                     Console.WriteLine("ERROR: File could not be read"); //Q5
  168.                     return;
  169.                 }
  170.             }
  171.         }
  172.  
  173.         //Q3
  174.         public override string Write(StreamWriter w)
  175.         {
  176.             //string contents = title + " by " + author + " $" + price + "\n" + type + ": " + pages + " pages\nGenre: " + genre + "\nPublisher: " + publisher + "\nPublished: " + published + "\nISBN-13: " + ISBN;
  177.             string basecontents = base.Write(w);
  178.             string pubType = "a";
  179.             string format = price + "\t" + type + "\t" + pages + "\t" + genre + "\t" + publisher + "\t" + published + "\t" + ISBN;
  180.             return pubType + basecontents + format;      
  181.         }
  182.  
  183.         //Q7
  184.         public long CheckISBN()
  185.         {
  186.             int total1 = 0;
  187.             int total2 = 0;
  188.             char[] array1 = ISBN.ToCharArray();
  189.             int[] stuff = new int[13];
  190.  
  191.             for (int i = 0; i < array1.Length; i++)
  192.                 stuff[i] = (int)Char.GetNumericValue(array1[i]);
  193.  
  194.             for (int i = 1; i < stuff.Length; i = i + 2)
  195.                 total1 = total1 + stuff[i];
  196.             for (int i = 0; i < stuff.Length; i = i + 2)
  197.                 total2 = total2 + stuff[i];
  198.             if ((total1 * 3 + total2) % 10 == 0)
  199.                 return long.Parse(ISBN);
  200.             else
  201.             {
  202.                 throw new Exception("ERROR: Invalid ISBN number");
  203.             }
  204.         }
  205.     }
  206.  
  207.     class AudioBook : Publication
  208.     {
  209.         //Constructors
  210.         public AudioBook ()
  211.         {
  212.  
  213.         }
  214.  
  215.         public AudioBook(string title_, string author_, string narrator_, decimal price_, string format_, string playlength_, string genre_, string publisher_, string published_)
  216.             : base(title_, author_, price_, genre_, publisher_, published_)
  217.         {
  218.             this.format = format_;
  219.             this.playlength = playlength_;
  220.             this.narrator = narrator_;
  221.         }
  222.  
  223.         public string narrator { get; set; } // auto-implemeneted properties
  224.         public string format { get; set; }
  225.         public string playlength { get; set; }
  226.  
  227.         //Q1
  228.         public void Read(StreamReader readFile)
  229.         {
  230.             if (readFile != null)
  231.             {
  232.                 try
  233.                 {
  234.                     string readLine = readFile.ReadLine();
  235.                     string[] str = readLine.Split('\t'); //read from (tab delimited) file
  236.                     title = str[1];
  237.                     author = str[2];
  238.                     narrator = str[3];
  239.                     price = decimal.Parse(str[4]);
  240.                     format = str[5];
  241.                     playlength = str[6];
  242.                     genre = str[7];
  243.                     publisher = str[8];
  244.                     published = str[9];
  245.                 }
  246.                 catch
  247.                 {
  248.                     Console.WriteLine("ERROR: File could not be read"); //Q5
  249.                     return;
  250.                 }
  251.             }
  252.         }
  253.  
  254.         //Q3
  255.         public override string Write(StreamWriter w)
  256.         {
  257.             //string contents = title + " by " + author + ", Narrated by " + narrator + " $" + price + "\nFormat : " + format + "\nPlay Length: " + playlength + "\nGenre: " + genre + "\nPublisher: " + publisher + "\nPublished: " + published;
  258.             string basecontents = base.Write(w);
  259.             string pubType = "a";
  260.             string stuff = narrator + "\t" + price + "\t" + format + "\t" + playlength + "\t" + genre + "\t" + publisher + "\t" + published;
  261.             return pubType + basecontents + stuff;
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement