Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. // PaymentCard Class
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  *
  11.  * @author miguel
  12.  */
  13. public class PaymentCard {
  14.  
  15.     private double balance;
  16.  
  17.     public PaymentCard(double balance) {
  18.         this.balance = balance;
  19.     }
  20.  
  21.     public String toString() {
  22.         return "The card has a balance of " + this.balance + " euros";
  23.     }
  24.  
  25.     public void eatAffordably() {
  26.         if (this.balance - 2.60 < 0) {
  27.             this.balance = this.balance;
  28.         } else {
  29.             this.balance = this.balance - 2.60;
  30.         }
  31.     }
  32.  
  33.     public void eatHeartily() {
  34.         this.balance = this.balance - 4.60 < 0 ? this.balance : this.balance - 4.60;
  35.     }
  36.  
  37.     public void addMoney(double amount) {
  38.         if (this.balance + amount < this.balance) {
  39.             this.balance = this.balance;
  40.         } else {
  41.             this.balance = this.balance + amount > 150 ? 150 : this.balance + amount;
  42.         }
  43.     }
  44.  
  45. }
  46.  
  47.  
  48. // MainProgram Class !!!
  49.  
  50.  
  51. public class MainProgram {
  52.  
  53.     public static void main(String[] args) {
  54.  
  55.         PaymentCard paulsCard = new PaymentCard(20);
  56.         PaymentCard mattsCard = new PaymentCard(30);
  57.  
  58.         paulsCard.eatHeartily();
  59.         mattsCard.eatAffordably();
  60.         System.out.println("Paul: " + paulsCard);
  61.         System.out.println("Matt: " + mattsCard);
  62.  
  63.         paulsCard.addMoney(20);
  64.         mattsCard.eatHeartily();
  65.         System.out.println("Paul: " + paulsCard);
  66.         System.out.println("Matt: " + mattsCard);
  67.  
  68.         paulsCard.eatAffordably();
  69.         paulsCard.eatAffordably();
  70.         mattsCard.addMoney(-30);
  71.         System.out.println("Paul: " + paulsCard);
  72.         System.out.println("Matt: " + mattsCard);
  73.  
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement