【发布时间】:2013-11-30 15:37:33
【问题描述】:
我正在尝试保存和加载数据,但我感到很沮丧,需要好好睡一觉。如果没有错误或空值,我似乎无法弄清楚这一点。
我的代码:
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;
}
}
}
}
我在同一方法中尝试做的是将所有内容保存到一个字符串中,因此当调用加载方法时,它将加载并将变量设置为保存在该字符串中的内容。
例如另存为""+ 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);
}
}
【问题讨论】:
-
到底是什么问题?
-
这不是一个可以帮你做功课的网站。告诉我们您遇到了什么错误。
-
您尚未将任何
Offers 添加到列表中 -
请同时发布Offer课程
-
按要求发布报价类
标签: java string inputstream outputstream