JAVA
AutomatedSalad/.classpath
AutomatedSalad/.project
AutomatedSalad org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature
AutomatedSalad/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.8
AutomatedSalad/src/AutomatedSalad.java
AutomatedSalad/src/AutomatedSalad.java
import
java
.
util
.
ArrayList
;
public
class
AutomatedSalad
implements
ISaladObserver
{
public
static
void
main
(
String
[]
args
)
{
new
AutomatedSalad
().
automate
();
}
public
void
automate
()
{
SaladObservable
.
addSaladObserver
(
this
,
10
);
}
@
Override
public
void
salad
(
final
ArrayList
<
Order
<
SaladItem
,
ISaladItem
>>
salad
)
{
//add this in your imports java.util.ListIterator if you want to use ListIterator
System
.
out
.
print
(
"Assembling Salad-->"
);
//keep this line
//TODO -YOUR CODE GOES HERE - Additional Factory is acceptable if you want to go that route
System
.
out
.
print
(
"[SALAD MACHINE IS DOWN -- NO SALAD ASSEMBLED]"
);
//remove this line when done
System
.
out
.
println
(
"-->Salad Assembled"
);
//keep this line
}
}
AutomatedSalad/src/AutomatedSaladObservableFactory.java
AutomatedSalad/src/AutomatedSaladObservableFactory.java
import
java
.
util
.
concurrent
.
ThreadFactory
;
/**
*
* The thread factory which takes a thread and gives it a name
*
*/
public
class
AutomatedSaladObservableFactory
implements
ThreadFactory
{
private
String
observableName
=
null
;
/**
*
* default constructor
*
*
@param
the name for the observable
*
*/
public
AutomatedSaladObservableFactory
(
final
String
sObservableName
)
{
observableName
=
sObservableName
;
}
/**
*
* create a new thread given a runnable
*
*
@param
the runnable for the thread
*
*
@return
the newly constructed thread
*
*/
public
Thread
newThread
(
Runnable
runnable
)
{
return
new
Thread
(
runnable
,
observableName
);
}
/**
*
* gets the name of the observable
*
*
@return
the threads name
*
*/
public
final
String
getObservableName
()
{
return
observableName
;
}
}
AutomatedSalad/src/Automation.java
AutomatedSalad/src/Automation.java
public
abstract
class
Automation
implements
IAutomation
{
private
IAutomation
iOutput
=
null
;
@
Override
public
void
processInput
()
{
if
(
iOutput
!=
null
)
{
iOutput
.
processInput
();
}
}
@
Override
public
IAutomation
getOutput
()
{
return
iOutput
;
}
@
Override
public
void
setOutput
(
IAutomation
iAutomation
)
{
iOutput
=
iAutomation
;
}
}
AutomatedSalad/src/CheeseDispenser.java
AutomatedSalad/src/CheeseDispenser.java
//TODO
AutomatedSalad/src/CheeseItem.java
AutomatedSalad/src/CheeseItem.java
public
class
CheeseItem
implements
ISaladItem
{
private
enum
CheeseItems
{
American
,
Asiago
,
Cheddar
,
HotPepper
,
Mozzarella
,
Parmesan
,
Swiss
;
};
private
CheeseItems
cheeseItems
=
CheeseItems
.
values
()[(
int
)(
Math
.
random
()
*
CheeseItems
.
values
().
length
)];
@
Override
public
String
getDescription
()
{
return
cheeseItems
.
toString
();
}
}
AutomatedSalad/src/DressingDispenser.java
AutomatedSalad/src/DressingDispenser.java
//TODO
AutomatedSalad/src/DressingItem.java
AutomatedSalad/src/DressingItem.java
public
class
DressingItem
implements
ISaladItem
{
private
enum
DressingItems
{
Buttermilk
,
Catalina
,
Cesar
,
Italian
,
Ranch
,
ThousandIsland
,
Vinaigrette
};
private
DressingItems
dressingItems
=
DressingItems
.
values
()[(
int
)(
Math
.
random
()
*
DressingItems
.
values
().
length
)];
@
Override
public
String
getDescription
()
{
return
dressingItems
.
toString
();
}
}
AutomatedSalad/src/IAutomation.java
AutomatedSalad/src/IAutomation.java
public
interface
IAutomation
{
void
processInput
();
void
setOutput
(
IAutomation
IAutomation
);
IAutomation
getOutput
();
}
AutomatedSalad/src/ISaladItem.java
AutomatedSalad/src/ISaladItem.java
public
interface
ISaladItem
{
String
getDescription
();
}
AutomatedSalad/src/ISaladObserver.java
AutomatedSalad/src/ISaladObserver.java
import
java
.
util
.
ArrayList
;
public
interface
ISaladObserver
{
public
void
salad
(
ArrayList
<
Order
<
SaladItem
,
ISaladItem
>>
salad
);
}
AutomatedSalad/src/LeafDispenser.java
AutomatedSalad/src/LeafDispenser.java
//TODO
AutomatedSalad/src/LeafItem.java
AutomatedSalad/src/LeafItem.java
public
class
LeafItem
implements
ISaladItem
{
private
enum
LeafItems
{
BokChoy
,
Chard
,
Iceberg
,
Kale
,
Romaine
,
Spinach
;
};
private
LeafItems
leafItems
=
LeafItems
.
values
()[(
int
)(
Math
.
random
()
*
LeafItems
.
values
().
length
)];
@
Override
public
String
getDescription
()
{
return
leafItems
.
toString
();
}
}
AutomatedSalad/src/NullItem.java
AutomatedSalad/src/NullItem.java
public
class
NullItem
implements
ISaladItem
{
@
Override
public
String
getDescription
()
{
return
"None"
;
}
}
AutomatedSalad/src/Order.java
AutomatedSalad/src/Order.java
public
class
Order
<
T1
,
T2
>
{
private
SaladItem
saladItem
=
null
;
private
ISaladItem
iSaladItem
=
null
;
public
Order
(
SaladItem
oSaladItem
,
ISaladItem
iiSaladItem
)
{
saladItem
=
oSaladItem
;
iSaladItem
=
iiSaladItem
;
}
public
SaladItem
getSaladItem
()
{
return
saladItem
;
}
public
ISaladItem
getIItem
()
{
return
iSaladItem
;
}
}
AutomatedSalad/src/SaladFactory.java
AutomatedSalad/src/SaladFactory.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Iterator
;
public
class
SaladFactory
{
private
final
static
ArrayList
<
SaladItem
>
saladItems
=
new
ArrayList
<
SaladItem
>
(
SaladItem
.
values
().
length
*
2
);
static
{
saladItems
.
add
(
SaladItem
.
Leaf
);
saladItems
.
add
(
SaladItem
.
Leaf
);
saladItems
.
add
(
SaladItem
.
Cheese
);
saladItems
.
add
(
SaladItem
.
Cheese
);
saladItems
.
add
(
SaladItem
.
Dressing
);
saladItems
.
add
(
SaladItem
.
Dressing
);
saladItems
.
add
(
SaladItem
.
Topping
);
saladItems
.
add
(
SaladItem
.
Topping
);
}
public
static
synchronized
ArrayList
<
Order
<
SaladItem
,
ISaladItem
>>
assembleSalad
()
{
Collections
.
shuffle
(
saladItems
);
ArrayList
<
Order
<
SaladItem
,
ISaladItem
>>
orders
=
new
ArrayList
<
Order
<
SaladItem
,
ISaladItem
>>
();
int
nItems
=
(
int
)(
Math
.
ceil
(
Math
.
random
()
*
saladItems
.
size
()));
int
nthItem
=
0
;
Iterator
<
SaladItem
>
iSaladItems
=
saladItems
.
iterator
();
while
((
++
nthItem
<=
nItems
)
&&
(
iSaladItems
.
hasNext
()))
{
SaladItem
saladItem
=
iSaladItems
.
next
();
switch
(
saladItem
)
{
case
Leaf
:
orders
.
add
(
new
Order
<
SaladItem
,
ISaladItem
>
(
saladItem
,
new
LeafItem
()));
break
;
case
Cheese
:
orders
.
add
(
new
Order
<
SaladItem
,
ISaladItem
>
(
saladItem
,
new
CheeseItem
()));
break
;
case
Dressing
:
orders
.
add
(
new
Order
<
SaladItem
,
ISaladItem
>
(
saladItem
,
new
DressingItem
()));
break
;
case
Topping
:
orders
.
add
(
new
Order
<
SaladItem
,
ISaladItem
>
(
saladItem
,
new
ToppingItem
()));
break
;
default
:
break
;
}
}
return
orders
;
}
}
AutomatedSalad/src/SaladItem.java
AutomatedSalad/src/SaladItem.java
public
enum
SaladItem
{
Leaf
,
Cheese
,
Dressing
,
Topping
}
AutomatedSalad/src/SaladObservable.java
AutomatedSalad/src/SaladObservable.java
import
java
.
util
.
concurrent
.
ScheduledThreadPoolExecutor
;
import
java
.
util
.
concurrent
.
TimeUnit
;
public
class
SaladObservable
implements
Runnable
{
private
static
SaladObservable
saladObservable
=
new
SaladObservable
();
private
boolean
inSalad
=
false
;
private
ScheduledThreadPoolExecutor
scheduler
=
null
;
private
ISaladObserver
iSaladObserver
=
null
;
private
int
saladsToMake
=
0
;
private
SaladObservable
()
{
}
private
static
SaladObservable
getSaladObservable
()
{
return
saladObservable
;
}
public
static
void
addSaladObserver
(
final
ISaladObserver
iiSaladObserver
,
int
nSaladsToMake
)
{
SaladObservable
saladObservable
=
getSaladObservable
();
if
(
saladObservable
!=
null
)
saladObservable
.
add
(
iiSaladObserver
,
nSaladsToMake
);
}
private
synchronized
void
add
(
final
ISaladObserver
iiSaladObserver
,
int
nSaladsToMake
)
{
iSaladObserver
=
iiSaladObserver
;
saladsToMake
=
nSaladsToMake
;
if
(
!
getIsInSalad
())
startSalad
();
}
public
static
void
removeSaladObserver
(
final
ISaladObserver
iiSaladObserver
)
{
SaladObservable
saladObservable
=
getSaladObservable
();
if
(
saladObservable
!=
null
)
saladObservable
.
remove
(
iiSaladObserver
);
}
private
synchronized
void
remove
(
final
ISaladObserver
iiSaladObserver
)
{
if
(
iSaladObserver
==
iiSaladObserver
)
{
iSaladObserver
=
null
;
stopSalad
();
}
}
private
synchronized
void
startSalad
()
{
setIsInSalad
(
true
);
if
(
scheduler
==
null
)
{
AutomatedSaladObservableFactory
automatedSaladObservableFactory
=
new
AutomatedSaladObservableFactory
(
"AutomatedSalad Observable"
);
scheduler
=
new
ScheduledThreadPoolExecutor
(
1
,
automatedSaladObservableFactory
);
if
(
scheduler
!=
null
)
scheduler
.
scheduleAtFixedRate
(
this
,
0
,
1
,
TimeUnit
.
SECONDS
);
//1 salad every second
}
}
private
synchronized
void
stopSalad
()
{
setIsInSalad
(
false
);
if
(
scheduler
!=
null
)
{
scheduler
.
shutdown
();
scheduler
=
null
;
}
}
public
static
final
boolean
isInSalad
()
{
SaladObservable
saladObservable
=
getSaladObservable
();
return
(
saladObservable
!=
null
?
saladObservable
.
getIsInSalad
()
:
false
);
}
public
final
synchronized
boolean
getIsInSalad
()
{
return
inSalad
;
}
private
final
synchronized
void
setIsInSalad
(
final
boolean
bIsInSalad
)
{
inSalad
=
bIsInSalad
;
}
public
synchronized
void
run
()
{
if
(
--
saladsToMake
<
0
)
{
stopSalad
();
}
else
{
if
((
iSaladObserver
!=
null
)
&&
(
isInSalad
()))
{
iSaladObserver
.
salad
(
SaladFactory
.
assembleSalad
());
}
}
}
}
AutomatedSalad/src/ToppingDispenser.java
AutomatedSalad/src/ToppingDispenser.java
//TODO
AutomatedSalad/src/ToppingItem.java
AutomatedSalad/src/ToppingItem.java
public
class
ToppingItem
implements
ISaladItem
{
private
enum
ToppingItems
{
BaconBits
,
Croutons
,
Eggs
,
Tomatoes
,
SesameSeeds
,
Raisins
,
Onions
,
Cucumbers
,
Beets
,
Chicken
,
Ham
,
Tuna
};
private
ToppingItems
toppingItems
=
ToppingItems
.
values
()[(
int
)(
Math
.
random
()
*
ToppingItems
.
values
().
length
)];
@
Override
public
String
getDescription
()
{
return
toppingItems
.
toString
();
}
}