我有一个仓库项目,我可以创建长度,宽度和高度的盒子,并创建长度宽度高的货架。我可以选择将该框添加到此书架,但我想比较框的长度,宽度和高度是否小于或等于货架(L, W, H)
,但宽度必须与此{{1}相同所以我想算数,但我不知道如何找到货架内已经添加的箱子宽度的总和。
w - w2 >= w3
=货架的宽度w
=货架内已添加的框宽度的总和w2
=新框的宽度我是java的初学者,需要帮助。到目前为止,这是我用于比较的代码:
w3
货架类代码:
private boolean fitsIntoShelf(int place, Box box) {
int w = getwidth()-"the sum of the boxes widths inside it";
if (this.getlength() >= box.getLength() && w >= box.getWidth() && this.getheight() >= box.getHeight()) {
return true;
} else {
return false;
}
}
Box类代码:
package de.majed.warehouse;
import javax.swing.JOptionPane;
public class Shelf {
// the array of boxes stored in the shelf
private Box[] places;
// The properties of the shelf.
// size of the shelf
private int capacity = 10;
private int length, width, height;
private String ID;
/**
*
* @param capacity
* @param newid
*/
public Shelf(int cap, int newlength, int newwidth, int newheight, String newid) {
//
this.capacity = cap;
places = new Box[this.capacity];
setlength(newlength);
setwidth(newwidth);
setheight(newheight);
setid(newid);
}
/**
*
*/
public Shelf(int newlength, int newwidth, int newheight, String newid) {
setlength(newlength);
setwidth(newwidth);
setheight(newheight);
setid(newid);
}
public int getCapacity() {
return places.length;
}
public String getid() {
return ID;
}
private void setid(String newid) {
ID = newid;
}
public int getlength() {
return length;
}
private void setlength(int newlength) {
length = newlength;
}
public int getwidth() {
return width;
}
private void setwidth(int newwidth) {
width = newwidth;
}
public int getheight() {
return height;
}
private void setheight(int newheight) {
height = newheight;
}
/**
*
* @param place
* @return
*/
public Box getContentOf(int place) {
return places[place];
}
//
/**
* Add box if a place empty.
*
* @param box
* The Box which should be added
*/
public void addBox(Box box) {
/*
* check one time if box fits into shelf. if not >>>
* JOptionPane.showMessageDialog(null,
* "The box is bigger than the shelf.");
**/
for (int f = 0; f < places.length; f++) {
if (fitsIntoShelf(f, box)) {
} else {
JOptionPane.showMessageDialog(null, "The box is bigger than the shelf.");
break;
}
// check if there isa an empty place. if not
// JOptionPane.showMessageDialog(null, "No place found in the
// shelf.");
if (isPlaceEmpty(f)) {
places[f] = box;
return;
}
JOptionPane.showMessageDialog(null, "No place found in the shelf.");
}
}
/**
* Add the box if the place is empty.
*
* @param place
* @param box
*/
public void putBoxTo(int place, Box box) {
if (isPlaceEmpty(place)) {
places[place] = box;
} else {
// Message to user
JOptionPane.showInputDialog("There is no empty place");
}
}
// Check if place is empty.
public boolean isPlaceEmpty(int place) {
if (places[place] == null) {
return true;
} else {
return false;
}
}
private boolean fitsIntoShelf(int place, Box box) {
if (this.getlength() >= box.getLength() && this.getwidth() >= box.getWidth() && this.getheight() >= box.getHeight())
return true;
else {
return false;
}
}
// return index if found otherwise -1
/**
*
* @param box
* @return the index of the box, -1 if not found
*/
public int findBox(Box box, Shelf str) {
for (int i = 0; i < places.length; i++) {
if (places[i].equals(box)) {
JOptionPane.showMessageDialog(null, "The item has been found in: | " + str + " | ");
}
}
return -1;
}
public String toString() {
// To show the list of the shelves and the boxes inside it.
String str = "";
for (int i = 0; i < places.length; i++) {
if (!isPlaceEmpty(i)) {
str += places[i].toString() + "; ";
}
}
return "Shelf [ " + getid() + " ] >>>" + " boxes in shelf:" + str;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Shelf))
return false;
Shelf b = (Shelf) obj;
if (b == null)
return false;
if (this.ID == null)
return b.ID == null;
return this.ID.equals(b.getid());
}
}
答案 0 :(得分:0)
可能最简单的解决方案是向Shelf添加一个名为“availableWidth”的新属性。每次将一个框添加到工具架时,都会从availableWidth中减去该框的宽度。换句话说,它将在您的addBox方法中完成。然后当你编写fitIntoShelf方法时,你可以调用getAvailableWidth(),假设你为它编写了一个getter。另外,我想知道你为什么要将一个int传入fitIntoShelf()方法,因为你没有对它做任何事情。没什么大不了的,但它似乎只是死代码。