ruby斯宾塞的快乐尾巴(代码片段)

author author     2023-01-15     132

关键词:

# happy_tails.rb
# Spencer Eldred
# Sept 27, 2013

# Activity:
# You are the manager at HappiTails animal shelter. You need to manage your shelter by storing and manipulating information about clients and animals.
# Object Specs:

# Animal:
# An animal should have a name, an age, a gender, a species, and can have multiple toys.
class Animal

  attr_accessor :name, :age, :gender, :species, :toys

  @@new_animal_id = 0

  def initialize(name, age, gender, species, toys)
    @name = name
    @age = age
    @gender = gender
    @species = species
    @toys = toys
    @@new_animal_id += 1
  end

  def self.get_animal_id
    @@new_animal_id
  end

  def add_toy(toy)
    @toys << toy
  end

  def to_s
    "#@name: Age: #@age, Gender: #@gender, Species: #@species, Toys: #@toys"
  end

end

# Client:
# A client should have a name, a number of children, an age, and a number of pets.
class Client

  attr_accessor :name, :number_of_children, :age, :number_of_pets, :animals

  @@new_client_id = 0

  def initialize(name, number_of_children, age)
    @name = name
    @number_of_children = number_of_children
    @age = age
    @number_of_pets = 0
    @animals = []
    @@new_client_id += 1
  end

  def add_animal(animal)
    @animals << animal
    @number_of_pets += 1
  end

  def remove_animal(animal)
    @animals.delete(animal)
    @number_of_pets -= 1
  end

  def display_pets
    @animals.each  |animal| puts animal 
  end

  def self.get_client_id
    @@new_client_id
  end

  def to_s
    "#@name: Number of children: #@number_of_children, Age: #@age, Number of pets: #@number_of_pets"
  end

end

# Shelter:
# The shelter should display all the clients, and all the animals.
class Shelter

attr_accessor :animals, :clients

  def initialize(animals, clients)
    @animals = animals
    @clients = clients
  end

  def add_client(client)
    @clients << client
  end

  def remove_client(client)
    @client.delete(client)
  end

  def add_animal(animal)
    @animals << animal
  end

  def remove_animal(animal)
    @animals.delete(animal)
  end

  def display_clients
    @clients.each  |client|  display_user(client) unless client.name == "Visitor" 
  end

  def display_client(user)
    @clients.each  |client| display_user(client) if client.name == user
  end

  def display_user(client)
    puts client
      if client.number_of_pets > 0
        puts "\t#client.name's' list of adopted animals: \n\t#client.animals.join("\n\t")."
      else
        puts "\t#client.name has not adopted any animals yet."
      end
  end

  def display_animals
    @animals.each  |animal| puts animal 
  end

  def display_animal(pet)
    @animals.each  |animal| puts animal.name 
  end

end # Shelter

# Login with error handling and  custom error
class UserValidationError < StandardError

  def message
    display_banner
    "The name you entered is not on the client list. \n" +
    "Enter your name, or Visitor to log in:"
  end

end

def validate_client(shelter)
  puts ""
  puts "Enter your name, or Visitor to log in:"
  begin
    user = gets.chomp
    raise UserValidationError if !shelter.clients.any?  |client| client.name == user 
  rescue StandardError => e  # runs if there is an error
  puts e.message
  # puts "Retry [y/n]"
  # try_again = gets.chomp
  # if try_again == "n"  # want to exit block if user needs to create client account
  #   login(shelter)
  # end
  retry # runs program starting at the begin block

  ensure # always runs at the end of the block.  good for closing files or closing databases
    puts "Welcom #user, you are now logged in."
    return user
  end
end

# Relationships:
# A client should be able to adopt an animal.
# A client should be able to put an animal up for adoption

def adopt_an_animal(shelter, user)
  puts "\tAdopt an animal"
  animal_list(shelter)
  puts "\nLogged in user account information:\n"
  shelter.display_client(user)
  puts "\nHello #user, type in the name of the animal you would like to adopt:"
  pet = gets.chomp
  if shelter.animals.any?   |animal| animal.name == pet 
    shelter.clients.each do |client|
      if client.name == user
        shelter.animals.each  |animal| client.add_animal(animal) if animal.name == pet 
      end
    end
    shelter.animals.each  |animal| shelter.remove_animal(animal) if animal.name == pet 
    puts "Congratulations #user, you have just adopted #pet!"
  else
    puts "#pet is not one of the animals on the list. Return to menu and try again."
  end
end

def put_animal_up_for_adoption(shelter, user)
  #shelter.display_client(user)
  puts "\tPut animal up for adoption:"
  puts "Is this animal a stray? [y/n]"
  stray = gets.chomp
  if stray == "n"
    puts "Hello #user, here is your current list of pets:"
    shelter.clients.each  |client| client.display_pets if client.name == user
    puts "What pet would you like to put up for adoption:"
    pet = gets.chomp
    puts "youve requested animal: #pet"
    shelter.clients.each do |client|
      if client.name == user
        client.animals.each do |animal|
          shelter.add_animal(animal)
          client.remove_animal(animal)
        end # client.animals.each
      end # if client.name == user
    end # shelter.clients.each
  else
    create_animal(shelter)
    put_animal_up_for_adoption(shelter,user)
  end # if not stray
  puts "#pet has been put up for adoption."
end

# class Animal(name, age, gender, species, toys)
def create_animal(shelter)
  puts "\tCreate an animal"
  puts "Enter the following data for your animal:"
  print "Name: "
  name = gets.chomp
  print "Age: "
  age = gets.chomp.to_i
  print "Gender: "
  gender = gets.chomp
  print "Species: "
  species = gets.chomp
  print "Toys - (enter a comma separated list): "
  toys = []
  toys << gets.chomp
  shelter.add_animal(Animal.new(name, age, gender, species, toys.join.split(", ")))
end

# class Client(name, number_of_children, age, number_of_pets)
def create_client(shelter)
  puts "\tCreate a client:"
  puts "Enter the following data for client:"
  print "Name: "
  name = gets.chomp
  print "Number of children: "
  number_of_children = gets.chomp.to_i
  print "Age: "
  age = gets.chomp.to_i
  shelter.add_client(Client.new(name, number_of_children, age))
end

# Helper methods
def return_to_continue
  puts ""
  puts "press return to continue"
  gets.chomp
end

def quit
  puts "Aloha, come back again!"
end # def quit

def display_banner
  puts `clear`
  puts "Animal Shelter - adopt a new pet today!"
  puts "****************************************"
end

def animal_list(shelter)
  puts ""
  puts "There are #shelter.animals.length animals available for adoption:"
  puts ""
  shelter.display_animals
end

def client_list(shelter)
  puts ""
  puts "Shelter client list: (#shelter.clients.length clients)"
  puts ""
  shelter.display_clients
end

# main menu
def main(shelter, user)
  valid_mode = false
  while !valid_mode
    display_banner
    puts "Enter number to execute request:"
    puts "  1) Display all animals"
    puts "  2) Display all clients"
    puts "  3) Create an animal"
    puts "  4) Create a client"
    puts "  5) Adopt an animal"
    puts "  6) Put an animal up for adoption"
    puts "  Q) Quit"
    mode = gets.chomp.upcase
    if mode == "1" || mode == "2" || mode == "3" || mode == "4" || mode == "5" || mode == "6" || mode == "Q"
      valid_mode = true
      display_banner
      case mode
      when "1"
        animal_list(shelter)
      when "2"
        client_list(shelter)
      when "3"
        create_animal(shelter)
      when "4"
        create_client(shelter)
      when "5"
        if user == "Visitor"
          puts "\n\tAs Visitor, you cannot adopt an animal, \n\tyou must create an account and log in."
        else
          adopt_an_animal(shelter, user)
        end
      when "6"
        if user == "Visitor"
          puts "\n\tAs Visitor, you cannot place an animal up for adoption, \n\tyou must create an account and log in."
        else
          put_animal_up_for_adoption(shelter, user)
        end
      when "Q"
        quit
      end # case mode
      unless mode == "Q"
        return_to_continue
        main(shelter, user) unless mode == "Q"
      end
    else
      puts "You didn't enter a valid selection, try again.\n\n"
    end # if
  end # while !valid_mode
end # def start_menu

# login menu
def login(shelter)
  valid_mode = false
  while !valid_mode
    display_banner
    puts "Select"
    puts "1) Login"
    puts "2) Create Client Account"
    puts "Q) Quit"
    mode = gets.chomp.upcase
    if mode == "1" || mode == "2" || mode == "Q"
      valid_mode = true
      display_banner
      case mode
      when "1"
        user = validate_client(shelter)
        main(shelter, user)
      when "2"
        create_client(shelter)
        client_list(shelter)
        return_to_continue
        login(shelter)
      when "Q"
        quit
      else
        login
      end # case mode
    else
      puts "You didn't enter a valid selection, try again.\n\n"
    end # if
  end # while !valid_mode
end # def login

# initialize method, calls main
def init
  # create the initial animals
  # class Animal(name, age, gender, species, toys)
  bootsie = Animal.new("Bootsie", 12, "female", "cat", ["roaches", "cane spiders", "geckos"])
  lani = Animal.new("Lani", 4, "female", "cat", ["fuzzy ball", "geckos"])
  thor = Animal.new("Thor", 8, "male", "dog", ["tennis ball", "apples"])
  lili = Animal.new("Lili", 4, "female", "cat", ["imaginary friend", "chair"])
  dexter = Animal.new("Dexter", 7, "male" ,"dog", ["chew toy", "stuffed animal"])

  # create the initial clients
  # class Client(name, number_of_children, age, number_of_pets)
  visitor = Client.new("Visitor", 0, 25)
  bob = Client.new("Bob", 4, 76)
  annie = Client.new("Annie", 0, 42)
  adara = Client.new("Adara", 0, 16)
  josh = Client.new("Josh", 0, 18)

  # create the shelter with the initial animals and clients
  animals = [bootsie, lani, thor, lili, dexter]
  clients = [bob, annie, adara, josh,visitor]
  shelter = Shelter.new(animals, clients)

  # call login
  login(shelter)
end

# Start the program with a call to "init"
init

# Instructions:

# Phase 1
# Can create animals and clients

# Phase 2
# New animals and clients can be added to the shelter

# Phase 3
# When creating an animal or client, the user is prompted for information like names, gender etc.

# Phase 4

# At start, the user is prompted with a menu of options:
# display all animals, display all clients, create an animal, create an client
# facilitate client adopts an animal,  facilitate client puts an animal up for adoption
# After selecting from the menu the task the user is prompted through the entire process

ruby红宝石尾巴(代码片段)

查看详情

[luogu4556]雨天的尾巴(代码片段)

[luogu4556]雨天的尾巴luogu发现是一顿子修改然后再询问,那么把修改树上差分一下再线段树合并但是...如果你只有35分...https://www.luogu.org/discuss/show/88259#include<bits/stdc++.h>usingnamespacestd;constint_=1e5+5;intre()intx=0,w=1;charch=get 查看详情

reactcontextapi似乎重新渲染每个组件(代码片段)

...下文。DEMO这是它的工作原理还是我错过了什么?谢谢,斯宾塞答案这是预期的行为。作为消费者的组件在其提供者数据更新时重新呈现。此外,shouldCompon 查看详情

bzoj3307:雨天的尾巴(代码片段)

传送门树上差分+线段树合并+离散化对于修改的路径,树上差分就好了代码:#include<cstdio>#include<iostream>#include<cstring>#include<tr1/unordered_map>#include<algorithm>usingnamespacestd;voidread(int&x) 查看详情

java尾巴电话(代码片段)

查看详情

pythonpython远程尾巴(代码片段)

查看详情

快乐编程学ruby

  人们常说:不忘初心,方得始终。所以,code除了完成工作任务,在最初还应该是富于乐趣的,正所谓,宅男配女仆,我们来了解了解我们的ruby萌妹子吧:-)。              &nb... 查看详情

雨天的尾巴(代码片段)

考试的时候直接扎第一题上了这到题连暴力都没打出来T_T;心路历程:当时想到了离散化(很慌没打出来。。。),树上差分,lca倍增,当时觉滴倍增很难打,一看n<100000,于是选择用向上标记法,然而少了一行代码,,,,爆... 查看详情

javascriptarr处理尾巴0(代码片段)

查看详情

markdownbash,猫,头,尾巴(代码片段)

查看详情

luogu4556雨天的尾巴树上差分线段树合并(代码片段)

传送门 一个套路题……树上差分+线段树合并即可注意一个细节:$pushup$的时候如果最大值为$0$一定要把最大值对应的答案也设为$0$,否则会$WA$第二个点另外如果这道题空限变小了请不要交这份代码,因为这份代码没有卡空... 查看详情

[vani有约会]雨天的尾巴(代码片段)

我之前考试是遇到过这题,但是数据范围k<=20,状压就能过。结果原题范围k<=100000……果断线段树合并。普及线段树合并:比如两个相同大小的线段树,将b树各个区间上的值合并到a树上,从树根开始合并,然后递归合并左右... 查看详情

2dproceduralanimation小试:尾巴,翅膀,触手等等(代码片段)

ProceduralAnimation的百科解释:https://en.wikipedia.org/wiki/Procedural_animation想深入了解UE4引擎中的:controlrig,Unity引擎中的:AnimationRigging最近在学习ProceduralAnimation相关的只是,先从2D开始。最好理 查看详情

markdown快乐的钱包,样机(代码片段)

查看详情

gdoi2014模拟雨天的尾巴(代码片段)

题目深绘里一直很讨厌雨天。灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切。虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒了几座老房子,几棵老树被连根拔起,以及田地里的粮食被... 查看详情

你值得拥有的快乐(代码片段)

前言人生无时无刻都有快乐,只是等待你去发现。快乐是一种宝贵的资源,它是生活的意义之一与人生的价值之一。如果快乐不能与人分享,这不算是真正的快乐了。所以,我来分享分享并且时时更新ヾ(≧▽≦*)o内容神注释:... 查看详情

html健康的马快乐骑手(代码片段)

查看详情

数据结构:栈——在“尾巴”上搞事情(代码片段)

目录前言1.栈的概念2.进出栈动画演示3.通过几道题目来理解LIFO4.栈的实现4.1Stack.h文件4.2Stack.c文件4.3test.c文件4.4测试运行后记前言hello,大家好,今天我们来继续更新数据结构方面的文章,这期主要用来介绍栈和队列... 查看详情