-1
我有一個倉庫項目,我可以創建長度,寬度和高度的箱子,並創建長度寬度高的貨架。而且我可以選擇將該框添加到此貨架上,但我想比較一下該框的長度,寬度和高度是否較小或等於貨架(L, W, H)
,但寬度必須與此相同w - w2 >= w3
,所以我想要計算它但我不知道如何找到貨架內已添加的箱子寬度的總和。貨架的寬度減去箱子寬度的總和> =新箱的寬度
w
=貨架w2
的寬度=架子w3
裏面已經加入框寬度的總和=新框的寬度
我真的初學者在Java中,需要幫助。這是到目前爲止的代碼我做了比較:
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;
}
}
保質類代碼:
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());
}
}
Box類代碼:
package de.majed.warehouse;
public class Box {
// The properties of the Box
private int Length, Width, Height;
private String id;
public Box() {
}
// To set the new properties values.
public Box(int newLength, int newWidth, int newHeight, String newID) {
setLength(newLength);
setWidth(newWidth);
setHeight(newHeight);
setID(newID);
}
public Box(Box newWidth) {
}
public int getLength() {
return Length;
}
public int getWidth() {
return Width;
}
public int getHeight() {
return Height;
}
public String getID() {
return id;
}
private void setLength(int newLength) {
Length = newLength;
}
private void setWidth(int newWidth) {
Width = newWidth;
}
private void setHeight(int newHeight) {
Height = newHeight;
}
private void setID(String newID) {
id = newID;
}
@Override
// To get the list of the new added boxes with their properties values.
public String toString() {
return "Box ID: " + getID() + " (L: " + getLength() + " - W: " + getWidth() + " - H: " + getHeight() + ")";
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof Box))
return false;
Box b = (Box) obj;
if (b == null)
return false;
if (this.id == null)
return b.id == null;
return this.id.equals(b.getID());
}
}
實際上我不知道,我有Box類和Shelf類的代碼,他們希望從我這裏創建這個代碼,我想添加到貨架上的這個代碼不能大於剩餘空間架子。你能幫助我嗎? –