Public feed #599579

hmpf

public java v1 · immutable
Raw Edit
·published 2007-07-02 10:17 UTC
rendered paste body
package klausur;public class BTeil {	String name;	Integer preis;		public BTeil(String name, Integer preis){		this.name=name;		this.preis=preis;	}	public String getName() {		return name;	}	public Integer getPreis() {		return preis;	}		public String toString(){		return name + ": " + preis + "$";	}		public boolean equals(Object obj){ //funktioniert aber wird nicht von der map erkannt?										// Muss ich da nich irgent nen interface einladen?		if (!(obj instanceof BTeil))            return false;		return ((BTeil) obj).getName().equals(this.name);	}	}package klausur;import java.io.*;import java.util.*;public class Produkt {	Map<BTeil,List<String>> productTree = new HashMap<BTeil,List<String>>();	public void readTuples(String path){		try {			BufferedReader in = new BufferedReader(new FileReader(new File(path)));			String line;			while((line=in.readLine()) != null){				String[] trupel = line.split("[#]+");				for(int i= 1; i<trupel.length;i++){//					System.out.println(i+trupel[i]);					String[] splitted = trupel[i].split("[%$]");						if(splitted.length == 2) {							BTeil aBTeil = new BTeil(splitted[0].trim(),0);							addToTree(aBTeil,splitted[1].trim());						}						if(splitted.length == 3) {							BTeil aBTeil = new BTeil(splitted[0].trim(),Integer.valueOf(splitted[2].trim()));							addToTree(aBTeil,splitted[1].trim());						}					}			}			} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}		private void addToTree(BTeil aBTeil, String slave){		//if(!productTree.containsKey(aBTeil)){ // erkennt das equals in BTeil nicht?		if(!contains(aBTeil)){ 			//System.out.println(productTree.containsKey(aBTeil));			List<String> aArrayList = new ArrayList<String>();			aArrayList.add(slave);			productTree.put(aBTeil, aArrayList);		} else {			List<String> aArrayList = productTree.get(aBTeil);			aArrayList.add(slave);			productTree.put(aBTeil, aArrayList);		}	}		public void treeFromTuples(){			}		private boolean contains(BTeil aBTeil){ //will auch nicht so recht  		for(Map.Entry<BTeil, List<String>> each : productTree.entrySet()){			if(each.getKey().equals(aBTeil)){				return true;			} 		}		return false;	}		public void traverseTree(String name){		BTeil tempBTeil = new BTeil(name,0);		if(contains(tempBTeil)){			System.out.println(tempBTeil);			List<String> aList = productTree.get(tempBTeil);			for(int i = 0; i<aList.size(); i++){				this.traverseTree(aList.get(i));			}		}					}	public void traversTree(){		this.traverseTree("Produkt");	}		public static void main(String[] args) {		Produkt test = new Produkt();		test.readTuples("Z:\\Tuples-Prfg SS07.txt");		test.traverseTree("Produkt");	}		// und schon is die Zeit um :(	// Die Berechnung des der Preise hab ich nicht mehr geschaft	// irgentwie bekomm ich keine BTeile verglichen in der Map :(}