This was the first assignment that I helped my friend with. The problem was to create an "ATM" simulation program. The user would start with $1000 in their account and they would enter in a command: B -- show balance, D -- deposit, W -- withdrawal, or Q -- quit. If they selected D or W, the user is asked to enter the amount to deposit/withdraw. Assumptions are that valid numbers are entered, but there still needed to be a check for somebody trying to withdraw more money than was available in their account.

Java 

import java.util.Scanner;

public class Atm {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        double balance = 1000;
        while (true) {
            System.out.println("Enter the command B,D,W, or Q to quit");
            String command = scanner.nextLine();
            char ch = 'X';
            if (command.length() == 1) {
                ch = command.charAt(0);
            }
            if (ch == 'B') {
                printBalance(balance);
            } else if (ch == 'D') {
                System.out.println("Enter the amount to deposit");
                String amount = scanner.nextLine();
                double amt = Double.parseDouble(amount);
                balance += amt;
                printBalance(balance);
            } else if (ch == 'W') {
                System.out.println("Enter the amount to withdraw");
                String amount = scanner.nextLine();
                double amt = Double.parseDouble(amount);
                if (amt > balance) {
                    System.out.println("Sorry insufficient funds");
                } else {
                    balance -= amt;
                    printBalance(balance);
                }
            } else if (ch == 'Q') {
                System.exit(0);
            } else {
                System.out.println("Sorry didn't understand that command");
            }
        }
    }

    public static void printBalance(double balance) {
        System.out.println("Current balance= $" + balance);
    }
}

 

Again, the idea was to do this as a student in the class would do it. At the time of the assignment, they had not learned a case statement, so I did not use that. They had not learned objects yet either, hence the structure used here. Also, I generally wanted to do this as simply as possible, since I wanted to compare it in other languages.

C#

using System;

class Atm
{
static void Main(string[] args)
{
double balance = 1000;
while (true)
{
Console.WriteLine("Select B, D, W, or Q to quit");
string command = Console.ReadLine();
char ch = 'X';
if (command.Length == 1)
{
ch = command[0];
}
if (ch == 'B')
{
printBalance(balance);
}
else if (ch == 'D')
{
Console.WriteLine("How much do you want to deposit?");
string amount = Console.ReadLine();
double amt = Double.Parse(amount);
balance += amt;
printBalance(balance);
}
else if (ch == 'W')
{
Console.WriteLine("How much do you want to withdraw?");
string amount = Console.ReadLine();
double amt = Double.Parse(amount);
if (amt > balance)
{
Console.WriteLine("Sorry but you only have $" + balance);
}
else
{
balance -= amt;
printBalance(balance);
}
}
else if (ch == 'Q')
{
Environment.Exit(0);
}
else
{
Console.WriteLine("Sorry but that command was not understood");
}
}
}

static void printBalance(double balance)
{
Console.WriteLine("Your current balance is $" + balance);
}
}
 

C++

#include <iostream>
#include <string>
using namespace std;

void printBalance(double balance);

int main(){
double balance = 1000;
while(true){
cout << "Enter command B,D,W, or Q to quit" << endl;
string command;
cin >> command;
char ch = 'X';
if (command.length() == 1){
ch = command[0];
}
if (ch == 'B'){
printBalance(balance);
} else if (ch == 'D'){
string amount;
cout << "Enter amount to deposit" << endl;
cin >> amount;
double amt = atof(amount.c_str());
balance += amt;
printBalance(balance);
} else if (ch == 'W'){
string amount;
cout << "Enter amount to withdraw" << endl;
cin >> amount;
double amt = atof(amount.c_str());
if (amt > balance){
cout << "Sorry insufficient funds" << endl;
} else {
balance -= amt;
printBalance(balance);
}
} else if (ch == 'Q'){
return 0;
} else {
cout << "Sorry unknown command" << endl;
}
}
return 0;
}

void printBalance(double balance){
cout << "Current balance is $" << balance << endl;
}

Ruby

def printBalance(balance)
puts 'Current balance is $'+balance.to_s
end

balance = 1000
while true
puts 'Enter command: B, D, W, or Q to quit'
command = gets
ch = 'X'
if command.length == 2
ch = command[0,1]
end
if ch == 'B'
printBalance(balance)
elsif ch == 'D'
puts 'Enter amount to deposit'
amount = gets
balance += amount.to_f
printBalance(balance)
elsif ch == 'W'
puts 'Enter amount to withdraw'
amount = gets
if (amount.to_f > balance)
puts 'Sorry not enough funds'
else
balance -= amount.to_f
printBalance(balance)
end
elsif ch == 'Q'
break
else
puts 'Did not understand that command'
end
end

Python

def printBalance(balance):
print "Current balance is $" + str(balance)
balance = 1000
command = "X"
while command != "Q":
command = raw_input("Enter B,D,W, or Q to quit\n")
if command == "B":
printBalance(balance)
elif command == "D":
amount = float(raw_input("Enter amount to deposit\n"))
balance += amount
printBalance(balance)
elif command == "W":
amount = float(raw_input("Enter amount to withdraw\n"))
if amount > balance:
print "Sorry insufficient funds"
else :
balance -= amount
printBalance(balance)
elif command == "Q":
break
else:
print "Sorry didn't understand that command"