Write a JAVA program that stores and displays the on-duty schedule for a team. F
ID: 666043 • Letter: W
Question
Write a JAVA program that stores and displays the on-duty schedule for a team.
Features
Display today’s Support Hero.
Display a single user’s schedule showing the days they are assigned to Support
Hero.
Display the full schedule for all users in the current month.
Users should be able to mark one of their days on duty as undoable
The system should reschedule accordingly
Should take into account weekends and California’s holidays.
Users should be able to swap duty with another user’s specific day
STARTING ORDER
[''Sherry'', ''Boris'', ''Vicente'', ''Matte'', ''Jack'', ''Sherry'',
''Matte'', ''Kevin'', ''Kevin'', ''Vicente'', ''Zoe'', ''Kevin'',
''Matte'', ''Zoe'', ''Jay'', ''Boris'', ''Eadon'', ''Sherry'',
''Franky'', ''Sherry'', ''Matte'', ''Franky'', ''Franky'', ''Kevin'',
''Boris'', ''Franky'', ''Vicente'', ''Luis'', ''Eadon'', ''Boris'',
''Kevin'', ''Matte'', ''Jay'', ''James'', ''Kevin'', ''Sherry'',
''Sherry'', ''Jack'', ''Sherry'', ''Jack'']
Explanation / Answer
Ans:
require 'active_record'
require 'logger'
require 'holidays'
require 'holidays/us'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Schema.define do
self.verbose = false
create_table "shifts", force: :cascade do |t|
t.string "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest", default: "pw"
t.integer "role", default: "hero"
end
end
class Shift < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :shifts
enum role: %w(hero admin)
has_secure_password
end
class ShiftDates
def initialize(from:)
self.from = from
end
include Enumerable
def each
return to_enum :each unless block_given?
current = from()
loop do
yield current if shift? current
current += 1
end
end
private
attr_accessor :from
def shift?(date)
weekday?(date) && !holiday?(date)
end
def holiday?(date)
Holidays.on(date, :observed).any?
end
def weekday?(date)
monday = 1
friday = 5
monday <= date.wday && date.wday <= friday
end
end
shift_queue = ['Sherry', 'Boris', 'Vicente', 'Matte', 'Jack', 'Sherry','Matte', 'Kevin', 'Kevin', 'Vicente', 'Zoe', 'Kevin','Matte', 'Zoe', 'Jay', 'Boris', 'Eadon', 'Sherry','Franky', 'Sherry', 'Matte', 'Franky', 'Franky', 'Kevin','Boris', 'Franky', 'Vicente', 'Luis', 'Eadon', 'Boris','Kevin', 'Matte', 'Jay', 'James', 'Kevin', 'Sherry','Sherry', 'Jack', 'Sherry', 'Jack']]
users_by_name = ActiveRecord::Base.transaction do
shift_queue.uniq.each_with_object({}) do |name, result|
result[name] = User.create! name: name
end
end
dates = ShiftDates.new(from: Date.today)
timestamp = Time.current
values = shift_queue.zip(dates).map { |name, date| "('#{date.to_date.to_s(:db)}', #{User.find_by(name: name).id}, '#{timestamp.to_s(:db)}', '#{timestamp.to_s(:db)}')" }.join(", ")
Shift.connection.execute "INSERT INTO shifts (`date`, `user_id`, `updated_at`, `created_at`) VALUES #{values}"
Shift.count
Shift.first.user.shifts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.