Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PaymentCard Class
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author miguel
- */
- public class PaymentCard {
- private double balance;
- public PaymentCard(double balance) {
- this.balance = balance;
- }
- public String toString() {
- return "The card has a balance of " + this.balance + " euros";
- }
- public void eatAffordably() {
- if (this.balance - 2.60 < 0) {
- this.balance = this.balance;
- } else {
- this.balance = this.balance - 2.60;
- }
- }
- public void eatHeartily() {
- this.balance = this.balance - 4.60 < 0 ? this.balance : this.balance - 4.60;
- }
- public void addMoney(double amount) {
- if (this.balance + amount < this.balance) {
- this.balance = this.balance;
- } else {
- this.balance = this.balance + amount > 150 ? 150 : this.balance + amount;
- }
- }
- }
- // MainProgram Class !!!
- public class MainProgram {
- public static void main(String[] args) {
- PaymentCard paulsCard = new PaymentCard(20);
- PaymentCard mattsCard = new PaymentCard(30);
- paulsCard.eatHeartily();
- mattsCard.eatAffordably();
- System.out.println("Paul: " + paulsCard);
- System.out.println("Matt: " + mattsCard);
- paulsCard.addMoney(20);
- mattsCard.eatHeartily();
- System.out.println("Paul: " + paulsCard);
- System.out.println("Matt: " + mattsCard);
- paulsCard.eatAffordably();
- paulsCard.eatAffordably();
- mattsCard.addMoney(-30);
- System.out.println("Paul: " + paulsCard);
- System.out.println("Matt: " + mattsCard);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement