Created
May 3, 2014 01:50
-
-
Save justgage/d234ba42928c3556e1f1 to your computer and use it in GitHub Desktop.
A simple program to keep track of money in various wallets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a simple program that will keep track of how much money you have in various "Wallets". | |
----------------------------------------- | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.BufferedWriter; | |
import java.io.FileWriter; | |
import java.io.PrintWriter; | |
class Wallet { | |
private double balence; | |
private String name; | |
/** | |
* @Constructor | |
*/ | |
public Wallet(String inputName) { | |
name = inputName; | |
load(); | |
} | |
public double getBalence() { | |
return balence; | |
} | |
/** | |
* This will add (or subtract with negitive numbers) the current balence. | |
*/ | |
public double add(double num) { | |
return balence += num; | |
} | |
/** | |
* This will load the wallet based on it's name from a file. | |
*/ | |
private void load() { | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader(name + ".wal")); | |
String line; | |
balence = 0; | |
while((line = reader.readLine()) != null) { | |
balence += Double.parseDouble(line); | |
} | |
reader.close(); | |
} catch (java.io.FileNotFoundException e) { | |
balence = 0; | |
} catch (Exception e) { | |
System.out.println("ERROR: loading wallet file ->" + e); | |
} | |
} | |
/** | |
* this will write the the object to a file named after itself. | |
*/ | |
public void save() { | |
try { | |
PrintWriter fout = new PrintWriter( new BufferedWriter (new FileWriter(name + ".wal"))); | |
fout.print(balence); | |
fout.close(); | |
} catch (Exception e) { | |
System.out.println("ERROR:saving wallet file -> " + e); | |
} | |
} | |
/** | |
* This will display the balence of the wallet. | |
*/ | |
public void display() { | |
System.out.println(name + " has $ " + balence); | |
} | |
/** | |
This will test out the Wallet object | |
*/ | |
public static void main(String[] args) { | |
if (args.length >= 1) { | |
if (args[0].equals("-h")) { | |
System.out.println("Wallet: a simple command line tracker of expenses."); | |
System.out.println("\nHelp:"); | |
System.out.println("\t To view a wallet: \t$ Java Wallet myWallet"); | |
System.out.println("\t To add to a wallet: \t$ Java Wallet myWallet 10.00"); | |
System.out.println("\t To subtract from a wallet: \t$ Java Wallet myWallet -5.00"); | |
System.out.println("\n NOTE: new wallets will be created if they do not exist."); | |
return; | |
} | |
String name = args[0]; | |
Wallet mywallet = new Wallet(name); | |
if (args.length > 1) { | |
mywallet.add(Double.parseDouble(args[1])); | |
} | |
mywallet.display(); | |
mywallet.save(); | |
} else { | |
System.out.println("for help do $ java Wallet -h"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment