Object-Oriented Programming with Java, part I – Exercise 77

This program implements the Lyyra Card main and class, adds and subtracts money and prints the result.

My Code

Main

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        LyyraCard cardPekka = new LyyraCard(20);
        LyyraCard cardBrian = new LyyraCard(30);

        // write code here
        cardPekka.payGourmet();
        cardBrian.payEconomical();
        
        System.out.println("Pekka: " + cardPekka);
        System.out.println("Brian: " + cardBrian);
        
        cardPekka.loadMoney(20);
        
        cardBrian.payGourmet();
        
        System.out.println("Pekka: " + cardPekka);
        System.out.println("Brian: " + cardBrian);
        
        cardPekka.payEconomical();
        cardPekka.payEconomical();
        
        cardBrian.loadMoney(50.00);
        
        System.out.println("Pekka: " + cardPekka);
        System.out.println("Brian: " + cardBrian);
    }
}

LyyraCard

public class LyyraCard {

    private double balance;

    public LyyraCard(double balanceAtStart) {
        // write code here
        this.balance = balanceAtStart;
    }

    public void payEconomical() {
        // write code here
        if (this.balance >= 2.50) {
            this.balance = this.balance - 2.50;
        }
    }

    public void payGourmet() {
        // write code here
        if (this.balance >= 4.00) {
            this.balance = this.balance - 4.00;
        }
    }

    public void loadMoney(double amount) {
        // write code here
        if (amount > 0) {
            this.balance = this.balance + amount;
        }
        if (this.balance > 150.00) {
            this.balance = 150.00;
        }
    }

    public String toString() {
        // write code here
        return "The card has " + this.balance + " euros";
    }
}

Model Code

Main

import java.util.Scanner;

 

public class Main {

 

    public static void main(String[] args) {

        // add here code that tests LyraCard. However before doing 77.6 remove the

        // other code 

 

        LyyraCard cardOfPekka = new LyyraCard(20);

        LyyraCard cardOfBrian = new LyyraCard(30);

 

        cardOfPekka.payGourmet();

        cardOfBrian.payEconomical();

 

        System.out.println("Pekka: " + cardOfPekka);

        System.out.println("Brian: " + cardOfBrian);

 

        cardOfPekka.loadMoney(20);

        cardOfBrian.payGourmet();

 

        System.out.println("Pekka: " + cardOfPekka);

        System.out.println("Brian: " + cardOfBrian);

 

        cardOfPekka.payEconomical();

        cardOfPekka.payEconomical();

        cardOfBrian.loadMoney(50);

 

        System.out.println("Pekka: " + cardOfPekka);

        System.out.println("Brian: " + cardOfBrian);

    }

}

LyyraCard

public class LyyraCard {

    private double balance;

    // we define the prices as final variable

    private final double ECONOMICAL = 2.5;

    private final double GOURMET = 4.0;

    // defining variables final makes those constant, 

    // i.e., the value of a final variable van not be changed

    // final variables are usually named with uppercase

    

    public LyyraCard(double balanceAtStart) {

        this.balance = balanceAtStart;

    }

 

    public void payEconomical() {

        if (this.balance >= ECONOMICAL) {

            this.balance -= ECONOMICAL;

        }

    }

 

    public void payGourmet() {

        if (this.balance >= GOURMET) {

            this.balance -= GOURMET;

        }

    }

 

    public void loadMoney(double amount) {

        if (amount < 0) {

            return;

        }

 

        this.balance += amount;

        if (this.balance > 150) {

            this.balance = 150;

        }

    }

 

    @Override

    public String toString() {

        return "The card has " + this.balance + " euros";

    }

}

Comments

I would have liked to have printed the name of the card using the toString but I would have had to have added the value name.

Leave a comment