/* Lesley Hogan * July 30th 2003 * Money.java * Cash Record Program for Exeter */ package net.bunnie.dos; import java.io.*; import net.bunnie.*; public class Money { public static void main (String[] args) { Echo.e("E X E T E R C A S H R E C O R D 2 0 0 3 --for Lesley Hogan\n\n\n"); // variables for file name, contact email, and error messages int lines = 6; int linesNums = 5; int keys = 5; String moneyFileNamePackage = "net\\bunnie\\dos\\"; String moneyFileName = moneyFileNamePackage + "Money.txt"; String email = "bunnie[@]bunnie.net"; String[] errorMessages = new String[9]; /* errors * [0] file not found * [1] not readable/writable/not 6 lines * [2] first 5 lines not numbers or negative * [3] last line numbers not numbers or negative * [4] last line missing : or | * [5] input : * [6] input keyletter * [7] input int * [8] write to file */ errorMessages[0] = moneyFileName + " cannot be found. Please restore this file to its proper location.\nIf you have lost or damaged this file, contact " + email + ".\nThis program cannot run without this file. Program will now exit.\n\n"; errorMessages[1] = ".\nPlease fix this and rerun the program. Program will now exit.\n\n"; errorMessages[2] = moneyFileName + " has corrupted numerical data. Please fix this and rerun the program.\nContact " + email + " if you need a copy of the original file.\nProgram will now exit.\n\n"; errorMessages[3] = moneyFileName + " has corrupted numerical data on the last line. Please fix\nthis and rerun the program. Contact " + email + " if you need\na copy of the original file. Program will now exit.\n\n"; errorMessages[4] = moneyFileName + " has corrupted data (missing colon/pipe) on the last line.\nPlease fix this and rerun the program. Contact " + email + " if\nyou need a copy of the original file. Program will now exit.\n\n"; errorMessages[5] = "Incorrect format for inputting items. "; errorMessages[6] = "Nonexistant keyletter. "; errorMessages[7] = "Incorrect format for inputting cost. "; errorMessages[8] = moneyFileName + " is corrupted. Please contact " + email + "\nNo data has been saved since the file is unavailable to be written to.\n"; // check if file exists and is readable and writable // also test for corrupted data try { // check for existentialism and permissions File w = new File(moneyFileName); if (w.isFile() == false) { throw new FileNotFoundException(); } if (w.canRead() == false) { IOException FileNotReadableException = new IOException("does not have readable permissions set"); throw FileNotReadableException; } if (w.canWrite() == false) { IOException FileNotWriteableException = new IOException("does not have writable permissions set"); throw FileNotWriteableException; } // test for corrupted data String[] moneyFileTest = ToFileArray.toFileArray(moneyFileName); IOException FileCorruptedException = new IOException("has corrupted data"); // if too many or too few lines if (moneyFileTest.length != lines) { throw FileCorruptedException; } // if first 5 lines are not numbers for (int i = 0; i < linesNums; i++) { moneyFileTest[i] = Regex.replace("\n", "", moneyFileTest[i]); // throws NumberFormatException moneyFileTest[i] = "" + Integer.parseInt(moneyFileTest[i], 10); // throw NumberFormatException if number is negative if (Integer.parseInt(moneyFileTest[i], 10) < 0) { throw new NumberFormatException(); } } // try-catch below for corruption of last line } // thrown if the file does not exist catch (FileNotFoundException fnfe) { Echo.e(errorMessages[0]); System.exit(0); } // thrown if file is not readable/writable/has too many/too few lines catch (IOException ioe) { Echo.e(moneyFileName + " " + ioe.getMessage() + errorMessages[1]); System.exit(0); } // thrown if first 5 lines are not numbers or negative numbers catch (NumberFormatException nfef) { Echo.e(errorMessages[2]); System.exit(0); } // declare/initialize variables double account; double wallet; double cost; int entered; int accepted = 0; int keyletter = 0; int unaccounted = 0; String enterItems = "y"; String blah = ""; String explodeItem; String[] explodedItem; // arrays to deal with file String[] moneyFile = ToFileArray.toFileArray(moneyFileName); String[] newMoneyFile = new String[lines]; String[] spentStrings = Explode.explode("|", moneyFile[5]); String[] spentSmallStrings = new String[2]; String[] acceptableInput = {"B", "G", "M", "T", "W"}; int[] moneyInts = new int[linesNums]; int[] compareInt = new int[2]; int[] spentMoney = new int[keys]; /* [0] = in account * [1] = in wallet * [2] = total money * [3] = total spent * [4] = unaccounted * [5] = cost record */ // replace ending \n's for (int i = 0; i < lines; i++) { moneyFile[i] = Regex.replace("\n", "", moneyFile[i]); newMoneyFile[i] = moneyFile[i]; } // try-catch for data on last line try { // explode :s and replace \n on end, subtract from unaccounted money for (int i = 0; i < keys; i++) { spentSmallStrings = Explode.explode(":", spentStrings[i]); // throws ArrayIndexOutOfBoundsException if no : or too few |s spentSmallStrings[1] = Regex.replace("\n", "", spentSmallStrings[1]); // throws NumberFormatException if number after : not a number spentMoney[i] = Integer.parseInt(spentSmallStrings[1], 10); // throw NumberFormatException if number is negative if (spentMoney[i] < 0) { throw new NumberFormatException(); } unaccounted -= spentMoney[i]; } } // thrown if number after : not a number or negative number catch (NumberFormatException nfent) { Echo.e(errorMessages[3]); System.exit(0); } // thrown if no : catch (ArrayIndexOutOfBoundsException aioobet) { Echo.e(errorMessages[4]); System.exit(0); } KeyboardReader r = new KeyboardReader(); // get input Echo.e("Please input how much money is left in your account: "); account = r.readDouble(); Echo.e("Please input how much money is left in your wallet: "); wallet = r.readDouble(); // round up input moneyInts[0] = (int) (Math.ceil(account)); moneyInts[1] = (int) (Math.ceil(wallet)); moneyInts[2] = moneyInts[0] + moneyInts[1]; compareInt[0] = Integer.parseInt(moneyFile[2], 10); compareInt[1] = Integer.parseInt(moneyFile[3], 10); // if money in wallet+account greater than last time // then money must have been added to wallet/account // ask for added money if (moneyInts[2] > compareInt[0] - compareInt[1]) { Echo.e("Please input how much money was added to your account/wallet since last run: "); blah = "" + (int) (Math.ceil(r.readDouble())); blah = Regex.replace("\n", "", blah); entered = Integer.parseInt(blah, 10); } // otherwise no money was added else { entered = 0; } // add added money to total and get how much money was spent moneyInts[2] = compareInt[0] + entered; moneyInts[3] = moneyInts[2] - (moneyInts[0] + moneyInts[1]); unaccounted += moneyInts[3]; // do loop for entering items do { Echo.e("\nPlease input cost of items bought with the following keycodes:\nB:(cost) for Bookstore\nG:(cost) for Grill\nM:(cost) for Mall\nT:(cost) for Trips\nW:(cost) for Walgreens\n\n"); explodeItem = r.readLine(); explodedItem = Explode.explode(":", explodeItem); // try-catch for input in form k:double // k is keyletter in array // double is how much spent at k try { explodedItem[0] = explodedItem[0].toUpperCase(); // throws ArrayOutOfBoundsException if input has no : // and NumberFormatException if double is not a double/int explodedItem[1] = "" + (int) (Math.ceil(Integer.parseInt(explodedItem[1], 10))); cost = Integer.parseInt(explodedItem[1], 10); // for loop to find out which keyletter it is accepted = 0; for (int i = 0; i < keys; i++) { if (explodedItem[0].equals(acceptableInput[i])) { accepted++; keyletter = i; } } // throws IOException if k is not a keyletter if (accepted != 1) { throw new IOException(); } // add cost to total and total spent, subtract from unaccounted spentMoney[keyletter] += cost; moneyInts[3] += cost; unaccounted -= cost; // for to join last line of file back together for (int i = 0; i < 5; i++) { spentStrings[i] = acceptableInput[i] + ":" + spentMoney[i]; } newMoneyFile[5] = ToFileArray.join("|", spentStrings); } // thrown if no : catch (ArrayIndexOutOfBoundsException aioobe) { Echo.e(errorMessages[5]); } // thrown if k is not a keyletter catch (IOException ioe) { Echo.e(errorMessages[6]); } // thrown if double is not a double/int catch (NumberFormatException nfe) { Echo.e(errorMessages[7]); } // do loop to continue, ask user Echo.e("Enter another item (Y/N): "); enterItems = r.readLine().toLowerCase(); } // only continues if user enters y or Y while (enterItems.equals("y")); // define strings from ints for new file contents newMoneyFile[0] = "" + moneyInts[0]; newMoneyFile[1] = "" + moneyInts[1]; newMoneyFile[2] = "" + moneyInts[2]; newMoneyFile[3] = "" + moneyInts[3]; newMoneyFile[4] = "" + unaccounted; /* newMoneyFile[5] already defined in try-catch above * if try-catch threw an exception, * newMoneyFile[5] is the same as moneyFile[5] * (the original line in the file) * as defined in first for loop after declaring variables */ // try-catch for file again, just to be safe =) try { // overwrite old contents with new content array FileWriter wt = new FileWriter(moneyFileName); for (int i = 0; i < lines - 1; i++) { wt.write(newMoneyFile[i] + "\r\n"); } // do not put a \n after last line wt.write(newMoneyFile[lines - 1]); wt.close(); } // catch if some weird thing goes wrong (never should) catch (IOException ioex) { Echo.e(errorMessages[8]); } } }