I completed this for a class project and was wondering if you guys could see any possible improvements for it.
I will attach the .java file and a .doc which explains how it should work. That is the actual assignment so I will explain how this program works.
You are given a list of numbers (cost centers) "xxxx xxxx" the first number is the son of the second number - if the father is a 0 the first number is the root. Then you are given expenses "xxxx xx xxxx" the first number is the cost center number which we explained earlier the second is the expense number (paper costs, utilities, salaries, etc) and the third number is the amount.
The program gets both of that input - using the first file it finds how many unique numbers it has and makes the array that big to start off with (the costcenter array) it uses an array because the nodes or costcenters may not be in any order so we may have a few trees before they all develop correctly.
Then it links the nodes together, then it adds the expenses to the correct node and all of the fathers of the nodes to make trans versing easier.
It then has 2 print outs of what it calculated. There are more than 100 lines of comments in this program so you guys should be able to figure out what things do - follow the input at the bottom and you should be able to get it all. Any questions please feel free to ask. This is in Java.
Originally Posted by ACNT.dat
Originally Posted by EXP.dat
Code:/* Output: Root: 0 Levels: 3 Nodes: 8 Cost Center Totals: 1012................ 15118 5654............ 879 3741............ 1253 1320............ 12986 3426........ 6862 4918.... 2909 8649.... 3953 2143........ 6124 Expense Code Summary: 1012 5654 3741 1320 12 2063 822 316 925 15 3427 0 0 3427 17 1069 0 265 804 21 166 57 0 109 24 672 0 672 0 34 5320 0 0 5320 56 746 0 0 746 74 1655 0 0 1655 1320 3426 2143 12 925 925 0 15 3427 3427 0 17 804 0 804 21 109 109 0 34 5320 0 5320 56 746 746 0 74 1655 1655 0 3426 4918 8649 12 925 925 0 15 3427 0 3427 21 109 35 74 56 746 294 452 74 1655 1655 0 */ import java.io.*; import java.util.ArrayList; class expenses { //Just a simple node with the name, amount, and the next public int name, amount; public expenses next; expenses() { name = amount = 0; next = null; } } class costcenters { /* Like we went over in class This has the name, next brother, first son This also has the expense linked list created above */ public int name, totalexps; public costcenters nbro, fson; public expenses exps; costcenters() { name = 0; nbro = fson = null; exps = new expenses(); } } class modifier { /* This is my modifier class which we will use to modify all my nodes and lists all my data here is set to private so that if somebody else wants to create nodes using our previously declared nodes they still can but they cannot edit the ones here =) */ private costcenters[] tentwo; private costcenters temp; private int pointer, i, temp1, temp2, root, deep; boolean found; modifier(String j) { //Constructor used to define variables used this.pointer = this.i = this.temp1 = this.temp2 = this.root = this.deep = 0; this.tentwo = new costcenters[loaddata(j, 3)]; for(int i = 0;i<tentwo.length;i++) { this.tentwo[i] = new costcenters(); this.tentwo[i].name = 0; } } /* Private Classes: */ private void linktwo(costcenters father, costcenters son) { //links the father to the son if(father.name != son.name) { if(father.fson == null) { //no sons just link and your done this.deep++; father.fson = son; } else { //find the last son and link this.temp = father.fson; while(this.temp.nbro != null) { this.temp = this.temp.nbro; } this.temp.nbro = son; } } } private void addCenter(int costcenter, int father) { //adds the "costcenter" to the binary tree this.i = this.temp1 = this.temp2 = -1; this.found = true; //end loop if both nodes or found or we reach the end while((this.found) && (i<pointer)) { this.i++; if(this.tentwo[this.i].name == costcenter) { this.temp1 = this.i; } else if(this.tentwo[this.i].name == father) { this.temp2 = this.i; } //one line statement saying that both are found or not this.found = ((this.temp1 != -1) && (this.temp2 != -1)) ? false : true; } if(this.temp1 == -1) { //no son node need to create this.temp1 = this.pointer; this.tentwo[this.temp1].name = costcenter; this.pointer++; } if(father==0) { //if the node already exists and is the root say so this.root = this.temp1; } else { if(this.temp2 == -1) { //no father need to create this.temp2 = this.pointer; this.tentwo[this.temp2].name = father; this.pointer++; } //private one that links two together linktwo(this.tentwo[this.temp2], this.tentwo[this.temp1]); } } private boolean addExpense(costcenters root, int costcenter, int name, int amount) { /* Loops through until it finds the node recursively then finds the expenses then uses 'addtoit' to add it on there */ costcenters btemp; if(root.name != costcenter) { if(root.fson != null) { btemp = root.fson; do { if(addExpense(btemp, costcenter, name, amount)) { root.totalexps += amount; addtoit(root.exps, name, amount); return true; } btemp = btemp.nbro; } while(btemp != null); } } else { root.totalexps += amount; addtoit(root.exps, name, amount); return true; } return false; } private void addtoit(expenses exps, int name, int amount) { //if name is the expense name add to the amount //loop through until null or until you find one bigger then add this.found = true; //not used because loop is going to end later boolean skip = true; boolean count = true; do { if(exps.name == name) { exps.amount += amount; this.found = false; } else if(count) { count = false; if(exps.name > name) { //if first node is smaller then you gotta reput it expenses b = new expenses(); b.name = exps.name; b.amount = exps.amount; b.next = exps.next; exps.name = name; exps.amount = amount; exps.next = b; skip = false; } } else { if(exps.name < name && exps.next.name > name) { //if it fits in the middle then link it in expenses b = new expenses(); b.name = name; b.amount = amount; b.next = exps.next; exps.next = b; skip = false; } exps = exps.next; } } while((this.found) && (exps.next != null) && (skip)); if(this.found && skip){ //did not update node if(exps.name == 0) { //no nodes are present exps.name = name; exps.amount = amount; } else { //nodes present exps.next = new expenses(); exps.next.name = name; exps.next.amount = amount; } } } private String visit(costcenters i, String add) { //visit costcenter and format it correctly expenses j = i.exps; if(add == "") { add = "\t"+i.name+"\n\n"; while(j != null) { add += j.name+"\t"+j.amount+"\n"; j = j.next; } } else { String[] ads = add.split("\n"); add = ads[0]+"\t"+i.name+"\n\n"; int b = 2; while(b < ads.length) { add += ads[b]+"\t"+findexp(i.exps, ads[b].substring(0,2))+"\n"; b++; } } return add; } private int findexp(expenses exp, String i) { //just finds the expense with the number int b = Integer.parseInt(i); while(exp != null) { if(exp.name == b) { return exp.amount; } exp = exp.next; } return 0; } private String printadv(costcenters i) { //workhorse //needs to be a local variable to not mess up my recursion String first = ""; String next = ""; if(i.fson != null) { first = visit(i, first); i = i.fson; while(i != null) { next += printadv(i); first = visit(i, first); i = i.nbro; } return first+"\n\n"+next; } return ""; } private String printbylevel(costcenters i, int level) { //Recursive underpaid workhorse //the second String first, second; first = second = ""; if(i.fson != null) { first = printbylevel(i.fson, level+1); } if(i.nbro != null) { //dont increase the level since its a binary tree //and your going to the "brother" which is right not down second = printbylevel(i.nbro, level); } String top = addspacing(level, " ", "")+ i.name+addspacing((this.deep-level)+1, "....", " ")+ i.totalexps +"\n"+ first + second; return top; } private String addspacing(int amount, String type, String trail) { //the "amount" of "type" do you want with a "trail" at the end this.i = 0; String spa = ""; while(this.i < amount) { spa += type; this.i++; } return spa + trail; } /* Public Classes: */ public int loaddata(String file, int type) { /* This class does 3 things: Gets size of array from first input file Adds them to the system Adds expenses */ String line; String[] t; BufferedReader f; int ret = 0; try { f = new BufferedReader(new FileReader(file)); if (type==3) { //get amounts //create expense list and then add them all up ArrayList<String> al = new ArrayList<String>(); while ((line = f.readLine()) != null) { t = line.split("\\s+"); al.remove(t[0]); al.add(t[0]); al.remove(t[1]); al.add(t[1]); } //sets the size of it based on the lines above ret = al.size(); } else if(type==2) { //adds expenses while ((line = f.readLine()) != null) { t = line.split("\\s+"); addExpense(this.tentwo[this.root],Integer.parseInt(t[0]), Integer.parseInt(t[1]), Integer.parseInt(t[2])); } } else { //adds to addCenter while ((line = f.readLine()) != null) { t = line.split("\\s+"); addCenter(Integer.parseInt(t[0]), Integer.parseInt(t[1])); } } f.close(); } catch (FileNotFoundException e) { System.out.println("Unable to open file: "+file+" - " + e.getMessage()); } catch (IOException e) { System.out.println("IO exception ("+file+"): " + e.getMessage()); } return ret; } public void printall() { //pretty store lady System.out.println("Root: " + this.root + " Levels: " + this.deep+" Nodes: "+ this.pointer+"\n\n"); String first = "Cost Center Totals:\n\n"+printbylevel(this.tentwo[this.root], 0); String second = "Expense Code Summary:\n\n"+printadv(this.tentwo[this.root]); System.out.print(first+"\n"+second); } } public class prog10 { public static void main(String[] args) { //the actual work the new programmer has to do is very minimal String acnt = "E:\\ACNT.dat"; String exps = "E:\\EXP.dat"; modifier tentwo = new modifier(acnt); tentwo.loaddata(acnt, 1); tentwo.loaddata(exps, 2); tentwo.printall(); } }
Last edited by BlaineSch; 04-24-2009 at 02:49 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks