Coin Class question

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2400 Views - Last Post: 08 March 2013 - 09:48 PM Rate Topic: -----

#31 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Coin Class question

Posted 08 March 2013 - 09:41 PM

get the coins value and return it so something like

            if (userCoin.getCoinValue() == generatedCoins.get(1).getCoinValue())
            {
               System.out.println("match");
            }
            else
            {
               System.out.println("nope");
            }



should work.

Forgot to add .equals will not work on a primitive type so calling.
            if (userCoin.getCoinValue().equals(generatedCoins.get(1).getCoinValue()))
            {
               System.out.println("match");
            }
            else
            {
               System.out.println("nope");
            }



will not work, hope this helps.

This post has been edited by floppyspace: 08 March 2013 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

#32 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 08 March 2013 - 09:48 PM

wow, I completely overlooked that. Thank you for the help +1 for the help =D

MAIN
package programmingunit5;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 *
 * @author Patrick
 */
class ProgrammingUnit5 {
    //////////////////////////////////////////////////////////////////////////
    /**
     * @array coinArray, these are the test objects (coins)
     */
    private static final Coin[] coinArray = 
    {
        new Coin("Penny", 1),
        new Coin("Nickel", 5),
        new Coin("Dime", 10),
        new Coin("Quarter", 25),
        new Coin("Half-Dollar", 50),
        new Coin("Gold-Dollar", 100)
    };
    /**
     * @arary coinCloneArray, these are the cloned test objects (coins)
     */
    private static Coin[] coinCloneArray = new Coin[6];
    static Scanner console = new Scanner(System.in);
    private static boolean isValidUserInput = false;
    private static int userSelection1 = 0;
    private static int userSelection2 = 0;
    //////////////////////////////////////////////////////////////////////////
    public static void main(String[] args){
        while(true){
            printOriginalCoinValues();
            cloneOriginalCoinValues();
            printClonedCoinValues();
            userSelection1 = getUserInput1();
            userSelection2 = getUserInput2();
            userSelection1 = validateUserInput1(userSelection1);
            userSelection2 = validateUserInput2(userSelection2);
            compareCoinValues(userSelection1, userSelection2);
            try {
                Thread.sleep(5000);
            } catch(InterruptedException e) {
                System.err.println(e);
            } 
        }
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * @forloop
     * for each index in coinArray
     *      print out the value at coinArray[x] with overridden toString()
     *      also print "[x]" so that user can use this value to compare two 
     *      coin types
     * end forloop
     */
    private static void printOriginalCoinValues() {
        System.out.println("******COIN VALUES******");
         for(int x = 0; x < coinArray.length;x++){
            System.out.println(coinArray[x].toString() + " [" + x + "] ");
         }
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * @forloop
     * for each index in coinArray
     *      try to{
     *          create a copy of our coinArray by assigning into 
     *          coinCloneArray[x] while remembering to type cast them to
     *          Coin type
     *      catch CloneNotSupportedException (almost never actually happens)
     *          print out the error with a .err
     * end forloop
     */
    private static void cloneOriginalCoinValues() {
        for(int x = 0; x < coinArray.length;x++){
            try{
                coinCloneArray[x] = (Coin)coinArray[x].clone();
            }catch(CloneNotSupportedException e){
                System.err.println(e);
            }
        }
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * @forloop
     * for each index in coinCloneArray
     *      print out the value at coinCloneArray[x] with overridden
     *      toString() also print "[x]" so that user can compare any two
     *      items
     * end forloop
     */
    private static void printClonedCoinValues() {
        System.out.println("*****COPIED VALUES*****");
        for(int x = 0; x < coinCloneArray.length; x++){
            System.out.println(coinCloneArray[x].toString() + " [" + x + 
                    "] ");
        }
        System.out.println("***********************");
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * @varibale userSelection1 this value will store the user input and on
     * each iteration this value is reset to zero
     * 
     * @trycatch
     * try to{
     *      get the userSelection1 variable set with custom value; however
     * catch{
     *      InputMismatchException or NumberFormat or in worst case scenario
     *      catch an unknown error; 
     *          for all catches;
     *              1. printout the exception
     *              2. obtain a new value (that's valid)
     * 
     * @return userSelection1, this is the custom value returned, as long
     * as the userSelection1 passed the try/catch block
     */
    private static int getUserInput1() throws InputMismatchException, 
            NumberFormatException{
        userSelection1 = 0;
        try{
            System.out.println("Please enter [coin] choice from original "
                    + "coin "
                    + "values to compare with copied coins");
            userSelection1 = console.nextInt();
        }catch(InputMismatchException | NumberFormatException e){
            System.err.println(e);
            System.out.println("Please enter valid option");
            console.nextLine();
            userSelection1 = console.nextInt();
        }catch(Exception e){
            System.err.println(e);
            System.out.println("Please enter valid option");
            console.nextLine();
            userSelection1 = console.nextInt();
        }
        return userSelection1;
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * @varibale userSelection2 this value will store the user input and on
     * each iteration this value is reset to zero
     * 
     * @trycatch
     * try to{
     *      get the userSelection2 variable set with custom value; however
     * catch{
     *      InputMismatchException or NumberFormat or in worst case scenario
     *      catch an unknown error; 
     *          for all catches;
     *              1. printout the exception
     *              2. obtain a new value (that's valid)
     * 
     * @return userSelection2, this is the custom value returned, as long
     * as the userSelection1 passed the try/catch block
     */
    private static int getUserInput2() throws InputMismatchException, 
            NumberFormatException{
        userSelection2 = 0;
        try{
            System.out.println("Please enter [coin] choice from copied "
                    + "coin "
                    + "values to compare with original coins");
            userSelection2 = console.nextInt();
        }catch(InputMismatchException | NumberFormatException e){
            System.err.println(e);
            System.out.println("Please enter valid option");
            console.nextLine();
            userSelection2 = console.nextInt();
        }catch(Exception e){
            System.err.println(e);
            System.out.println("Please enter valid option");
            console.nextLine();
            userSelection2 = console.nextInt();
        }
        return userSelection2;
    }
    
    /**
     * 
     * @param userSelection1 this is the first value that user entered
     * @param userSelection2 this is the second value that user entered
     * 
     * @ifelse
     * if(coinCloneArray[userSelection2]'s value is equivalent to the
     *      original coinArray[userSelection1]'s getCoinValue)
     *      then{
     *          print that they are equivalent
     *      else{
     *          print that they are not equivalent
     * end ifelse
     */
    private static void compareCoinValues(int userSelection1, 
            int userSelection2) {
        if(coinCloneArray[userSelection2].getCoinValue() == 
                coinArray[userSelection1].getCoinValue()){
            System.out.println("\t***cloned coin and original coin match"
                    + "***");
        }
        else{
            System.out.println("\t***cloned coin and original don't match"
                    + "***");
        }
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * 
     * @param userSelection1, this method will confirm that userSelection1 is 
     * in the correct bounds
     * @return userSelection1 or
     * @return2 0 (only if userSelection1 less than -1 or greater than 6)
     */
    private static int validateUserInput1(int userSelection1) {
        if(userSelection1 > -1 && userSelection1 < 7){
            return userSelection1;
        }
        System.out.println("defaulted to zero");
        return 0; //default to penny
    }
    //////////////////////////////////////////////////////////////////////////
    /**
     * 
     * @param userSelection2, this method will confirm that userSelection2 is 
     * in the correct bounds
     * @return userSelection2 or
     * @return2 0 (only if userSelection2 less than -1 or greater than 6)
     */
    private static int validateUserInput2(int userSelection2) {
        if(userSelection2 > -1 && userSelection2 < 7){
            return userSelection2;
        }
        System.out.println("defaulted to zero");
        return 0; // default to penny
    }
    //////////////////////////////////////////////////////////////////////////
} // end



COIN
package programmingunit5;

import java.text.NumberFormat;

/**
 *
 * @author Patrick
 */
class Coin implements Cloneable{
    private String coinN;
    private double coinV;
    
    public Coin(String coinName, double coinValue) {
        coinN = coinName;
        coinV = coinValue;
    }
    public String getCoinName(){
        return coinN;
    }
    public double getCoinValue(){
        return coinV;
    }
    @Override
    public String toString(){
        return (coinN + " " + NumberFormat.getCurrencyInstance().format(coinV/100));
    }
    @Override
    public Object clone() throws CloneNotSupportedException{
        Coin clonedCoin = (Coin)super.clone();
        return clonedCoin;
    }
    public Coin(Coin other){
        this.coinN = other.coinN;
        this.coinV = other.coinV;
    }

    public void copy(Coin aCopy) {
        coinN = aCopy.getCoinName();
        coinV = aCopy.getCoinValue();
    }
}



Thank you everyone that helped me =D

This post has been edited by k3y: 09 March 2013 - 05:59 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3