C# Exercise: Vending Machine (OOP, Objects, Classes, Methods)

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Create a Console application called “Ch4-Vending“.
    Add a class to the application called “VendingMachine“.This class will be composed of one field (i.e., instance variable or attribute) and several methods as follows:
    A field of type int called “DepositedAmount“
    A constructor that takes no parameters that simply initializes the machine’s DepositedAmount to 0.
    A method (no return value) called “DepositCoin” with a single int parameter called “coinAmount”.
    This method adds the value passed in the coinAmount parameter to the machine’s DepositedAmount field.
    A method (no return value) called “GetDrink” with no parameters
    This method does one of two things. If the machine’s DepositedAmount is is sufficient to cover the cost of a drink (75 cents or more), then the method calculates the change (i.e.,DepositedAmount less 75 cents), writes “Your change is {change amount} cents” to the Console, and resets the DepositedAmount back to 0. If the machine’s DepositedAmount is not sufficient (less than 75 cents), it writes “Insert more coins” to the Console.
    A method (no return value) called “GetRefund” with no parameters
    This method simply displays “You were refunded {refund amount}” (refund amount should be the whole DepositedAmount) to the console and resets the machine’s DepositedAmountback to 0.
    Your Main method should create a single instance of your VendingMachine class. You should then call methods on this object to fully test that the machine is functioning properly. No need to get input from the user. Just “hard code” instructions in your Main method.
    Learning to code? Follow my C# tutorials for beginners. I do a lot of C# programming challenges and C# projects as part of my homework - every day!
    If you are learning to program, nothing beats solving real programming exercises and coding challenges.
    So don't forget to subscribe, as I release new programming videos every day!

ความคิดเห็น • 11

  • @kgwack
    @kgwack 7 ปีที่แล้ว +4

    Why don't you use depositedAmount += coinAmount inside DepositCoin method so that the coin insertion is accumulated?

  • @dogusural5792
    @dogusural5792 9 ปีที่แล้ว

    Great video as always :) Thank you.

  • @COD_Teo
    @COD_Teo 5 หลายเดือนก่อน

    what if you tried adding stock to the vending machine ?

  • @xVarga
    @xVarga 3 ปีที่แล้ว

    why is the constructor necessary here? cant we just declare the depositedAmount field as 0 without using a constructor?

  • @itech40
    @itech40 5 ปีที่แล้ว

    Awesome vid keep it up :)!

  • @Dreadric79
    @Dreadric79 9 ปีที่แล้ว

    Good work as always. Could you do a program that calculates take home pay of an hourly and or salaried employee? Ive got this type of problem for an assignment and the switch statement is really screwing me up. I dont know if I should keep it in main() or put it in class ABC. It would be much appricated.

  • @gamerdarkness7021
    @gamerdarkness7021 8 ปีที่แล้ว +1

    can u code an exercise for me

  • @নীরবপ্রহর
    @নীরবপ্রহর 7 ปีที่แล้ว

    will you please solve a problem for me?

  • @jordanrussell5502
    @jordanrussell5502 8 ปีที่แล้ว

    how would i do this in a form, i need to have buttons for each entity instead of it being in a console