Wednesday, September 25, 2024

Pertemuan 4 - Kalkulator Sederhana

 Nama: Muhammad Risyad Himawan Putra

NRP: 5025231205

Kelas: Pemrograman Berbasis Obyek (G)




Source Code Github

Source Code

/**
public class CalcEngine
{
    // The calculator's state is maintained in three fields:
    //     buildingDisplayValue, haveLeftOperand, and lastOperator.
    
    // Are we already building a value in the display, or will the
    // next digit be the first of a new one?
    private boolean buildingDisplayValue;
    // Has a left operand already been entered (or calculated)?
    private boolean haveLeftOperand;
    // The most recent operator that was entered.
    private char lastOperator;

    // The current value (to be) shown in the display.
    private int displayValue;
    // The value of an existing left operand.
    private int leftOperand;

    /**
     * Create a CalcEngine.
     */
    public CalcEngine()
    {
        clear();
    }

    /**
     * @return The value that should currently be displayed
     * on the calculator display.
     */
    public int getDisplayValue()
    {
        return displayValue;
    }

    /**
     * A number button was pressed.
     * Either start a new operand, or incorporate this number as
     * the least significant digit of an existing one.
     * @param number The number pressed on the calculator.
     */
    public void numberPressed(int number)
    {
        if(buildingDisplayValue) {
            // Incorporate this digit.
            displayValue = displayValue*10 + number;
        }
        else {
            // Start building a new number.
            displayValue = number;
            buildingDisplayValue = true;
        }
    }

    /**
     * The 'plus' button was pressed. 
     */
    public void plus()
    {
        applyOperator('+');
    }

    /**
     * The 'minus' button was pressed.
     */
    public void minus()
    {
        applyOperator('-');
    }
    
    /**
     * The '=' button was pressed.
     */
    public void equals()
    {
        // This should completes the building of a second operand,
        // so ensure that we really have a left operand, an operator
        // and a right operand.
        if(haveLeftOperand &&
                lastOperator != '?' &&
                buildingDisplayValue) {
            calculateResult();
            lastOperator = '?';
            buildingDisplayValue = false;
        }
        else {
            keySequenceError();
        }
    }

    /**
     * The 'C' (clear) button was pressed.
     * Reset everything to a starting state.
     */
    public void clear()
    {
        lastOperator = '?';
        haveLeftOperand = false;
        buildingDisplayValue = false;
        displayValue = 0;
    }

    /**
     * @return The title of this calculation engine.
     */
    public String getTitle()
    {
        return "Java Calculator";
    }

    /**
     * Combine leftOperand, lastOperator, and the
     * current display value.
     * The result becomes both the leftOperand and
     * the new display value.
     */
    private void calculateResult()
    {
        switch(lastOperator) {
            case '+':
                displayValue = leftOperand + displayValue;
                haveLeftOperand = true;
                leftOperand = displayValue;
                break;
            case '-':
                displayValue = leftOperand - displayValue;
                haveLeftOperand = true;
                leftOperand = displayValue;
                break;
            default:
                keySequenceError();
                break;
        }
    }
    
    /**
     * Apply an operator.
     * @param operator The operator to apply.
     */
    private void applyOperator(char operator)
    {
        // If we are not in the process of building a new operand
        // then it is an error, unless we have just calculated a
        // result using '='.
        if(!buildingDisplayValue &&
                    !(haveLeftOperand && lastOperator == '?')) {
            keySequenceError();
            return;
        }

        if(lastOperator != '?') {
            // First apply the previous operator.
            calculateResult();
        }
        else {
            // The displayValue now becomes the left operand of this
            // new operator.
            haveLeftOperand = true;
            leftOperand = displayValue;
        }
        lastOperator = operator;
        buildingDisplayValue = false;
    }

    /**
     * Report an error in the sequence of keys that was pressed.
     */
    private void keySequenceError()
    {
        System.out.println("A key sequence error has occurred.");
        // Reset everything.
        clear();
    }
}

Compile CalculatorUI lalu input perhitungan.

















Wednesday, September 18, 2024

Pertemuan 3 - Ticket Machine

 Nama: Muhammad Risyad Himawan Putra

NRP: 5025231205

Kelas: Pemrograman Berbasis Obyek (G)




Source Code Github

Source Code

/**
 * SimpleTicketMachine models a basic ticket machine that sells tickets
 * at a fixed price. Users are expected to enter the correct amount.
 * No sophisticated checks are performed.
 * 
 * @author Refactor
 * @version 2024.09.19
 */
public class SimpleTicketMachine
{
    // The price of a single ticket.
    private int ticketPrice;
    // The current balance of the user's inserted money.
    private int currentBalance;
    // The total revenue collected by the machine.
    private int totalRevenue;
    
    /**
     * Create a ticket machine that sells tickets at a specific price.
     * @param price the cost of a ticket
     */
    public SimpleTicketMachine(int price)
    {
        ticketPrice = price;
        currentBalance = 0;
        totalRevenue = 0;
    }
    
    /**
     * Get the price of a single ticket.
     * @return the ticket price
     */
    public int getTicketPrice()
    {
        return ticketPrice;
    }
    
    /**
     * Get the total money inserted for the current transaction.
     * @return the current balance
     */
    public int getCurrentBalance()
    {
        return currentBalance;
    }
    
    /**
     * Insert money into the machine.
     * Only positive amounts are accepted.
     * @param amount the amount to be inserted
     */
    public void insertMoney(int amount)
    {
        if(amount > 0) {
            currentBalance += amount;
        } else {
            System.out.println("Please insert a positive amount.");
        }
    }
    
    /**
     * Print a ticket if enough money has been inserted.
     * Otherwise, inform the user of how much more is needed.
     */
    public void printTicket()
    {
        if(currentBalance >= ticketPrice) {
            System.out.println("**************");
            System.out.println("* Ticket *");
            System.out.println("* Price: " + ticketPrice + " *");
            System.out.println("**************");
            totalRevenue += ticketPrice;
            currentBalance -= ticketPrice;
        } else {
            int deficit = ticketPrice - currentBalance;
            System.out.println("You need " + deficit + " more to buy the ticket.");
        }
    }
    
    /**
     * Refund the balance if any.
     * @return the refunded balance
     */
    public int refundBalance()
    {
        int amountToRefund = currentBalance;
        currentBalance = 0;
        if(amountToRefund > 0) {
            System.out.println("Refunding: " + amountToRefund);
        }
        return amountToRefund;
    }
    
    /**
     * Get the total revenue collected by the machine.
     * @return the total revenue
     */
    public int getTotalRevenue()
    {
        return totalRevenue;
    }
}

 Masukkan Harga Tiket


Masukkan Uang User

   



Print tiket



Kembalian



Total Keuntungan









PPB EAS - Aplikasi Member Timezone

Manajemen Keanggotaan & Loyalty Program Timezone dengan Room Database dan Jetpack Compose Aryaka Leorgi Epridaka - 5025231117 Muhammad ...