Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Let's make some simple programs!

Name: Anonymous 2010-05-08 4:03

Because I know that half of /prog/ can't program, let's make simple ones!

---------------------- MySavings.java ----------------------

/**
 * @(#)MySavings.java
 *
 * MySavings application
 *
 * @author Tim H
 * @version 1.00 2010/5/7
 */
 
import java.util.Scanner;

public class MySavings {
   
    public static void printInterface() {
        System.out.print(
            "1. Show total in bank.\n" +
            "2. Add a penny.\n" +
            "3. Add a nickel.\n" +
            "4. Add a dime.\n" +
            "5. Add a quarter.\n" +
            "6. Take money of out bank.\n" +
            "Enter 0 to quit.\n" +
            "Enter your choice: "
            );
    }
   
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        int choice = 1;
        int moneyChoice = 0;
        double amount = 0;
       
        PiggyBank bank = new PiggyBank();
       
        while(choice > 0){
            printInterface();
            choice = input.nextInt();
           
            switch(choice){
                case 0: System.out.println("(c)Tim H 2010\nHave a nice day!"); break;
                case 1: System.out.println("Your current total is: " + bank.GetTotal()); break;
                case 2:
                case 3:
                case 4:
                case 5: moneyChoice = choice - 2; bank.AddMoney(moneyChoice); break;
                case 6:
                    System.out.print("Enter the amount you wish to withdraw: ");
                    amount = input.nextDouble();
                    bank.SubtractTotal(amount);
                    break;
                default:
                    System.out.println("Please enter a choice between 1-6!");
                    break;
            }
               
        }

---------------------- PiggyBank.java ----------------------


class PiggyBank {
    double total;
   
   
    public PiggyBank(){
        total = 100;
    }
   
    public void AddMoney(int choice){
        switch (choice) {
            case 0:
                total += 0.01;
                System.out.println("One penny added to total!\nYour total is now: " + total);
                break;
            case 1:
                total += 0.05;
                System.out.println("One nickel added to total!\nYour total is now: " + total);
                break;
            case 2:
                total += 0.10;
                System.out.println("One dime added to total!\nYour total is now: " + total);
                break;
            case 3:
                total += 0.25;
                System.out.println("One quarter added to total!\nYour total is now: " + total);
                break;
           
        }
    }
   
    public double GetTotal(){
        return total;
    }
   
    public void SubtractTotal(double amount){
        total -= amount;
        System.out.println(amount + " has been subtracted from your total!\nYour total is now: " + total);
    }
}

    }
   
}

Name: Anonymous 2010-05-08 12:30

>>1
I realize you are one of those who "can't program", please find attached a corrected version of your program. In future, please refrain from posting on /prog/ until you have achieved satori.

import java.util.*;
public class Savings {
    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        Account account = new Account();
        SortedMap<Integer, AccountChoice> choices = new TreeMap<Integer, AccountChoice>();
       
        choices.put(0, new AccountChoice() {
            public String toString() { return "Close account and exit."; }
            public void invoke(Account ac) { System.exit(0); }
        });
        choices.put(1, new AccountChoice() {
            public String toString() { return "Show balance."; }
            public void invoke(Account ac) {
                System.out.println(new java.text.DecimalFormat("#0.00").format(ac.getBalance()/100.0));
            }
        });
        choices.put(2, new AccountChoice() {
            public String toString() { return "Add a "+ Currency.PENNY + "."; }
            public void invoke(Account ac) {
                try { ac.deposit(Currency.PENNY.toCents()); }
                catch(IllegalArgumentException e) { System.out.println(e.getMessage()); }
            }
        });
        choices.put(3, new AccountChoice() {
            public String toString() { return "Add a "+ Currency.NICKEL + "."; }
            public void invoke(Account ac) {
                try { ac.deposit(Currency.NICKEL.toCents()); }
                catch(IllegalArgumentException e) { System.out.println(e.getMessage()); }
            }
        });
        choices.put(4, new AccountChoice() {
            public String toString() { return "Add a "+ Currency.DIME + "."; }
            public void invoke(Account ac) {
                try { ac.deposit(Currency.DIME.toCents()); }
                catch(IllegalArgumentException e) { System.out.println(e.getMessage()); }
            }
        });
        choices.put(5, new AccountChoice() {
            public String toString() { return "Add a "+ Currency.QUARTER + "."; }
            public void invoke(Account ac) {
                try { ac.deposit(Currency.QUARTER.toCents()); }
                catch(IllegalArgumentException e) { System.out.println(e.getMessage()); }
            }
        });
        choices.put(6, new AccountChoice() {
            public String toString() { return "Make a withdrawl."; }
            public void invoke(Account ac) {
                System.out.print("Enter the amount you wish to withdraw (cents): ");
                int amount = sc.nextInt();
                try { ac.withdraw(amount); System.out.println("Withdrawl successful."); }
                catch(Exception e) { System.out.println(e.getMessage()); }
            }
        });
       
        while(true) {
            for(Map.Entry<Integer, AccountChoice> choice : choices.entrySet())
                System.out.println(choice.getKey() + ". " + choice.getValue());
            System.out.print("Enter your choice: ");
            int option = sc.nextInt();
            if(!choices.containsKey(option)) System.out.println("Please enter a valid choice.");
            else choices.get(option).invoke(account);
        }
    }
}
abstract class AccountChoice {
    public abstract void invoke(Account ac);
}
enum Currency {
    PENNY(1, "penny"),
    NICKEL(5, "nickel"),
    DIME(10, "dime"),
    QUARTER(25, "quarter");
   
    private long cents;
    private String name;
   
    private Currency(long cents, String name) {
        this.cents = cents;
        this.name = name;
    }
    public long toCents() {
        return cents;
    }
    public String toString() {
        return name;
    }
}   
class Account {
    private long savings;
    public Account() {
        savings = 0;
    }
    public void deposit(long cents) {
        if(cents < 0) throw new IllegalArgumentException("Cannot deposit negative cents.");
        savings += cents;
    }
    public long getBalance() {
        return savings;
    }
    public void withdraw(long cents) throws InsufficientFundsException {
        if(cents > savings) throw new InsufficientFundsException("Insufficient funds for withdrawl.");
        if(cents < 0) throw new IllegalArgumentException("Cannot withdraw negative cents.");
        savings -= cents;
    }
}
class InsufficientFundsException extends IllegalArgumentException {
    public InsufficientFundsException(String message) {
        super(message);
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List