Vending Machine - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Vending Machine

Description:

System.out.println(howManyLeft ' cans of ' whatType ' left in machine 1'); // display what's left in machine 2. howManyLeft = machine2.getNumCans ... – PowerPoint PPT presentation

Number of Views:1389
Avg rating:3.0/5.0
Slides: 16
Provided by: michael316
Category:
Tags: machine | vending

less

Transcript and Presenter's Notes

Title: Vending Machine


1
Vending Machine
2
What does a vending machine do?
  • Keeps and dispenses cans of soda
  • Sodas have a type and a cost
  • Machine totals up the money

3
The Vending Machine Class
  • Framework no main
  • public class VendingMachineClass
  • private int numCans
  • private double sodaCost
  • private String sodaType
  • private double totalMoney 0

4
The Constructor
  • public class VendingMachineClass
  • private int numCans
  • private double sodaCost
  • private String sodaType
  • private double totalMoney 0
  • public VendingMachineClass(String inType,double
    inCost,int inNumber)
  • sodaCost inCost
  • sodaType inType
  • numCans inNumber
  • totalMoney 0

5
  • public VendingMachineClass(String inType,double
    inCost,int inNumber)
  • sodaCost inCost
  • sodaType inType
  • numCans inNumber
  • totalMoney 0
  • System.out.println()
  • System.out.println("Soda Machine Created with
    ")
  • System.out.println("Type "sodaType)
  • System.out.println("Cost " sodaCost)
  • System.out.println("Number of Cans " numCans)
  • System.out.println("Total at start "
    totalMoney)

6
  • public VendingMachineClass(String inType,double
    inCost,int inNumber)
  • sodaCost inCost
  • sodaType inType
  • numCans inNumber
  • totalMoney 0
  • if(inType.equals("Pepsi") true) // new String
    method!
  • System.out.println("Sorry, no Pepsi, just
    Coke")
  • inType "Coke"
  • System.out.println()
  • System.out.println("Soda Machine Created with
    ")
  • System.out.println("Type "sodaType)
  • System.out.println("Cost " sodaCost)
  • System.out.println("Number of Cans " numCans)
  • System.out.println("Total at start "
    totalMoney)

7
Accessors - one for each private variable
  • public int getNumCans()
  • return(numCans)
  • public String getSodaType(
  • return(sodaType)
  • public double getCost()
  • return(sodaCost)

8
Mutator - for each variable that is settable
  • public void addCans(int howMany )
  • System.out.println(Adding " howMany " cans of
    " sodaType)
  • numCans numCans howMany

9
  • public void buyCans( int howMany )
  • numCans numCans - howMany
  • totalMoney totalMoney (howMany sodaCost)
  • // end buy cans method

10
  • public void buyCans( int howMany )
  • // adjust for request larger than available
  • if (howMany gt numCans)
  • howMany numCans
  • numCans numCans - howMany
  • totalMoney totalMoney (howMany sodaCost)
  • // end buy cans method

11
  • public void buyCans( int howMany )
  • System.out.println("-gt in method BuyCans")
  • System.out.println(Attempting to buy howMany
    " cans of " sodaType)
  • if (howMany gt numCans)
  • System.out.println("Sorry, there's only "
    numCans " left")
  • System.out.println("Buying " numCans)
  • howMany numCans
  • numCans numCans - howMany
  • totalMoney totalMoney (howMany sodaCost)
  • System.out.println("TotalCash now " totalMoney
    " in machine " sodaType)
  • System.out.println("-gt leaving method buyCans")
  • // end buy cans method

12
Main
  • different class
  • must be present
  • uses VendingMachineClass (instantiates)

13
  • public class VendingMachineUser
  • public static void main(String args)
  • VendingMachineClass machine1 new
    VendingMachineClass("Pepsi", .75, 100)
  • // end main
  • //end class

14
  • public class VendingMachineUser
  • public static void main(String args)
  • int howManyLeft
  • String whatType
  • VendingMachineClass machine1 new
    VendingMachineClass("Pepsi", .75, 100)
  • // buy some
  • machine1.buyCans(60)
  • // display what's left
  • howManyLeft machine1.getNumCans()
  • whatType machine1.getSodaType()
  • System.out.println("Results....")
  • System.out.println(howManyLeft " cans of "
    whatType " left ")
  • System.out.println()

15
  • public class VendingMachineUser
  • public static void main(String args)
  • int howManyLeft
  • String whatType
  • VendingMachineClass machine1 new
    VendingMachineClass("Pepsi", .75, 100)
  • VendingMachineClass machine2 new
    VendingMachineClass(Orange", .65, 120)
  • // buy some
  • machine1.buyCans(60)
  • machine2.buyCans(40)
  • // display what's left in machine 1
  • howManyLeft machine1.getNumCans()
  • whatType machine1.getSodaType()
  • System.out.println(howManyLeft " cans of "
    whatType " left in machine 1")
Write a Comment
User Comments (0)
About PowerShow.com