Giter Club home page Giter Club logo

atm's Introduction

Exercise: ATM Simulator

Specifications

Read initial balance from file, or use constant

  • Write a cli script that starts by reading a customer’s bank balance from a file named balance.txt.
  • This file contains a single line with the customer’s starting balance.
  • If the file does not exist, use a starting balance of 100.0, which must be a constant.

Prompt the customer for input

  • The program will then repeatedly prompt the customer to deposit money, withdraw money, inspect balance, or quit, using the letters D, W, B, and Q.
  • If any other input is given, the program has to remind the customer of the available commands.
  • The program must accept input in uppercase or lowercase.

Deposit money

  • For deposits, the program prompts for an amount.
  • The amount must be greater than zero.
  • In case of invalid input, the program must give an appropriate error message that tells the customer how to fix it.
  • If the amount is valid, the program will add the deposit amount to the customer’s balance and display the new balance.

Withdraw money

  • For withdrawals, the program prompts for an amount.
  • The amount must be greater than zero and less than or equal to the current balance.
  • In case of invalid input, the program must give an appropriate error message that tells the customer how to fix it.
  • If the amount is valid, the program will subtract the withdrawal amount from the customer’s balance and display the new balance.

Inspect balance

  • For a balance inspection, the program simply displays the current balance.

Write balance to file

  • When the customer chooses to quit, the program will write the current balance back to the balance.txt file.
  • If the file does not yet exist, it will be created.
  • The program will inform about a successful write or give an error message.

Hints

To read user input from the command line, use gets.chomp for Strings and gets.to_f for numbers (with decimals).

  • You could just write a procedural script to complete this task. But as you want to learn object oriented programming with Ruby, you should write a class Atm with several short methods.
  • Name the file that contains the class atm.rb
  • Create a second file runner.rb with this content:
  load "./atm.rb"
  Atm.new.run
  • You should now be able to run the program on the command line with: ruby runner.rb
  • You should also be able to run the program in irb - just enter the same two lines that you find in the runner.rb file.

To be able to call Atm.new.run your class Atm needs an initializer that has no mandatory arguments and a method called run that executes the program.

Bonus

Write tests to ensure your ATM is performing correctly.

For this exercise do not declare any methods as private and write tests for most of them.

To be able to test the user input parts, extract all gets calls into methods and stub this methods in your tests.

atm's People

Contributors

mediafinger avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.