Results 1 to 5 of 5
  1. #1
    happytrails is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default [SOLVED] CoinCounter Class - How do I store data in text file?

    Hello everyone, excuse my nubness, as I am still a nub in programming, and I have a few questions. I'm just having some fun learning programming, watched a youtube video on a tutorial for java programming which inspired me to make my own class called CoinCounterEnhanced.

    Basically I made this class be able to do a number of different methods all relating to adding/subtracting quarters, dimes, nickles, and pennies. In addition to that, the program is also able to return the amount of coins, value of coins, and thats pretty much all.

    Now, since I took a long time to make this and it was fun! I'm looking for ways to maybe add on to what I have? I was thinking maybe of somehow being able to store the data I create in a text file or something like that. Say I make a CoinCounterEnhanced object named billyBob. I would like to store that data in a notepad .txt or maybe I want to yea maybe I want to write what I want to add in quarters and stuff in a text file then run the program on it? I dunno, anyone's opinion would be great! Anything in addition to what I could do now definitely helps me learn/more fun! Thanks in advance, oh, and also, If you see any ways I could improve my code or w/e please tell me, I need the help/criticism!
    Thanks,
    -Adam

    Java Code:
    /* My CoinCounterEnhanced class for counting coins! :)
     * 
     */
    package coinsEnhanced;
    
    /**
     * @author Adam
     * This program creates a CoinsCounterEnhanced object that allows the user to keep count of
     * coins.  This includes quarters, dimes, nickels, and pennies.
     */
    public class CoinsCounterEnhanced {
    	private double quarters;
    	private double dimes;
    	private double nickels;
    	private double pennies;
    	private double coins;
    	/**
    	 * Constructor for the CoinsCounterEnhanced object
    	 * It will start with no parameters and a value of 0 quarters, dimes, nickels,
    	 * pennies, and coins.
    	 */
    	public CoinsCounterEnhanced(){
    		quarters = 0;
    		dimes = 0;
    		nickels = 0;
    		pennies = 0;
    		coins = 0;
    	}
    	
    	/**
    	 * Adds quarters to the CoinsCounterEnhanced object.
    	 * @param amount The amount to add.
    	 */
    	public void addQuarters(int amount){
    		quarters = quarters + amount;
    	}
    	/**
    	 * Subtracts quarters from the CoinsCounterEnhanced object.
    	 * @param amount The amount to subtract.
    	 */
    	public void subtractQuarters(int amount){
    		quarters = quarters - amount;
    	}
    	/**
    	 * Adds dimes to the CoinsCounterEnhanced object.
    	 * @param amount The amount to add.
    	 */
    	public void addDimes(int amount){
    		dimes = dimes + amount;
    	}
    	/**
    	 * Subtracts dimes from the CoinsCounterEnhanced object.
    	 * @param amount The amount to subtract.
    	 */
    	public void subtractDimes(int amount){
    		dimes = dimes - amount;
    	}
    	/**
    	 * Adds nickels(aka nickles) to the CoinsCounterEnhanced object.
    	 * @param amount The amount to add.
    	 */
    	public void addNickels(int amount){
    		nickels = nickels + amount;
    	}
    	/**
    	 * Subtracts nickels(aka nickles) from the CoinsCounterEnhanced object.
    	 * @param amount The amount to subtract.
    	 */
    	public void subtractNickels(int amount){
    		nickels = nickels - amount;
    	}
    	/**
    	 * Adds pennies to the CoinsCounterEnhanced object.
    	 * @param amount the amount to add
    	 */
    	public void addPennies(int amount){
    		pennies = pennies + amount;
    	}
    	/**
    	 * Subtracts pennies from the CoinsCounterEnhanced object.
    	 * @param amount the amount to subtract
    	 */
    	public void subtractPennies(int amount){
    		pennies = pennies - amount;
    	}
    	/**
    	 * Returns the amount of quarters in the CoinsCounterEnhanced object.
    	 * @return
    	 */
    	public double getQuarters(){
    		return quarters;
    	}
    	/**
    	 * Returns the amount of dimes in the CoinsCounterEnhanced object.
    	 * @return
    	 */
    	public double getDimes(){
    		return dimes;
    	}
    	/**
    	 * Returns the amount of nickels(aka nickles) in the CoinsCounterEnhanced object.
    	 * @return
    	 */
    	public double getNickels(){
    		return nickels;
    	}
    	/**
    	 * Returns the amount of pennies in the CoinsCounterEnhanced object.
    	 * @return
    	 */
    	public double getPennies(){
    		return pennies;
    	}
    	/**
    	 * Returns the value, in cents, of quarters.
    	 * @return
    	 */
    	public double getQuartersValue(){
    		quarters = quarters * 0.25;
    		return quarters;
    	}
    	/**
    	 * Returns the value, in cents, of dimes.
    	 * @return
    	 */
    	public double getDimesValue(){
    		dimes = dimes * 0.10;
    		return dimes;
    	}
    	/**
    	 * Returns the value, in cents, of nickels(aka nickles).
    	 * @return
    	 */
    	public double getNickelsValue(){
    		nickels = nickels * 0.05;
    		return nickels;
    	}
    	/**
    	 * Returns the value, in cents, of pennies.
    	 * @return
    	 */
    	public double getPenniesValue(){
    		pennies = pennies * 0.01;
    		return pennies;
    	}
    	/**
    	 * Returns the amount of all coins combined.
    	 * @return
    	 */
    	public double getNumberOfCoins(){
    		coins = quarters + dimes + nickels + pennies;
    		System.out.println("The number of coins you have is: ");
    		return coins;
    	}
    	/**
    	 * Returns the value, in cents, of all the coins combined.
    	 * @return
    	 */
    	public double getCoinsValue(){
    		coins =         (quarters * 0.25) + 
    						(dimes    * 0.10) +
    						(nickels  * 0.05) +
    						(pennies  * 0.01);
    		System.out.println("The amount of money your coins add up to is: ");
    		return coins;
    	}
    }

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    There a couple of different ways that you can read and write to files... One that I picked up pretty easily is RandomAccessFile... If you Google it there are a lot of good examples...
    Who Cares... As Long As It Works...

  3. #3
    happytrails is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Ok great, thanks for your help and info mark, I'm definitely going to look in to that. Thanks again, and any other suggestions anyone might else have to improve my class? :)

  4. #4
    renegadeandy is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Best way to output text is using the File class and then BufferedOutputStream and FileOutputStream classes - go have a read of those!

  5. #5
    happytrails is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Ok great, thanks renegadeandy very much for that info, I will definitely take a look at that!

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 07:43 AM
  2. help me to store data in a class
    By neeraj.singh in forum New To Java
    Replies: 2
    Last Post: 08-06-2009, 09:31 AM
  3. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 07:21 AM
  4. Replies: 3
    Last Post: 03-28-2009, 06:02 AM
  5. Class to store Image+Text
    By Gudradain in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •