Results 1 to 5 of 5
- 04-09-2009, 02:55 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
[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; } }
- 04-09-2009, 10:22 PM #2
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...
- 04-10-2009, 04:01 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
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? :)
- 04-10-2009, 04:38 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
Best way to output text is using the File class and then BufferedOutputStream and FileOutputStream classes - go have a read of those!
- 04-10-2009, 05:35 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Need a solution to read and store data from a file
By sheetalnri in forum New To JavaReplies: 10Last Post: 09-30-2010, 06:43 AM -
help me to store data in a class
By neeraj.singh in forum New To JavaReplies: 2Last Post: 08-06-2009, 08:31 AM -
how to store string in text file
By santhosh_el in forum AWT / SwingReplies: 2Last Post: 04-03-2009, 06:21 AM -
Using arrayList to store data from one class to another.
By nickc88 in forum New To JavaReplies: 3Last Post: 03-28-2009, 05:02 AM -
Class to store Image+Text
By Gudradain in forum New To JavaReplies: 2Last Post: 11-23-2008, 08:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks