2017-08-02 86 views
-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()); 
    } 

} 
+0

实际上我不知道,我有Box类和Shelf类的代码,他们希望从我这里创建这个代码,我想添加到货架上的这个代码不能大于剩余空间架子。你能帮助我吗? –

回答

0

也许最简单的解决办法是增加一个新的属性到货架称为“availableWidth”。每当箱子被添加到货架时,就从availableWidth中减去该箱子的宽度。换句话说,它会在你的addBox方法中完成。然后当你写你的fitsIntoShelf方法时,你可以调用getAvailableWidth(),假设你为它写了一个getter方法。另外,我想知道为什么你传入一个int来放入fitsIntoShelf()方法,因为你没有做任何事情。不是一个大问题,但它似乎是死代码。