我正在努力保存&加载数据,但我感到沮丧&需要一些该死的睡眠。如果没有bug或空值,我似乎无法弄明白。
我的代码:
public class Offers {
public static List<Offer> offers = new ArrayList<Offer>(2000);
public Offers() {
}
public static void saveOffers() {
for (Offer offer : offers) {
if (offer == null) {
continue;
}
int id = offer.id;
int item = offer.item;
int amount = offer.amount;
int price = offer.price;
int currentAmount = offer.currentAmount;
int removedAmount = offer.removedAmount;
int type = offer.type;
int completed = offer.completed ? 1 : 0;
int aborted = offer.aborted ? 1 : 0;
int currentPrice = offer.currentPrice;
int slot = offer.slot;
String owner = offer.owner;
String VALUES(""+ id + "" + item + "" + amount + "" + price + "" + currentAmount + "" + removedAmount + "" + type + "" + completed + "" + aborted + "" + currentPrice + "" + slot + "" + owner + "");
}
}
}
public static void load() {
BufferedReader stream = new BufferedReader(new InputStreamReader(
new FileInputStream("GEoffers.txt")));
int id = stream.readShort();
int item = stream.read();
int amount = stream.read();
int price = stream.read();
int currentAmount = stream.read();
int removedAmount = stream.read();
int type = stream.readShort();
boolean completed = stream.readShort() == 1;
boolean aborted = stream.readShort() == 1;
int currentPrice = stream.read();
int slot = stream.readShort();
String owner = stream.readString();
if (id != -1)
offers.add(new Offer(id, item, amount, currentAmount, removedAmount, price, type, owner, completed, slot));
for (Offer l : offers) {
if (l == null) {
continue;
}
if (l.id == id && l.item == item && l.amount == amount && l.currentAmount == currentAmount && l.price == price && l.type == type && l.owner.equals(owner) && l.slot == slot) {
l.currentPrice = currentPrice;
l.aborted = aborted;
break;
}
}
}
}
我在同一个方法中尝试做的是将所有内容保存到字符串中,这样当调用load方法时,它将加载&amp;将变量设置为该字符串中保存的内容。
例如保存为""+ id + "" + item + "" + amount + "" + price + "" + currentAmount + "" + removedAmount + "" + type + "" + completed + "" + aborted + "" + currentPrice + "" + slot + "" + owner + ""
并将变量设置为保存时的变量。
我已经完成了这项工作,我正在支付像5美元这样的人为我做这件事。只是寻求一些帮助,或者是否有人可以善待我的垃圾工作。 ++ * 10求助。
按要求提供课程:
public class Offer {
public int id = -1;
public int item = 0;
public int amount = 0;
public int currentAmount = 0;
public int removedAmount = 0;
public int price = 0;
public int type = 0;
public String owner = "";
public boolean completed = false;
public int currentPrice = 0;
public boolean aborted = false;
public int slot = 0;
public boolean itemsLeft = false;
/**
* Creates a new global offer.
*
* @param id
* @param item
* @param amount
* @param currentAmount
* @param removedAmount
* @param price
* @param type
* @param owner
* @param completed
* @param slot
*/
public Offer(int id, int item, int amount, int currentAmount, int removedAmount, int price, int type, String owner, boolean completed, int slot) {
this.id = id;
this.item = item;
this.amount = amount;
this.currentAmount = currentAmount;
this.removedAmount = removedAmount;
this.price = price;
this.type = type;
this.owner = owner;
this.completed = completed;
this.slot = slot;
this.itemsLeft = false;
}
public void updatePrice(int amount, boolean bool) {
if (bool) {
currentPrice += amount;
} else {
currentPrice -= amount;
}
}
public void clear() {
id = -1;
item = 0;
amount = 0;
currentAmount = 0;
price = 0;
type = -1;
owner = "";
completed = false;
currentPrice = 0;
aborted = false;
itemsLeft = false;
}
public void delete() {
Offers.offers.remove(this);
}
}
答案 0 :(得分:2)
也许你想要这样的东西
public class Offers {
public static List<Offer> offers = new ArrayList<Offer>(2000);
public Offers() {
}
public void saveOffers(int id, int item, int amount, int currentAmount,
int removedAmount, int price, int type, String owner,
boolean completed, int slot) {
Offer offer = new Offer(id, item, amount, currentAmount, removedAmount,
price, type, owner, completed, slot)
offers.add(offer);
}
}
您尚未在Offer
列表中添加任何offers
。所以你不能改变价值观。这就是你得到null的原因。 ArrayList<>(2000)
不会添加2000个商品,只需创建2000个容量。根据您定义Offer
类及其构造函数和getter方法(如果有)的方式,您可能需要类似我上面的代码。
运行它
public class TestOffers {
public static void main(String[] args){
Offers offers = new Offers();
int id = 1;
int item = 1;
int amount = 1;
int currentAmount = 1;
int removedAmount = 1;
int price = 10;
int type =- 1;
String owner = "Me";
boolean completed = true;
int slot =1;
offers.saveOffers(id, item, amount, currentAmount, removedAmount,
price, type, owner, completed, slot);
for (Offer offer : offers){
System.out.println(offer);
}
}
}
您还希望覆盖toString()
课程中的Offer
方法,以便在您想要打印offer
时获得所需的输出
public class Offer {
...
@Override
public String toString(){
return id + " " + item + " " + amount + " " + price + " "
+ currentAmount + " " + removedAmount + " " + type + " "
+ completed + " " + aborted + " " + currentPrice + " "
+ slot + " " + owner;
}
}