public class FlowerShop {
//Fields
Bouquet flowers = new Bouquet();
double profit = 0;
//Accessors
public double getProfit() {
return profit;
}
//Mutators
public void sell(Flower flower) {
flowers.removeFlower(flower);
profit += flower.getPrice();
}
public void sell(Bouquet bouquet) {
for (int i = 0; i < bouquet.getSize(); i++){
sell(bouquet.getFlower(i));
}
}
public void acceptDelivery(Flower flower) {
flowers.addFlower(flower);
profit -= flower.getCost();
}
public void acceptDelivery(Bouquet bouquet) {
for (int i = 0; i < bouquet.getSize(); i++){
acceptDelivery(bouquet.getFlower(i));
}
}
//Main method - could be anywhere
public static void main(String[]args){
//Creates some flowers (rose, tulip, and daisy)
Flower rose=new Flower("rose",2,3,5);
Flower tulip=new Flower("tulip",1,2,3);
Flower daisy=new Flower("daisy",.5,1,7);
//Our FlowerShop
FlowerShop fs = new FlowerShop();
// accept delivery of 1 rose, 1 tulip, and 1 daisy
fs.acceptDelivery(rose); //mutating flower shop, fs, changing its state
fs.acceptDelivery(tulip);
fs.acceptDelivery(daisy);
System.out.println("Total cost: " + fs.getProfit());
// sell 1 rose
fs.sell(rose); // changes fs' state, changes fields profit and boquet
System.out.println("Current profit: " + fs.getProfit());
// sell a bouquet of 1 tulip and 1 daisy
Bouquet tulipAndDaisy=new Bouquet();
tulipAndDaisy.addFlower(tulip); //can only add existing flowers
tulipAndDaisy.addFlower(daisy);
//Sell a boquet
fs.sell(tulipAndDaisy);
//Now we have made money
System.out.println("Current profit: " + fs.getProfit());
}
}