AR삽질러

Ruby Class例題 - 銀行口座管理システム 본문

Ruby/Ruby-日本語

Ruby Class例題 - 銀行口座管理システム

아랑팡팡 2023. 12. 19. 00:10
728x90

 

Ruby Class例題 - 銀行口座管理システム

 

説明

 - Rubyのクラスを使用して簡単な銀行口座管理システムを実装します。 このシステムは、顧客情報管理、口座作成、入出金処理などの機能を提供する必要があります。

 

要求事項

1. 顧客クラス(Client)の実装
 - 顧客の名前と年齢を属性として持ちます。
 - ユーザーから名前と年齢を入力してもらい、オブジェクトを初期化します。

2. アカウントクラス(Account)の実装
 - 口座番号、所有者名、所有者年齢、現在の残高を属性として持ちます。
 - 入金(deposit)メソッド:金額を入力してもらい、口座残高を増加させます。
 - 出金(withdraw)メソッド:金額を入力して口座残高を減少させます。
 - 口座情報出力(info)メソッド:口座の詳細情報を出力します。

3. 銀行クラス(Bank)の実装:
 - 銀行システムの全体的な動作を管理します。
 - 通帳開設のための顧客登録機能を提供します。
 - 顧客の年齢が 19 歳以上の場合、口座を作成できるようにします。
 - ランダム口座番号生成機能を含みます。

 

実装方法
 - クラスとメソッドを定義し、各クラスのインスタンスを作成して相互作用するようにします。
 - ユーザー入力を受け取り、プログラムの流れを制御します。
 - 入金と出金時の残高が正しく更新されていることを確認します。
 - 口座開設条件(例:年齢制限)を適切に処理します。

 

class Client

    attr_reader :name, :age

    def initialize
        print "お名前 : "
        @name = gets.chomp
        print "年齢 : "
        @age = gets.chomp.to_i
    end
end

class Account

    attr_reader :account_number, :owner, :age, :balance
    
    def initialize(account_number, owner, age, balance)
        @account_number = account_number
        @owner = owner
        @age = age
        @balance = balance
    end

    def deposit
      print "入金 金額 入力 : "
      deposit_money = gets.chomp.to_i
      if deposit_money <= 0 || deposit_money > 100000
        puts "0ウォン以上10,000円以下で入力してください。"
      else
        @balance += deposit_money
        puts "#{deposit_money}円が入金されました。\n現在の残高は #{@balance}円です。\n"
        self.info
      end
    end
  
    def withdraw
      print "出金 金額 入力 : "
      withdraw_money = gets.chomp.to_i
      if withdraw_money > @balance || withdraw_money <= 0
        puts "出金金額は0円以上、そして現在の残高以上でなければなりません。\n 現在の残高 #{@balance}"
      else
        @balance -= withdraw_money
        puts "#{withdraw_money}円が出金されました。\n現在の残高は #{@balance}円 です。\n"
        self.info
      end
    end
  
    def info
        puts "口座番号: #{@account_number}, 所有者: #{@owner}, 年齢: #{@age}, 現在の残高: #{@balance}円"
        print " 入金 1、出金 2、終了 3 : "
        info_select = gets.chomp.to_i
        case info_select
        when 1
            puts "\n---入金に移動します。---\n"
            self.deposit
        when 2
            puts "\n---出金に移動します。---\n"
            self.withdraw
        when 3
            puts "\n---プログラムを終了します..---\n"
        else
            puts "\n間違って入力しました.."
        end
    end
  end
  

class Bank
    def start
        puts "---こんにちは。K銀行へようこそ🫡---" 
        print "通帳の開設をご希望ですか? y | n : "
        next_start = gets.chomp
        if next_start == 'y'
            register_client
        else
            puts "---プログラムを終了します。---"
        end
    end

    def register_client
        client = Client.new
        if client.age >= 19
            puts "通帳開設が可能です。\n\n通帳開設窓口にご案内いたします。---\n"
            create_account(client)
        else
            puts "---未成年者は通帳開設が不可能です。---"
            puts "---プログラムを終了します。---"
        end    
    end

    def create_account(client)
        account_number = random_account_number
        account = Account.new(account_number, client.name, client.age, 0)
        puts "#{client.name}様の口座ができました。"
        account.info
    end

    def random_account_number
        number = ''
        11.times { number += rand(0..9).to_s }
        number.insert(3, '-').insert(7, '-')
    end
end

bank = Bank.new
bank.start

 

728x90
반응형
LIST

'Ruby > Ruby-日本語' 카테고리의 다른 글

Ruby クラス変数とクラスメソッド  (0) 2023.12.19
Ruby アクセス メソッド  (0) 2023.12.19
Ruby オリジナルのクラスを作成する  (0) 2023.12.18
Ruby 戻り返し処理  (0) 2023.12.18
Ruby ハッシュ  (0) 2023.12.18