Created
May 6, 2014 06:17
-
-
Save justgage/c4b523d4165f95911c35 to your computer and use it in GitHub Desktop.
wallet3.java
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
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; | |
import java.util.ArrayList; | |
class Transaction { | |
private double balence; | |
private String message; | |
public double getBalence() { | |
return balence; | |
} | |
public Transaction(String line) { | |
String[] lineSplit = line.split("\t"); | |
balence = Math.round(Double.parseDouble(lineSplit[0]) * 2) / 2; | |
if (lineSplit.length == 2) { | |
message = lineSplit[1]; | |
} else { | |
message = ""; | |
} | |
} | |
public Transaction(String m, double b) { | |
message = m; | |
balence = b; | |
} | |
public void setMessage(String mess) { | |
message = mess; | |
} | |
public String toString() { | |
return String.format("%.2g",balence) + "\t" + message; | |
} | |
} | |
public class Wallet { | |
private String name; | |
ArrayList<Transaction> ledger; | |
public Wallet(String n) { | |
name = n; | |
ledger = new ArrayList<Transaction>(); | |
} | |
public void addTrans(String m, Double b) { | |
ledger.add(new Transaction(m,b)); | |
} | |
public void displayLog(int amount) { | |
if (amount >= ledger.size()) { | |
amount = ledger.size() - 1; | |
} | |
for(int i = amount; i >= 0; i--) { | |
System.out.println(" $" + ledger.get(i)); | |
} | |
} | |
/** | |
* This will load the wallet based on it's name from a file. | |
*/ | |
private void load() { | |
String line; | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader("." + name + ".wal")); | |
while((line = reader.readLine()) != null) { | |
ledger.add(new Transaction(line)); | |
} | |
reader.close(); | |
} catch (java.io.FileNotFoundException e) { | |
// nothing right now because it will be created in save | |
} catch (java.io.IOException e) { | |
System.out.println("ERROR: error reading file! ->" + e); | |
} | |
} | |
public double balence() { | |
Double bal = 0.0; | |
for (Transaction t : ledger) { | |
bal += t.getBalence(); | |
} | |
return bal; | |
} | |
/** | |
* 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", true))); | |
// note: the true above means that we append to the file | |
fout.print(getLatest() + "\n"); | |
fout.close(); | |
} catch (Exception e) { | |
System.out.println("ERROR:saving wallet file -> " + e); | |
} | |
} | |
public Transaction getLatest() { | |
return ledger.get(ledger.size() - 1); | |
} | |
public void display() { | |
System.out.println(name + " has $ " + balence()); | |
} | |
/** | |
This will test out the Wallet object | |
*/ | |
public static void main(String[] args) { | |
// if it includes a first argument (usually wallet name) | |
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; | |
} | |
Wallet mywallet = new Wallet(args[0]); | |
mywallet.load(); | |
// if includes money | |
if (args.length > 1) { | |
String mess = ""; | |
// if it includes comment | |
if (args.length > 2) { | |
for(int i = 2; i < args.length; i++) { | |
mess += args[i] + " "; | |
} | |
} | |
mywallet.addTrans(mess, Double.parseDouble(args[1])); | |
mywallet.save(); | |
mywallet.display(); | |
} else { | |
mywallet.display(); | |
mywallet.displayLog(10); | |
} | |
} else { | |
System.out.println("ERROR: no wallet name!\n\tfor help do $ java Wallet -h"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment