Created
May 7, 2014 06:23
-
-
Save justgage/c154e099cc818410a30d to your computer and use it in GitHub Desktop.
An even more improved wallet!
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.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.PrintWriter; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
class Recipt { | |
private double balence; | |
private String message; | |
private String date; | |
public double getBalence() { | |
return balence; | |
} | |
public Recipt(String line) { | |
String[] lineSplit = line.split("\t"); | |
balence = Math.round(Double.parseDouble(lineSplit[0]) * 2) / 2; | |
date = lineSplit[1]; | |
if (lineSplit.length == 3) { | |
message = lineSplit[2]; | |
} else { | |
message = "---"; | |
} | |
} | |
public Recipt(String m, double b) { | |
message = m; | |
balence = b; | |
Date now = new Date(); | |
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yy"); | |
date = f.format(now); | |
} | |
public void setMessage(String mess) { | |
message = mess; | |
} | |
public String toString() { | |
return String.format("%.2f",balence) + "\t" + date + "\t" + message; | |
} | |
} | |
public class Wallet { | |
private String name; | |
ArrayList<Recipt> ledger; | |
public Wallet(String n) { | |
name = n; | |
ledger = new ArrayList<Recipt>(); | |
} | |
public void addTrans(String m, Double b) { | |
ledger.add(new Recipt(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 Recipt(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 (Recipt 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 Recipt getLatest() { | |
return ledger.get(ledger.size() - 1); | |
} | |
public void display() { | |
if (balence() > 0) { | |
System.out.printf("%s has $%.0f\n",name, balence()); | |
} else if (balence() == 0) { | |
System.out.printf("%s is broke!\n", name); | |
} else { | |
System.out.printf("%s is $%.0f in debt!!!\n",name, 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