Pushok

course

Jul 29th, 2015
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | None | 0 0
  1. TimeManager = {}
  2.  
  3. do
  4.     function TimeManager:new(o)
  5.         local o = o or {}
  6.         setmetatable(o, self)
  7.         self.__index = self
  8.         return o
  9.     end
  10.  
  11.     function TimeManager:setTime(t)
  12.         if type(t) == 'table' then
  13.             self._selftime = (t[1] or 0) + (t[2] or 0) * 60 + (t[3] or 0) * 60 * 24
  14.         elseif type(t) == 'number' then
  15.             self._selftime = t
  16.         else
  17.             error('Wrong parameter!')
  18.         end
  19.        
  20.         return self
  21.     end
  22.  
  23.     function TimeManager:asNumber()
  24.         return self._selftime
  25.     end
  26.    
  27.     function TimeManager:asAcademicNumber()
  28.         return self._selftime / 0.75
  29.     end
  30.  
  31.     function TimeManager:asTime()
  32.         local tbl = {}
  33.        
  34.         tbl[1] = math.floor(self._selftime % 60)
  35.         tbl[2] = math.floor(self._selftime / 60) % 24
  36.         tbl[3] = math.floor(math.floor(self._selftime / 60) / 24)
  37.        
  38.         return tbl
  39.     end
  40.  
  41.     function TimeManager:toStringFull()
  42.         local tbl = self:asTime()
  43.         d = tostring(tbl[3])
  44.         h = tostring(tbl[2])
  45.         m = tostring(tbl[1])
  46.         return form(d) .. ':' .. form(h) .. ':' .. form(m)
  47.     end
  48.    
  49.     function TimeManager:toString()
  50.         local tbl = self:asTime()
  51.         h = tostring(tbl[2])
  52.         m = tostring(tbl[1])
  53.         return form(h) .. ':' .. form(m)
  54.     end
  55.  
  56.     function TimeManager:addTime(t)
  57.         local mins = 0
  58.         if type(t) == 'table' then
  59.             mins = ((TimeManager:new()):setTime(t)):asNumber()
  60.         elseif type(t) == 'number' then
  61.             mins = t
  62.         else
  63.             error('Wrong parameter!')
  64.         end
  65.        
  66.         self._selftime = self._selftime + mins
  67.     end
  68.  
  69.     function TimeManager.__lt(op1, op2)
  70.         if tostring(op1) == 'TimeManager' and tostring(op2) == 'TimeManager' then
  71.             return op1._selftime < op2._selftime
  72.         elseif tostring(op1) == 'TimeManager' and type(op2) == 'number' then
  73.             return op1._selftime < op2
  74.         end
  75.        
  76.         return false
  77.     end
  78.    
  79.     function TimeManager.__gt(op1, op2)
  80.         if tostring(op1) == 'TimeManager' and tostring(op2) == 'TimeManager' then
  81.             return op1._selftime > op2._selftime
  82.         elseif tostring(op1) == 'TimeManager' and type(op2) == 'number' then
  83.             return op1._selftime > op2
  84.         end
  85.        
  86.         return false
  87.     end
  88.  
  89.     function TimeManager.__add(op1, op2)
  90.         local res = TimeManager:new()
  91.         if tostring(op1) == 'TimeManager' and tostring(op2) == 'TimeManager' then
  92.             res:setTime(op1:asNumber() + op2:asNumber())
  93.         elseif tostring(op1) == 'TimeManager' and type(op2) == 'number' then
  94.             res:setTime(op1:asNumber() + op2)
  95.         else
  96.             error('Wrong operands')
  97.         end
  98.        
  99.         return res
  100.     end
  101.    
  102.     function TimeManager.__sub(op1, op2)
  103.         local res = TimeManager:new()
  104.         if tostring(op1) == 'TimeManager' and tostring(op2) == 'TimeManager' then
  105.             res:setTime(op1:asNumber() - op2:asNumber())
  106.         elseif tostring(op1) == 'TimeManager' and type(op2) == 'number' then
  107.             res:setTime(op1:asNumber() - op2)
  108.         else
  109.             error('Wrong operands')
  110.         end
  111.        
  112.         return res
  113.     end
  114.  
  115.     function TimeManager.__tostring()
  116.         return "TimeManager"
  117.     end
  118.  
  119.     function form(str)
  120.         return ((#str == 0) and '00' or ((#str == 1) and '0' or '')) .. str
  121.     end
  122.  
  123.     function createManager(t)
  124.         local mng = TimeManager:new()
  125.         mng:setTime(t)
  126.         return mng
  127.     end
  128.    
  129.     function timeToMin(t)
  130.         local mng = TimeManager:new()
  131.         mng:setTime(t)
  132.         return mng:asNumber()
  133.     end
  134.  
  135.     function minToTime(t)
  136.         local mng = TimeManager:new()
  137.         mng:setTime(t)
  138.         return mng:asTime()
  139.     end
  140.  
  141.     function mtToString(t)
  142.         local mng = TimeManager:new()
  143.         mng:setTime(t)
  144.         return mng:toString()
  145.     end
  146. end
  147.  
  148. course = {
  149.     {"Brief",                           15},
  150.     {"Java Technology",                 15},
  151.     {"NetBeans Development + Exercise", 30},
  152.     {"Java Specials",                   5 },
  153.     {"Primitive types and literals",    15},
  154.     {"Variables",                       15},
  155.     {"------------Exercise",            30},
  156.     {"Operations",                      10},
  157.     {"java.lang",                       10},
  158.     {"------------Exercise",            30},
  159.     {"Strings",                         10},
  160.     {"Arrays",                          15},
  161.     {"------------Exercise",            30},
  162.     {"Input/Output",                    15},
  163.     {"Branching",                       15},
  164.     {"Cycles",                          15},
  165.     {"------------Exercise",            30},
  166.     {"Methods",                         15},
  167.     {"Reference Types",                 5 },
  168.     {"------------Exercise",            30},
  169.     {"Class",                           15},
  170.     {"------------Exercise",            30},
  171.     {"Encapsulation",                   10},
  172.     {"Static Members",                  10},
  173.     {"Enumerations",                    10},
  174.     {"------------Exercise",            60},
  175.     {"Packages",                        15},
  176.     {"Inheritance",                     15},
  177.     {"Polymorphism",                    15},
  178.     {"------------Exercise",            60},
  179.     {"Interfaces",                      15},
  180.     {"Abstract classes",                10},
  181.     {"------------Exercise",            60},
  182.     {"Nested classes",                  10},
  183.     {"Exceptions",                      10},
  184.     {"Collections",                     10},
  185.     {"------------Exercise",            60}
  186. }
  187.  
  188. manager = TimeManager:new()
  189. hours = timeToMin({0, 4});
  190. cof_each = timeToMin({0, 2});
  191. cof_max = 1
  192. cof_time = 15;
  193. start_time = timeToMin({0, 10})
  194. delta_time = 0
  195.  
  196. do
  197.     local last_break = start_time
  198.     local cof_count = 0
  199.     local time_count = 0
  200.     local day_count = 1
  201.    
  202.     manager:setTime(start_time)
  203.     print('---------' .. '[Day' .. day_count .. ']' .. '---------')
  204.     for _, v in pairs(course) do
  205.         if ((manager + (v[2] + (v[3] or 0) - delta_time)) - start_time):asNumber() > hours then
  206.             time_count = time_count + manager:asNumber() - start_time
  207.             manager:setTime(start_time)
  208.             cof_count = 0
  209.             last_break = start_time
  210.            
  211.             --add new day separator
  212.             day_count = day_count + 1
  213.             print('---------' .. '[Day' .. day_count .. ']' .. '---------')
  214.            
  215.             --add new day brief
  216.             print(manager:toString() .. " - " .. (manager + 10):toString() .. " " .. "New day Brief")
  217.             manager:addTime(10)
  218.         end
  219.    
  220.         if (manager:asNumber() + v[2]) > last_break + cof_each and cof_count < cof_max then
  221.             print(manager:toString() .. " - " .. (manager + cof_time):toString() .. " " .. "Coffe break")
  222.             manager:addTime(cof_time)
  223.             last_break = manager:asNumber()
  224.             cof_count = cof_count + 1
  225.         end
  226.        
  227.         if #v == 2 then
  228.             print(manager:toString() .. " - " .. (manager + v[2]):toString() .. " " .. v[1])
  229.             manager:addTime(v[2])
  230.         elseif #v == 3 then --for a case when table have additional exercise time
  231.             print(manager:toString() .. " - " .. (manager + v[2]):toString() .. " " .. v[1])
  232.             manager:addTime(v[2])
  233.            
  234.             print(manager:toString() .. " – " .. (manager + v[3]):toString() .. " Exercise " .. v[1])
  235.             manager:addTime(v[3])
  236.         end
  237.     end
  238.     time_count = time_count + manager:asNumber() - start_time
  239.     print("Total hours: " .. mtToString(time_count))
  240. end
Advertisement
Add Comment
Please, Sign In to add comment