This exercise extends the Lyyra Card exercise so it can have money loaded and payed out.
My Code
Cash Register
public class CashRegister {
private double cashInRegister;
private int economicalSold;
private int gourmetSold;
public CashRegister() {
// at start the register has 1000 euros
this.cashInRegister = 1000.00;
this.economicalSold = 0;
this.gourmetSold = 0;
}
public double payEconomical(double cashGiven) {
// price of the economical lunch is 2.50 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (cashGiven >= 2.5) {
this.cashInRegister += 2.5;
this.economicalSold++;
return cashGiven - 2.5;
}
return cashGiven;
}
public boolean payEconomical(LyyraCard card) {
// price of the economical lunch is 2.50 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (card.balance() >= 2.5) {
card.pay(2.5);
this.economicalSold++;
return true;
}
return false;
}
public double payGourmet(double cashGiven) {
// price of the economical lunch is 2.50 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (cashGiven >= 4.0) {
this.cashInRegister += 4.0;
this.gourmetSold++;
return cashGiven - 4.0;
}
return cashGiven;
}
public boolean payGourmet(LyyraCard card) {
// price of the gourmet lunch is 4.00 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (card.balance() >= 4.0) {
card.pay(4.0);
this.gourmetSold++;
return true;
}
return false;
}
public void loadMoneyToCard(LyyraCard card, double sum) {
if (sum > 0) {
card.loadMoney(sum);
this.cashInRegister += sum;
}
}
public String toString() {
return "money in register " + cashInRegister + " economical lunches sold: " + economicalSold + " gourmet lunches sold: " + gourmetSold;
}
}
Model Code
Cash Register
public class CashRegister {
private double cashInRegister;
private int economicalSold;
private int gourmetSold;
// Javassa vakio määritellään näin
private static final double PRICE_OF_ECONOMICAL = 2.5;
private static final double PRICE_IOF_GOURMET = 4.0;
public CashRegister() {
// at start the register has 1000 euros
this.cashInRegister = 1000;
}
public double payEconomical(double cashGiven) {
// price of the economical lunch is 2.50 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (cashGiven < PRICE_OF_ECONOMICAL) {
return cashGiven;
}
this.cashInRegister += PRICE_OF_ECONOMICAL;
this.economicalSold++;
return cashGiven - PRICE_OF_ECONOMICAL;
}
public double payGourmet(double cashGiven) {
// price of the gourmet lunch is 4.00 euros
// if the given cash is at least the price of the lunch:
// the price of lunch is added to register
// the amount of sold lunch is incremented by one
// method returns cashGiven - lunch price
// if not enough money given, all is returned and nothing else happens
if (cashGiven < PRICE_IOF_GOURMET) {
return cashGiven;
}
this.cashInRegister += PRICE_IOF_GOURMET;
this.gourmetSold++;
return cashGiven - PRICE_IOF_GOURMET;
}
public boolean payEconomical(LyyraCard card) {
if (card.balance() < PRICE_OF_ECONOMICAL) {
return false;
}
card.pay(PRICE_OF_ECONOMICAL);
this.economicalSold++;
return true;
}
public boolean payGourmet(LyyraCard card) {
if (card.balance() < PRICE_IOF_GOURMET) {
return false;
}
card.pay(PRICE_IOF_GOURMET);
this.gourmetSold++;
return true;
}
public void loadMoneyToCard(LyyraCard card, double sum) {
if (sum < 0) {
return;
}
card.loadMoney(sum);
this.cashInRegister += sum;
}
public String toString() {
return "money in register " + cashInRegister + " economical lunches sold: " + economicalSold + " gourmet lunches sold: " + gourmetSold;
}
}
Comments
OK, I had to cheat on that one. The final step of the exercise required a boolean return from the cardOfJim from payGourmet to the main which really wasn’t happening. What I should have done was overloaded the payGourmet method to have an alternate payGourmet with a boolean return.