using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
BeautyCode.Common.ConApp.Head.First.OO.Design
{
public
abstract
class
InstrumentSpec
{
private
Builder _builder;
private
InstrumentType _type;
private
Wood _backwood;
private
Wood _topwood;
public
InstrumentSpec(Builder builder, InstrumentType type, Wood backwood, Wood topwood)
{
_builder
=
builder;
_topwood
=
topwood;
_type
=
type;
_backwood
=
backwood;
}
public
virtual
bool
Matches(InstrumentSpec instance)
{
if
(instance._builder
!=
_builder)
return
false
;
if
(instance._backwood
!=
_backwood)
return
false
;
if
(instance._topwood
!=
_topwood)
return
false
;
if
(instance._type
!=
_type)
return
false
;
return
true
;
}
}
public
abstract
class
Instrument
{
private
string
_serialNumber;
public
string
SerialNumber
{
get
{
return
_serialNumber; }
}
private
double
_price;
public
double
Price
{
get
{
return
_price; }
}
private
InstrumentSpec _spec;
public
InstrumentSpec Spec
{
get
{
return
_spec; }
}
public
Instrument(
string
serialNumber,
double
price, InstrumentSpec spec)
{
_serialNumber
=
serialNumber;
_price
=
price;
_spec
=
spec;
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
BeautyCode.Common.ConApp.Head.First.OO.Design
{
public
class
GuitarSpec : InstrumentSpec
{
private
int
_numString;
public
int
NumString
{
get
{
return
_numString; }
}
public
GuitarSpec(Builder builder, InstrumentType type, Wood backwood, Wood topwood,
int
numString)
:
base
(builder,
type, backwood, topwood)
{
this
._numString
=
numString;
}
public
override
bool
Matches(InstrumentSpec instance)
{
if
(
!
base
.Matches(instance))
return
false
;
if
(
!
(instance
is
GuitarSpec))
return
false
;
GuitarSpec spec
=
instance
as
GuitarSpec;
if
(spec._numString
!=
_numString)
return
false
;
return
true
;
}
}
public
class
Guitar : Instrument
{
public
Guitar(
string
serialNumber,
double
price, GuitarSpec spec)
:
base
(serialNumber, price, spec)
{
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
BeautyCode.Common.ConApp.Head.First.OO.Design
{
public
class
MandolinSpec : InstrumentSpec
{
private
Style _style;
public
Style Style
{
get
{
return
_style; }
}
public
MandolinSpec(Builder builder, InstrumentType type, Wood backwood, Wood topwood, Style style)
:
base
(builder,
type, backwood, topwood)
{
this
._style
=
style;
}
public
override
bool
Matches(InstrumentSpec instance)
{
if
(
!
base
.Matches(instance))
return
false
;
if
(
!
(instance
is
GuitarSpec))
return
false
;
MandolinSpec spec
=
instance
as
MandolinSpec ;
if
(spec._style
!=
_style )
return
false
;
return
true
;
}
}
public
class
Mandolin : Instrument
{
public
Mandolin(
string
serialNumber,
double
price, MandolinSpec spec)
:
base
(serialNumber, price, spec)
{
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
BeautyCode.Common.ConApp.Head.First.OO.Design
{
public
class
Inventory
{
private
List
<
Instrument
>
_inventory;
public
Inventory()
{
_inventory
=
new
List
<
Instrument
>
();
}
public
void
AddInstrument(
string
serialNumber,
double
price,InstrumentSpec spec)
{
Instrument instance
=
null
;
if
(spec
is
GuitarSpec)
{
instance
=
new
Guitar(serialNumber, price,(GuitarSpec ) spec);
}
else
if
(spec
is
MandolinSpec)
{
instance
=
new
Mandolin(serialNumber, price, (MandolinSpec)spec);
}
_inventory.Add(instance);
}
public
Instrument Get(
string
serialNumber)
{
return
_inventory.Find(i
=>
i.SerialNumber
==
serialNumber);
}
public
List
<
Instrument
>
Search(MandolinSpec searchSpec)
{
List
<
Instrument
>
instances
=
_inventory .FindAll(i
=>
i.Spec.Equals(searchSpec));
return
instances;
}
}
}