Edit: I got a working version using an ArrayList
void addItem(Item item, int count){
int stacks;
for (int i = 0; i < useableSlots && count != 0; i++){
if (itemSlot[i].stack.size() > 0){
if (itemSlot[i].stack.get(0) == item) {
if(itemSlot[i].stack.size() < item.stackLimit){
int n = itemSlot[i].stack.size();
for(; n < item.stackLimit && count > 0; count--, n++) {
itemSlot[i].stack.add(item);
}
}
}
}
if (i == (useableSlots - 1) && count > 0){
for(int n = 0; n < useableSlots && count != 0; n++){
stacks = ((count - (count % item.stackLimit)) / item.stackLimit);
if(itemSlot[n].occupied == false){
if(stacks == 0){
for(int j = 0; j < count; j++){
itemSlot[n].stack.add(item);
itemSlot[n].occupied = true;
}
count = 0;
}
else {
for(int j = 0; j < item.stackLimit; j++){
itemSlot[n].stack.add(item);
}
count -= item.stackLimit;
itemSlot[n].occupied = true;
}
}
if (n == (useableSlots - 1)){
println("You don't have any room in your inventory");
}
}
}
}
}
.
package com.projects.aoa;
import java.util.*;
public class Itemslot extends Item{
List<Item> stack = new ArrayList<Item>();
Item oi = new Item();
boolean occupied, isArray, full;
}
.
public class Inventory {
int useableSlots, slots = 50;
Itemslot[] itemSlot = new Itemslot[slots];
...}
I have an inventory made up of an array of Item
objects. I'm trying to have like items stack up to a certain limit before taking up another inventory space. I must be going about this the complete wrong way, because I've had the same problem for every on of my attempts.
The hp potion stackLimit is 25, (27 hpPotions would take two inventory spaces, one with 25 the other with 2.)
Main
playerOne.backPack.addItem(hpPotion, 20);
playerOne.backPack.printInventory();
playerOne.backPack.addItem(hpPotion, 15);
playerOne.backPack.printInventory();
Item.java
package com.projects.aoa;
import static com.projects.aoa.Print.*;
import java.util.*;
import java.io.*;
class Item {
//Item
String name, type, category;
int id;
int hp, mp, str, def, duration;
//Inventory
public boolean filled;
public int count, stackLimit;
static void getAllStats(Item[] e){
for(Item i : e){
getItemStats(i);
}
}
static void getItemStats(Item i){
i.getStats();
}
void getStats(){
try {
//System.out.println(System.getProperty("user.dir"));
FileInputStream fstream = new FileInputStream(System.getProperty("user.dir")
+ "/src/com/projects/aoa/" + this.type + "_" + this.name + ".txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int counter = 0;
while ((line = br.readLine()) != null) {
if (line.length() == 0){
break;
}
switch (counter) {
case 0:
this.hp = Integer.parseInt(line);
counter++;
break;
case 1:
this.mp = Integer.parseInt(line);
counter++;
break;
case 2:
this.def = Integer.parseInt(line);
counter++;
break;
case 3:
this.str = Integer.parseInt(line);
counter++;
break;
case 4:
this.stackLimit = Integer.parseInt(line);
counter++;
break;
case 5:
this.duration = Integer.parseInt(line);
counter++;
break;
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
void printStats(){
println("[" + name + "]");
println("Type: " + type);
println("Duration: " + duration);
println("HP: " + hp);
println("MP: " + mp);
println("Def: " + def);
println("Str: " + str);
}
}
Inventory Output: 3rd Attempt
addItem(hpPotion, 20);
[1] Mallet [2] BronzeHelmet [3] hpPotion(20) [4] Empty [5] Empty
[6] Empty [7] Empty [8] Empty [9] Empty [10] Empty
[11] Empty [12] Empty [13] Empty [14] Empty [15] Empty
[16] Empty [17] Empty [18] Empty [19] Empty [20] Empty
addItem(hpPotion, 15);
[1] Mallet [2] BronzeHelmet [3] hpPotion(10) [4] hpPotion(10) [5] Empty
[6] Empty [7] Empty [8] Empty [9] Empty [10] Empty
[11] Empty [12] Empty [13] Empty [14] Empty [15] Empty
[16] Empty [17] Empty [18] Empty [19] Empty [20] Empty
This same thing happens in the Second attempt, except it shows up adding the two
[3] hpPotion(35) [4] hpPotion(35)
It seems like it would just be a simple fix (and maybe it is), but I just have no clue what it could be. Any help would be appreciated.
Third Attempt
void addItem(Item item, int count){
boolean newStack = false;
int room, totalCount = 0;
for(int i = 0; i < 20; i++){
if(itemSlot[i] == item && count > 0){
room = (item.stackLimit - itemSlot[i].count);
if(room > 0){
if(count >= room){
itemSlot[i].count += room;
count -= room;
}
else if(count > 0 && count < room){
itemSlot[i].count += count;
count = 0;
break;
}
}
}
if(i >= 19 && count > 0){
for(int n = 0; n < 20; n++){
if(itemSlot[n].filled == false){
int stacks = ((count - (count % item.stackLimit)) / 25);
println(stacks);
if (stacks == 0){
itemSlot[n] = item;
itemSlot[n].filled = true;
itemSlot[n].count = count;
count = 0;
break;
}
else {
itemSlot[n] = item;
itemSlot[n].filled = true;
itemSlot[n].count = item.stackLimit;
count -= item.stackLimit;
}
}
}
}
}
}
Second Attempt
void addItem(Item item, int count){
boolean newStack = false;
int room, totalCount = 0;
outerLoopCurrentStack:
for(int i = 0; i < 20; i++) {
if(itemSlot[i].name == item.name){
if(itemSlot[i].count < itemSlot[i].stackLimit){
while(count > 0){
count--;
itemSlot[i].count++;
if(itemSlot[i].count == itemSlot[i].stackLimit) {
break outerLoopCurrentStack;
}
}
}
}
else if((i >= 19) && (count > 0)){
newStack = true;
}
}
if(newStack = true){
outerLoopNewStack:
for(int i = 0; i < 20; i++){
if(itemSlot[i].filled == false){
itemSlot[i] = item;
itemSlot[i].filled = true;
while(count > 0){
count--;
itemSlot[i].count++;
if(count == 0){
newItem = false;
count = 0;
break outerLoopNewStack;
}
}
}
}
}
}
}
First Attempt
void addItem(Item item, int count){
boolean newStack = false;
int room, totalCount = 0;
for (int i = 0; i < 20; i++){
if(itemSlot[i].name == item.name) {
if(itemSlot[i].count < item.stackLimit){
room = (item.stackLimit - itemSlot[i].count);
if (count > room){
itemSlot[i].count += room;
itemSlot[i].filled = true;
count -= room;
}
else {
itemSlot[i].count += count;
break;
}
}
}
else if(i == 19){
newStack = true;
}
}
if (newStack == true){
for(int i = 0; i < 20; i++){
if(itemSlot[i].filled == false) {
if(count > item.stackLimit){
itemSlot[i] = item;
itemSlot[i].count = item.stackLimit;
itemSlot[i].filled = true;
count -= item.stackLimit;
}
else{
itemSlot[i] = item;
itemSlot[i].count = count;
itemSlot[i].filled = true;
break;
}
}
}
}
}