|
|
Object-oriented design
/csbook/solutions/09/A07.cs
using System;
using System.Collections;
using System.Text;
//-------- Article -------
abstract class Article {
int id;
int price; // in Cent
public Article(int id, int price) {
this.id = id; this.price = price;
}
public int Id {
get { return id; }
}
public int Price {
get { return price; }
}
public abstract string Description();
}
//-------- Book -------
class Book : Article {
string author;
string title;
string publisher;
int year;
public Book(int id, int price, string author, string title, string publisher, int year)
: base(id, price) {
this.author = author; this.title = title; this.publisher = publisher; this.year = year;
}
public override string Description() {
return author + ": " + title + ". " + publisher + ", " + year;
}
}
//-------- CD -------
class CD : Article {
string singer;
string title;
ArrayList songs;
public CD(int id, int price, string singer, string title) : base (id, price) {
this.singer = singer; this.title = title; songs = new ArrayList();
}
public void AddSong(string song) {
songs.Add(song);
}
public string Song(int i) {
if (i < songs.Count) return (string)songs[i]; else return null;
}
public int NumberOfSongs {
get { return songs.Count; }
}
public override string Description() {
return singer + ": " + title;
}
}
//-------- Video -------
class Video : Article {
string title;
int length;
public Video(int id, int price, string title, int length) : base (id, price) {
this.title = title; this.length = length;
}
public override string Description() {
return title + ". " + length + " minutes";
}
}
//-------- ShoppingCart -------
class ShoppingCart {
ArrayList articles = new ArrayList();
public void Add(Article a) {
articles.Add(a);
}
public string Contents() {
StringBuilder s = new StringBuilder();
int total = 0;
foreach (Article a in articles) {
s.Append(String.Format("{0,6} ", a.Id));
s.Append(String.Format("{0,7:f2} ", (float)a.Price / 100));
s.Append(a.Description() + "\r\n");
total += a.Price;
}
s.Append("--------------\r\n");
s.Append(String.Format(" {0,7:f2}", (float)total / 100));
return s.ToString();
}
}
//-------- Shop -------
class Shop {
Hashtable articles = new Hashtable();
public void Add(Article a) {
articles[a.Id] = a;
}
public Article Item(int id) {
return (Article)articles[id];
}
}
class Test {
public static void Main() {
Shop shop = new Shop();
shop.Add(new Book(13544, 14999, "Donald Knuth", "The art of computer programming", "Addison-Wesley", 1999));
shop.Add(new Book(15922, 1250, "Stephen Hawking", "A brief history of time", "Bantam", 1998));
shop.Add(new Book(12768, 1190, "James Joyce", "Ulysses", "Vintage Books", 1990));
shop.Add(new CD(24588, 3999, "John Coltrane", "Legacy"));
shop.Add(new CD(27335, 11898, "Singers Unlimited", "Magic voices"));
shop.Add(new CD(22766, 1198, "Lionell Hampton", "Ring them bells"));
shop.Add(new Video(35682, 1754, "Harry Potter and the sorcerer's stone", 120));
shop.Add(new Video(38577, 2249, "E.T.", 110));
ShoppingCart cart = new ShoppingCart();
cart.Add(shop.Item(27335));
cart.Add(shop.Item(13544));
cart.Add(shop.Item(35682));
Console.WriteLine(cart.Contents());
}
}
|
|