Umieszczenie danych w kilku mapach

0

Witam,
mam problem z umieszczenie danych w taki sposób, żeby te dane:

c00004;Nowak Anna;banany;4;50
c00003;Kowalski Jan;mleko;4;5
c00001;Kowalski Jan;mleko;4;10

Rozłożyć w ten sposób:

  1. pierwsze dwa poszły do klasy Customer (w odpowiednich miejscach w mapie)
  2. reszta poszły do klasy Purchase (w odpowiednich miejscach w mapie)

Napisałem na razie coś takiego:

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

class Customer {
	
	Map<String, String> customer = new HashMap<String, String>();
	
	String identifier;
	String name;
	
	public Customer(String identifier, String name) {
		this.identifier = identifier;
		this.name = name;
		customer.put(identifier, name);
	}
	
	@Override
	public String toString() {
		return identifier + " " + name;
		
	}
	
}

class Purchase {
	
	class Price {
		
		Map<Integer, Double> mapPriceAndHowMuch = new HashMap<Integer, Double>();
		
		public Price(int howMuch, double price) {
			mapPriceAndHowMuch.put(howMuch, price);
		}
		
		
		
	}
	
	HashMap<String, Price> nameOfProductAndTotalPrice = new HashMap<String, Price>();
	
	String product;
	int howMuch;
	double price;
	
	public Purchase(String product, String howMuch, String price) {
		this.product = product;
		this.howMuch = Integer.parseInt(howMuch);
		this.price = Double.parseDouble(price);
		Price priceAndHowMuch = new Price(this.howMuch, this.price);
		nameOfProductAndTotalPrice.put(product, priceAndHowMuch);
	}
	
	@Override
	public String toString() {
		return product + " " + howMuch + " " + price;
	}
	
	
	
}

public class Customers {
	
	static Map<Customer, Purchase> map = new HashMap<Customer, Purchase>();
	List<String> list = new ArrayList<String>();
	
	public Customers() {
		
		try {
			
			FileReader file = new FileReader("C:\\Temp\\customers.txt");
			BufferedReader reader = new BufferedReader(file);
			
			String line;
			
			while((line = reader.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line,";");
				while(st.hasMoreTokens()) {
					list.add(st.nextToken());
				}
			}
			
			for(int i = 0 ; i < 10 ; i++) {
				System.out.println("#i: " + i);
				if(i < 2) {
					System.out.println("Customer");
				} else {
					System.out.println("Purchase");
				}
				for(int j = 0 ; j <= 6 ; j ++) {
					int x = (j*5)+i;
					System.out.println("@j[" + j + "] = " + x);
					String dataOfCustomer_1 = list.get(x);
				}
				
			}
			
			System.out.println(list);
			
			// to obrazuje co chce uzyskać
			Customer c1 = new Customer(list.get(0), list.get(1));
			Purchase p1 = new Purchase(list.get(2), list.get(3), list.get(4));
			
			Customer c2 = new Customer(list.get(5), list.get(6));
			Purchase p2 = new Purchase(list.get(7), list.get(8), list.get(9));
			
			Customer c3 = new Customer(list.get(10), list.get(11));
			Purchase p3 = new Purchase(list.get(12), list.get(13), list.get(14));
			
			Customer c4 = new Customer(list.get(15), list.get(16));
			Purchase p4 = new Purchase(list.get(17), list.get(18), list.get(19));
			
			Customer c5 = new Customer(list.get(20), list.get(21));
			Purchase p5 = new Purchase(list.get(22), list.get(23), list.get(24));
			
			Customer c6 = new Customer(list.get(25), list.get(26));
			Purchase p6 = new Purchase(list.get(27), list.get(28), list.get(29));
			
			Customer c7 = new Customer(list.get(30), list.get(31));
			Purchase p7 = new Purchase(list.get(32), list.get(33), list.get(34));
			
			map.put(c1, p1);
			map.put(c2, p2);
			map.put(c3, p3);
			map.put(c4, p4);
			map.put(c5, p5);
			map.put(c6, p6);
			map.put(c7, p7);
			
			
			Set s = map.entrySet();
			Iterator it = s.iterator();
			while(it.hasNext()) {
				System.out.println(it.next());
			}
			
			
		} catch(IOException exc) {
			JOptionPane.showMessageDialog(null, "Brak pliku!", "Error", JOptionPane.ERROR_MESSAGE);
			exc.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		new Customers();
	}

}

Będę bardzo wdzięczny za każdą podpowiedź.
Pozdrawiam

0

siema, sugeruje przemyśleć projekt takiego programu.
dużą duplikację danych robisz, i generalnie zamotane masz z tymi kolekcjami i ogólnie szału nie ma.

może lepiej tak, klasa Customer, w której przechowujesz tylko dane klienta, i połączyć to agregacją, czyli po prostu klasa Customer posiada referencję do klasy np. CustomerItems, w której może znajdować się kolekcja, może nawet mapa, w której przechowujesz listę zakupów.

0

Przyznaję, że nie jest to może super przemyślane, ale z drugiej strony...
Chcę podzielić pobrane dane na 2 sekcje:

  1. id klienta
  2. jego zakupy
    następnie wrzucić to do mapy. Bardzo zależy mi na podzieleniu tych zakupów ze względu na produkt (jako key) oraz ilość i cenę (value). Chcę tak zrobić, bo chcę mieć później dostęp do id klienta jego nazwiska i kosztów. Chcę to mieć w mapie, bo dostęp ma być szybki.
    Nie mam lepszego pomysłu, a ten nie wydaje mi się aż taki zły. Natomiast mój problem raczej tkwił w podziale tych danych, tak, żeby wszystko znalazło się na swoim miejscu (w odpowiedniej klasie i na odpowiednich miejscach w mapach). Nie wiem jak pobrać te dane w jakiś sensowny sposób...

Pozdrawiam

0

To stwórz sobie klasę Item w której przechowaj cenę, i przechowaj to w mapie w klasie CustomerItems, czy tam Cart, a jako klucz Item a wartość ilość sztuk, na przykład.

0

Ale jak to mam podzielić, żeby wszystko było na odpowiednim miejscu?
StringTokenizer dzieli mi to, ale nie wiem jak to włożyć do mapy w odpowiednim miejscu. Myślałem, żeby to włożyć najpierw do ArrayList, a z tego później do mapy, ale też nie mam pomysłu jak to pochować do mapy. Pętla tu się nie spełni.
Dane są Stringami, więc nie mogę użyć instance of.
Kompletnie nie mam pomysłu, z której strony się za to zabrać.

0

Jaki masz problem, w takim układzie wystarczą Ci dwie kolekcję i z klientami i produktami, a reszta przecież pójdzie po referencjach.

0

a ten kawałek:

Customer c1 = new Customer(list.get(0), list.get(1));
Purchase p1 = new Purchase(list.get(2), list.get(3), list.get(4));
			
Customer c2 = new Customer(list.get(5), list.get(6));
Purchase p2 = new Purchase(list.get(7), list.get(8), list.get(9));
			
Customer c3 = new Customer(list.get(10), list.get(11));
Purchase p3 = new Purchase(list.get(12), list.get(13), list.get(14));
			
Customer c4 = new Customer(list.get(15), list.get(16));
Purchase p4 = new Purchase(list.get(17), list.get(18), list.get(19));
			
Customer c5 = new Customer(list.get(20), list.get(21));
Purchase p5 = new Purchase(list.get(22), list.get(23), list.get(24));
			
Customer c6 = new Customer(list.get(25), list.get(26));
Purchase p6 = new Purchase(list.get(27), list.get(28), list.get(29));
			
Customer c7 = new Customer(list.get(30), list.get(31));
Purchase p7 = new Purchase(list.get(32), list.get(33), list.get(34));
			
map.put(c1, p1);
map.put(c2, p2);
map.put(c3, p3);
map.put(c4, p4);
map.put(c5, p5);
map.put(c6, p6);
map.put(c7, p7);

jak zastąpić to jakąś sensowną pętlą?

0
BufferedReader in = new BufferedReader(new FileReader(plik));
String line;
while ((line = in.readLine()) != null) {
    String[] words = line.split(";");
    Customer c = new Customer(words[0], words[1]);
    Item i = new Item(words[2], words[3], words[4]);
    // robisz z tym co chcesz np dodajesz do map
}
in.close();

Powyzszy kod to tylko namiastka, sprawdzanie bledow by sie przydalo itp, niektore stringi (elementy tablicy words) nalezy sparsowac na int czy float, ale to sobie poradzisz.

0

Dziękuję wszystkim ślicznie ;)
i pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1