You are on page 1of 9

Bank: Account:

import java.text.NumberFormat; public class Account { private double balance; private Customer cust;

/** * constructor * pre: none * post: An account created. Balance and customer data * initialized with parameters. */ public Account(double bal, String fName, String lName, String str, String city, String st, String zip) { balance = bal; cust = new Customer(fName, lName, str, city, st, zip); }

/** * Retruns the current balance. * pre: none * post: The account balance has been returned */ public double getBalance() { return(balance); }

/** * A deposit is made to the account. * pre: none * post: The balance has been increased by the amount of the deposit. */ public void deposit(double amt) { balance += amt; }

/** * A withdrawal is made from the account if there is enough money. * pre: none * post: The balance has bee decreased by the amount withdrawn. */ public void withdrawal(double amt) { if (amt <=balance) { balance -= amt; } else { System.out.println("Not enough money in account."); } }

/** * Changes the customers address * pre: none * post: The customers address has been updated with the parameters. */ public void changeAddress(String street, String city, String state, String zip) { cust.changeStreet(street); cust.changeCity(city); cust.changeState(state); cust.changeZip(zip); }

/** * Returns a String that represents the Account object. * pre: none * post: A string representing the Account object has been returned. */ public String toString() { String accountString; NumberFormat money = NumberFormat.getCurrencyInstance(); accountString = cust.toString(); accountString += "Current balance is " + money.format(balance); return(accountString); } }

Customer:
public class Customer { private String firstName, lastName, street, city, state, zip;

/** * constructor * pre: none * post: A Customer object has been created. * Customer data has been initialized with parameters. */ public Customer(String fName, String lName, String str, String c, String s, String z) { firstName = fName; lastName = lName; street = str; city = c; state = s; zip = z; }

/** * Changes the customers street

* pre: none * post: The customers street has been changed */ public void changeStreet(String street) { this.street = street; }

/** * Changes the customers city * pre: none * post: The customers city has been changed */ public void changeCity(String city) { this.city = city; }

/** * Changes the customers state * pre: none * post: The customers state has been changed */ public void changeState(String state) { this.state = state; }

/** * Changes the customers zip * pre: none * post: The customers zip has been changed */ public void changeZip(String zip) { this.zip = zip; }

/** * Returns a String that represents the Customer object. * pre: none * post: A string representing the Account object has been returned. */ public String toString() { String custString; custString = firstName + " " + lastName + "\n"; custString += street + "\n"; custString += city + ", " + state + " " + zip + "\n"; return(custString); } }

Bank:

import java.util.Scanner; import java.text.NumberFormat; public class Bank { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String street, city, state, zip; Account munozAccount = new Account(250, "Maria", "Munoz", "110 Glades Road", "Mytown", "FL", "33445"); Scanner input = new Scanner(System.in); double data; NumberFormat money = NumberFormat.getCurrencyInstance(); System.out.println(munozAccount); System.out.print("\nEnter a new Street: "); street = input.nextLine(); System.out.print("Enter a new City: "); city = input.nextLine(); System.out.print("Enter a new State: "); state = input.nextLine(); System.out.print("Enter a new Zip: "); zip = input.nextLine(); munozAccount.changeAddress(street, city, state, zip); System.out.println("\nAccount changed to:"); System.out.println(munozAccount); System.out.print("\nEnter deposit amount: "); data = input.nextDouble(); munozAccount.deposit(data); System.out.println("Balance is: " + money.format(munozAccount.getBalance())); System.out.print("Enter withdrawal amount: "); data = input.nextDouble(); munozAccount.withdrawal(data); System.out.println("Balance is: " + money.format(munozAccount.getBalance())); } }

Console:
Maria Munoz 110 Glades Road Mytown, FL 33445 Current balance is $250.00 Enter a new Street: 1 Infinite Loop Enter a new City: Cupertino Enter a new State: CA Enter a new Zip: 95014

Account changed to: Maria Munoz 1 Infinite Loop Cupertino, CA 95014 Current balance is $250.00 Enter deposit amount: 583 Balance is: $833.00 Enter withdrawal amount: 800 Balance is: $33.00

RPS2 (Rock Paper Scissors): RPS2_2:


import java.util.Scanner; public class RPS2_2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub RPSGame rps = new RPSGame(); RPSPlayer rpsOpponent = new RPSPlayer(); String name; int rounds; int playerThrow = 1; Scanner input = new Scanner(System.in); /* play RPS */ System.out.print("Enter your name: "); name = input.nextLine(); System.out.print("How many rounds? "); rounds = input.nextInt(); for (int i = 0; i < rounds; i++) { do { System.out.print("\nEnter your throw (ROCK=1," + "PAPER=2, SCISSORS=3): "); playerThrow = input.nextInt(); } while (!(playerThrow > 0 && playerThrow < 4)); rpsOpponent.makeThrow(playerThrow); rps.makeCompThrow(); rps.announceWinner(rpsOpponent.getThrow(), name); } rps.bigWinner(name); }

RPSGame:
import java.util.Random; public class RPSGame { public static final int ROCK = 1, PAPER = 2, SCISSORS = 3; private int compThrow; private int playerWins = 0, computerWins = 0;

/** * constructor * pre: none * post: RPSGame object created. Computer throw generated. */ public RPSGame() { Random rand = new Random(); compThrow = rand.nextInt(3) + 1; playerWins = 0; computerWins = 0; } // random int between 1 and 3

/** * Computer's throw is generated (ROCK, PAPER, or SCISSORS) * pre: none * post: Computer's throw has been made */ public void makeCompThrow() { Random rand = new Random(); compThrow = rand.nextInt(3) + 1; } // random int between 1 and 3

/** * Returns the computer's throw. * pre: none * post: Computer's throw has been returned. */ public int getCompThrow() { return(compThrow); }

/** * Determines the winner of the round. * pre: playerThrow is the integer 1, 2, or 3. * post: Displays a message indicating throws. Compares player's * throw to computer's throw and displays a message indicating * the winner. */ public void announceWinner(int playerThrow, String name) {

/* Inform player of throws */ System.out.print(name + " throws "); switch (playerThrow) { case ROCK: System.out.println("ROCK."); break; case PAPER: System.out.println("PAPER."); break; case SCISSORS: System.out.println("SCISSORS."); break; } System.out.print("Computer throws "); switch (compThrow) { case ROCK: System.out.println("ROCK."); break; case PAPER: System.out.println("PAPER."); break; case SCISSORS: System.out.println("SCISSORS."); break; } /* Determine and announce winner */ if (playerThrow == ROCK && compThrow == ROCK) { System.out.println("It's a draw!"); } else if (playerThrow == ROCK && compThrow == PAPER) { System.out.println("Computer wins!"); computerWins += 1; } else if (playerThrow == ROCK && compThrow == SCISSORS) { System.out.println(name + " wins!"); playerWins += 1; } if (playerThrow == PAPER && compThrow == ROCK) { System.out.println(name + " wins!"); playerWins += 1; } else if (playerThrow == PAPER && compThrow == PAPER) { System.out.println("It's a draw!"); } else if (playerThrow == PAPER && compThrow == SCISSORS) { System.out.println("ComputerWins"); computerWins += 1; } if (playerThrow == SCISSORS && compThrow == ROCK) { System.out.println("Computer wins!"); computerWins += 1; } else if (playerThrow == SCISSORS && compThrow == PAPER) { System.out.println(name + " wins!"); playerWins += 1; } else if (playerThrow == SCISSORS && compThrow == SCISSORS) { System.out.println("It's a draw!"); } }

/** * Displays the overall winner. * pre: none * post: COmputer and player wins compared and * an overall winner announced. */ public void bigWinner(String name) { System.out.print("\nComputer wins " + computerWins + ". "); System.out.println(name + " wins " + playerWins);

if (computerWins > playerWins) { System.out.println("Computer wins!"); } else if (playerWins > computerWins) { System.out.println(name + " wins!"); } else { System.out.println("It's a draw!"); } } }

RPSPlayer:
public class RPSPlayer { private int playerThrow; private String playerName;

// ROCK = 1, PAPER = 2, SCISSORS = 3

/** * constructor * pre: none * post: RPSPlayer object has been created. The player is given a default throw. */ public RPSPlayer() { playerThrow = 1; // Default throw }

/** * Sets the player's throw. * pre: newThrow is the integer 1, 2, or 3. * post: Players throw has been made. */ public void makeThrow(int newThrow) { playerThrow = newThrow; }

/** * Returns the player's throw * pre: none * post: Player's throw has been returned. */ public int getThrow() { return(playerThrow); }

/** * Sets the player's name * pre: none * post: The Player's name has been set. */ public void assignName(String name) { playerName = name; }

/** * Returns the player's name * pre: none * post: The Player's name has been returned */ public String getName() { return(playerName); } }

Console:
Enter your name: Josh How many rounds? 3 Enter your throw (ROCK=1,PAPER=2, SCISSORS=3): 3 Josh throws SCISSORS. Computer throws ROCK. Computer wins! Enter your throw (ROCK=1,PAPER=2, SCISSORS=3): 2 Josh throws PAPER. Computer throws PAPER. It's a draw! Enter your throw (ROCK=1,PAPER=2, SCISSORS=3): 1 Josh throws ROCK. Computer throws SCISSORS. Josh wins! Computer wins 1. Josh wins 1 It's a draw!

You might also like