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:
- pierwsze dwa poszły do klasy Customer (w odpowiednich miejscach w mapie)
- 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