public void DoHomework() { // Save the values before we try the homework. int tempEnergy = EnergyLevel, tempStress = StressLevel; try { // Try to set the properties to the new ones. // If we pop first, then we will be missing the item if anything fails. // If we pop in each step, we'll be getting a new item each time. // So we peek, which shows the top item, without removing it. Kind of like flipping the top card on a deck. EnergyLevel -= 10 * PendingHomework.Peek().Complexity; StressLevel += 5 * PendingHomework.Peek().Complexity; WritingInstrument.Write(100 * PendingHomework.Peek().Complexity); // Once we're sure everything is good, we pop the top item off. PendingHomework.Pop(); } catch (Exception ex) { // If either one fails, rollback both. EnergyLevel = tempEnergy; StressLevel = tempStress; // Throw the exception up the stack to main. throw ex; } }
public void DoHomework() { // Save the values before we try the homework. int tempEnergy = EnergyLevel, tempStress = StressLevel; try { // Try to set the properties to the new ones. EnergyLevel -= 10 * pendingHomeWork.Peek().Complexity; StressLevel += 5 * pendingHomeWork.Peek().Complexity; WritingInstrument.Write("It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing"); pendingHomeWork.Pop(); } catch (Exception ex) { // If either one fails, rollback both. EnergyLevel = tempEnergy; StressLevel = tempStress; // Throw the exception up the stack to main. throw ex; } }