Сериализация в xml, txt, json
03.12.2017, 21:38. Показов 1740. Ответов 0
доброго времени суток. прошу помочь реализовать сериализацию и десериализацию в xml, txt, json, так как совсем ничегошеньки не получаеться. всегда какая-то ошибка в gradle(Eclipse). вот собственно класс Goods и класс Warehouse, их и нужно сериализовать.
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
| import java.util.regex.*;
import java.time.LocalDate;
import java.util.regex.Pattern;
import java.io.*;
public class Goods implements Serializable{
String patternName = "[A-Z]{1}[a-z]{2,50}";
String patternType = "[A-Z]{1}[a-z]{2,50}";
private String name;
private String type;
private double price;
private LocalDate productionDate;
public LocalDate getProductionDate() {
return productionDate;
}
public Goods(String name, String type, double price, LocalDate productionDate, LocalDate expiryDate) {
this.name = name;
this.type = type;
this.price = price;
this.productionDate = productionDate;
this.expiryDate = expiryDate;
}
private LocalDate expiryDate;
public LocalDate getExpiryDate() {
return expiryDate;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public double getPrice() {
return price;
}
public Goods() {
super();
}
public static Builder newBuilder() {
return new Goods().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setName(String name) {
Pattern pName = Pattern.compile(patternName);
Matcher mName = pName.matcher(name);
if (mName.matches()) {
Goods.this.name = name;
} else
throw new RuntimeException("Invalid name");
return this;
}
public Builder setType(String type) {
Pattern pType = Pattern.compile(patternType);
Matcher mType = pType.matcher(type);
if (mType.matches()) {
Goods.this.type = type;
} else
throw new RuntimeException("Invalid type");
return this;
}
// public Builder setPrice(double price) {
// String patternPrice = "\\d{1,}+\\d{2}";
// Pattern pPrice = Pattern.compile(patternPrice);
// Matcher mPrice = pPrice.matcher(price);
// if (mPrice.matches()){
// Goods.this.price = price;}
// else throw new RuntimeException("Invalid price");
// return this;
// }
public Builder setProductionDate(LocalDate productionDate) {
int resultOfComparison = LocalDate.now().compareTo(productionDate);
if (resultOfComparison == -1 || resultOfComparison == 0) {
Goods.this.productionDate = productionDate;
} else
throw new RuntimeException("Invalid production date");
return this;
}
public Builder setExpiryDate(LocalDate expiryDate) {
Goods.this.expiryDate = expiryDate;
return this;
}
public Goods build() {
return Goods.this;
}
}
public static int ifExpired(Goods good) {
return LocalDate.now().compareTo(good.getExpiryDate());
}
@Override
public String toString() {
return "Goods [name=" + name + ", type=" + type + ", price=" + price + ", productionDate=" + productionDate
+ ", expiryDate=" + expiryDate + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((expiryDate == null) ? 0 : expiryDate.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((productionDate == null) ? 0 : productionDate.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (expiryDate == null) {
if (other.expiryDate != null)
return false;
} else if (!expiryDate.equals(other.expiryDate))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
if (productionDate == null) {
if (other.productionDate != null)
return false;
} else if (!productionDate.equals(other.productionDate))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setPrice(double price) {
this.price = price;
}
public void setProductionDate(LocalDate productionDate) {
this.productionDate = productionDate;
}
public void setExpiryDate(LocalDate expiryDate) {
this.expiryDate = expiryDate;
}
} |
|
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| import static org.testng.Assert.assertTrue;
import java.awt.List;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class WarehouseMap
{
private String responsibleForWarehouse;
private String address;
Map<Goods, Integer> base = new HashMap<Goods, Integer>();
public String getResponsibleForWarehouse() {
return responsibleForWarehouse;
}
public void setResponsibleForWarehouse(String responsibleForWarehouse) {
this.responsibleForWarehouse = responsibleForWarehouse;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int countGoods() {
return this.base.size();
}
public void addGoods(Goods goods) {
if (this.base.containsKey(goods))
{
base.put(goods, base.get(goods)+1);
}
else base.put(goods, 1);
}
public void deleteGoods(Goods goods) {
if(this.base.containsKey(goods))
{
if(base.get(goods) == 1)
{
this.base.remove(goods);
}
else if(base.get(goods) > 1)
{
base.put(goods, base.get(goods)-1);
}
}
}
@Override
public String toString() {
return "WarehouseMap [responsibleForWarehouse=" + responsibleForWarehouse + ", address=" + address
+ ", base=" + base + "]";
}
public WarehouseMap(String responsibleForWarehouse, String address, Map<Goods, Integer> base) {
super();
this.responsibleForWarehouse = responsibleForWarehouse;
this.address = address;
this.base = base;
}
public double priceOfAllItems()
{
double result = 0;
for (Map.Entry<Goods, Integer> entry : base.entrySet())
{
result += entry.getKey().getPrice()*entry.getValue();
}
return result;
}
public int amountOfItemsForMoney(Goods goods, double am_money) {
if (goods != null && am_money > 0) {
for (Map.Entry<Goods, Integer> entry : base.entrySet()) {
if (entry.getKey().equals(goods))
{
return (int) (am_money / entry.getKey().getPrice());
}
}
}
return 0;
}
public double sumOfMoneyForAllSpecificItems(String desiredType) {
double sumOfSimilar =0.;
for (Map.Entry<Goods, Integer> entry : base.entrySet()) {
if (entry.getKey().getType().equals(desiredType)) {
sumOfSimilar+= entry.getKey().getPrice()*entry.getValue();
}
}
return sumOfSimilar;
}
public ArrayList<Goods> similarItems(String desiredType) {
ArrayList<Goods> simItems = new ArrayList<>();
if (desiredType != null) {
for (Map.Entry<Goods, Integer> entry : base.entrySet()) {
if ( desiredType.equals(entry.getKey().getType())) {
simItems.add(entry.getKey());
}
}
}
return simItems;
}
}
////_-*-_\\_-_//_-*-_\\_-_//_-*-_\\_-_//_-*-_\\_-_//_-*-_\\
/*
@Override
public String toString() {
return "Warehouse [goodsArray=" + goodsArray + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((goodsArray == null) ? 0 : goodsArray.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Warehouse other = (Warehouse) obj;
if (goodsArray == null) {
if (other.goodsArray != null)
return false;
} else if (!goodsArray.equals(other.goodsArray))
return false;
return true;
}
*/ |
|
вот, например, для xml мои старания
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDate;
import com.thoughtworks.xstream.*;
public class Writer {
public static void main(String[] args) {
Goods e = new Goods();
e.setName("AIHAO");
e.setType("Pencil");
e.setPrice(5.6);
e.setProductionDate(LocalDate.of(2006,7,8));
e.setExpiryDate(LocalDate.of(2010,7,8));
XStream xs = new XStream();
try {
FileOutputStream fs = new FileOutputStream("C:/x/xmlGoods.txt");
xs.toXML(e, fs);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
} |
|
| Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Reader {
public static void main(String[] args) {
XStream xs = new XStream(new DomDriver());
Goods e = new Goods();
try {
FileInputStream fis = new FileInputStream("C:/x/xmlGoods.txt");
xs.fromXML(fis, e);
System.out.println(e.toString());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
} |
|
не понимаю, где неправильно и что писать в build gradle. беру текст для dependency, не работает. заранее спасибо за помощь
0
|