Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class CheckAccount {
- //Constructor
- CheckAccount(String info){
- String[] passedInfo = info.split(" ");
- this.fileLoc = passedInfo[1];
- this.accNum = passedInfo[2];
- this.fName = passedInfo[3];
- this.lName = passedInfo[4];
- this.bal = Double.parseDouble(passedInfo[5]);
- File file = new File(fileLoc);
- try {
- int counter = 0;
- BufferedReader in = new BufferedReader(new FileReader(file));
- try {
- String line = null;
- while ((line = in.readLine()) != null && counter <= 30)
- {
- transactions[counter] = line;
- counter++;
- }
- }
- finally {
- in.close();
- }
- }
- catch (IOException ex){
- System.out.println("File Read Error for: " + fileLoc);
- }
- /*for (int c = 0; c < transactions.length; c++){
- System.out.println(transactions[c]);
- } */
- run();
- }
- private void run(){
- System.out.println("Account: " + accNum + " Name: " + fName + " " + lName);
- System.out.println(padLeft("Date", 13) + padLeft("Description", 40) + padRight("Amount", 8) + " Balance");
- System.out.println(padLeft("", 13) + padLeft("Your balance at beginning of period", 40) + padLeft("", 8) + padRight("" + bal, 10));
- double[][] checksCleared = new double[30][2];
- int cc = 0;
- double checkTotal = 0, depositTotal = 0, servTotal = 0;
- for (int trans = 0; transactions[trans] != null; trans++){
- String[] oneTrans = transactions[trans].split(" ");
- boolean isDeposit = oneTrans[0].equals("DPT");
- String date = oneTrans[1] + "-" + oneTrans[2] + "-" + oneTrans[3];
- double amt = 0;
- if(isDeposit){
- amt = Double.parseDouble(oneTrans[4]);
- bal += amt;
- depositTotal += amt;
- System.out.println(padLeft(date, 13) + padLeft("Deposit", 40) + padRight("" + Math.round(amt*100)/100 + fixdp("." + Math.abs(Math.round(amt*100)%100)), 8) + padRight("" + Math.round(bal*100)/100 + fixdp("." + Math.abs(Math.round(bal*100)%100)), 10));
- }
- else{
- amt = Double.parseDouble(oneTrans[5]);
- int checkNum = Integer.parseInt(oneTrans[4]);
- String desc = oneTrans[6];
- checksCleared[cc][0] = checkNum;
- checksCleared[cc][1] = amt;
- cc++;
- bal -= amt;
- checkTotal += amt;
- System.out.println(padLeft(date, 13) + padLeft(desc.trim() + ", #" + checkNum, 40) + padRight("" + Math.round(amt*100)/100 + fixdp("." + Math.abs(Math.round(amt*100)%100)), 8) + padRight("" + Math.round(bal*100)/100 + fixdp("." + Math.abs(Math.round(bal*100)%100)), 10));
- if(bal < 0){
- bal -= 20;
- servTotal += 20;
- System.out.println(padLeft(date, 13) + padLeft("Service Fee", 40) + padRight("20.00", 8) + padRight("" + Math.round(bal*100)/100 + "." + Math.abs(Math.round(bal*100)%100), 10));
- }
- }
- }
- System.out.println("\n" + padLeft("Total checks:", 20) + padRight("" + Math.round(checkTotal*100)/100 + fixdp("." + Math.abs(Math.round(checkTotal*100)%100)), 10));
- System.out.println(padLeft("Total deposits:", 20) + padRight("" + Math.round(depositTotal*100)/100 + fixdp("." + Math.abs(Math.round(depositTotal*100)%100)), 10));
- System.out.println(padLeft("Total service fees:", 20) + padRight("" + Math.round(servTotal*100)/100 + fixdp("." + Math.abs(Math.round(servTotal*100)%100)), 10) + "\n");
- quickSort(checksCleared);
- System.out.println("\t\tChecks Cleared");
- for (int c = 0; c < checksCleared.length; c++){
- if ((int) checksCleared[c][0] != 0){
- String ccnum = "#" + (int) checksCleared[c][0];;
- if (c != 0 && checksCleared[c-1][0] != checksCleared[c][0] - 1 && checksCleared[c-1][0] != 0)
- ccnum += "*";
- System.out.println("\t\t" + padLeft(ccnum, 5) + padRight("" + Math.round(checksCleared[c][1]*100)/100 + fixdp("." + Math.abs(Math.round(checksCleared[c][1]*100)%100)), 6));
- }
- }
- }
- private void quickSort(double ccarray[][]){
- quickSort(ccarray, 0, ccarray.length - 1);
- }
- public void quickSort(double ccarray[][], int start, int end){
- int i = start;
- int j = end;
- if (end - start >= 1){
- double pivot = ccarray[start][0];
- while (j > i){
- while (ccarray[i][0] <= pivot && i <= end && j > i)
- i++;
- while (ccarray[j][0] > pivot && j >= start && j >= i)
- j--;
- if (j > i)
- swap(ccarray, i, j);
- }
- swap(ccarray, start, j);
- quickSort(ccarray, start, j - 1);
- quickSort(ccarray, j + 1, end);
- }
- else{
- return;
- }
- }
- public void swap(double ccarray[][], int loc1, int loc2){
- double[] temp = ccarray[loc1];
- ccarray[loc1] = ccarray[loc2];
- ccarray[loc2] = temp;
- }
- private String padLeft(String str, int length){
- int desL = length - str.length();
- for (int c = 0; c < desL; c++)
- str = str + " ";
- return str;
- }
- private String padRight(String str, int length){
- int desL = length - str.length();
- for (int c = 0; c < desL; c++)
- str = " " + str;
- return str;
- }
- private String fixdp(String toFix){
- if (toFix.endsWith(".0"))
- toFix += "0";
- if (toFix.length() == 2)
- toFix = ".0" + toFix.substring(1);
- return toFix;
- }
- //Instance variables
- String fileLoc = null, accNum = null, fName = null, lName = null;
- double bal = 0;
- String transactions[] = new String[30];
- }
Add Comment
Please, Sign In to add comment