Advertisement
Guest User

Array Implementation

a guest
May 13th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.98 KB | None | 0 0
  1. case class Customer(name: String, private var cash: Int) {
  2.     def buy(price: Int): Unit = {
  3.         if(canBuy(price))
  4.             cash -= price
  5.     }
  6.    
  7.     def canBuy: Int => Boolean = _ <= cash
  8. }
  9.  
  10. class Applestore(capacity: Int, itemsCount: Int) {
  11.     private var customersWaiting: Array[Customer] = new Array[Customer](capacity)
  12.     private var price: Int      = 900
  13.     private var serveTime: Int  = 60
  14.     private var pointer: Int    = 0
  15.    
  16.     def canEnqueue: Boolean = pointer < capacity
  17.     def canServe: Boolean = itemsCount > 0
  18.     def countQueue: Int = pointer
  19.     def timeGetServed(customer: Customer): Int = customersWaiting.indexOf(customer) * 60
  20.    
  21.     def enqueue(customer: Customer): Unit = {
  22.         if(!canEnqueue)
  23.             return
  24.        
  25.         customersWaiting(pointer) = customer
  26.         pointer += 1
  27.     }
  28.    
  29.     def serve: Unit = {
  30.         if(!canServe)
  31.             return
  32.        
  33.         val customer = customersWaiting.head
  34.        
  35.         if(customer.canBuy(price))
  36.             customer.buy(price)
  37.        
  38.         customersWaiting = customersWaiting.tail :+ null
  39.         pointer -= 1
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement