题目如下:有一个水果箱(box),箱子里装有水果(fruit)。每一种水果都有不同的重量(weight)和颜色(color),水果有:苹果(apple),梨(pear)。可以向水果箱(box)里添加水果(addFruit),也可以取出水果(getFruit)这个题目是论坛一个面试的题目,感觉虽然简单但是涉及java面向对象思维模式的东西却很多,拿出来再和大家讨论下

解决方案 »

  1.   


    利用面向对象思维,考虑系统的可扩展性:代码如下,仅供参考:1.水果箱对象:
    package com.box.fruit;import java.util.ArrayList;
    import java.util.List;public class Box {

    private List<Fruit> listBox = new ArrayList<Fruit>();

    private double maxWeight = 5000;

    private double minWeight = 500;

    private double currWeight = 0;

    public Box(){
    }

    public Box(double maxWeight,double minWeight){
    if(maxWeight > 0 && minWeight > 0 && maxWeight > minWeight) {
    this.maxWeight = maxWeight;
    this.minWeight = minWeight;
    }
    }

    public synchronized void inFruits(Fruit[] fruits) {
    if(null != fruits && fruits.length > 0) {
    int fruitSize = fruits.length;
    int i = 0; 
    double fruitWeight = this.getFruitWeight(fruits);
    System.out.println("即将放入的质量:"+ fruitWeight);
    if((this.currWeight + fruitWeight) > this.maxWeight) {
    System.out.println("即将放入的水果质量与箱中现有水果质量已经超过水果箱可承载的最大质量,不能放入这些水果!");
    return;
    }
    currWeight += fruitWeight;
    while(i < fruitSize) {
    listBox.add(fruits[i]);
    System.out.println("放入" + fruits[i].getWeight() + "克" + fruits[i].getColor() + fruits[i].getTypeName() + "一个");
    i++;
    }
    System.out.println("当前水果箱总质量:"+ currWeight + ",要求的最大质量:"+maxWeight+",要求的最小质量:"+ minWeight);
    }
    }

    public synchronized void outFruits(Fruit[] fruits) {
    if(null != fruits && fruits.length > 0) {
    int fruitSize = fruits.length;
    int i = 0; 
    double fruitWeight = this.getFruitWeight(fruits);
    System.out.println("即将取出的质量:"+ fruitWeight);
    if((this.currWeight - fruitWeight) < this.minWeight) {
    System.out.println("取出这些水果后箱中的水果质量将小于箱存储的最小质量,不能取出这么多的水果!");
    return;
    }
    currWeight -= fruitWeight;
    while(i < fruitSize) {
    listBox.remove(fruits[i]);
    System.out.println("取出" + fruits[i].getWeight() + "克" + fruits[i].getColor() + fruits[i].getTypeName() + "一个");
    i++;
    }
    }
    System.out.println("当前水果箱总质量:"+ currWeight + ",要求的最大质量:"+maxWeight+",要求的最小质量:"+ minWeight);
    }

    /**
     * 获取一次性放入或取出的水果质量.
     * @param fruit
     * @return
     */
    private double getFruitWeight(Fruit[] fruit) {
    double fruitWeight = 0;
    if(null != fruit && fruit.length>0) {
    for(int i=0; i< fruit.length; i++) {
    fruitWeight += fruit[i].getWeight();
    }
    }
    return fruitWeight;
    }
    }
      

  2.   

    2.水果对象:
    package com.box.fruit;public abstract class Fruit {

    private String typeName;
    private String color;
    private double weight;

    public void setTypeName(String typeName) {
    this.typeName = typeName;
    } public void setColor(String color) {
    this.color = color;
    } public void setWeight(double weight) {
    this.weight = weight;
    }

    public void setWeight() {
    this.weight = (int)(Math.random()*451) + (int)(Math.random()*51);
    }

    public String getTypeName() {
    return typeName;
    } public String getColor() {
    return color;
    } public double getWeight() {
    return weight;
    }
    }package com.box.fruit;public class Apple extends Fruit { public Apple() {
    super.setColor(Common.APPLECOLTYPE[(int)(Math.random()*Common.APPLECOLTYPE.length)]);
    super.setTypeName("苹果");
    super.setWeight();
    }
    }
    package com.box.fruit;public class Peal extends Fruit { public Peal() {
    super.setColor(Common.PEALCOLTYPE[(int)(Math.random()*Common.PEALCOLTYPE.length)]);
    super.setTypeName("梨子");
    super.setWeight();
    }
    }
    package com.box.fruit;import java.util.ArrayList;
    import java.util.List;public class fruitFactory { public static Fruit[] getFruits(int num){
    List<Fruit> fruits = new ArrayList<Fruit>();
    for(int i=0; i<num; i++){
    try {
    fruits.add((Fruit)Class.forName(Common.LISTFRUIT.get(((int)(Math.random()*2)))).newInstance());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return fruits.toArray(new Fruit[]{});
    }
    }
    package com.box.fruit;import java.util.ArrayList;
    import java.util.List;public class Common {

    public static final String[] APPLECOLTYPE = {"红色","黄色","绿色","白色"};

    public static final String[] PEALCOLTYPE = {"黄色","绿色","白色"};

    public static List<String> LISTFRUIT = new ArrayList<String>();

    static {
    LISTFRUIT.add("com.box.fruit.Apple");
    LISTFRUIT.add("com.box.fruit.Peal");
    }
    }此公告类可进一步修改:利用加载配置文件的方式可增强扩展性。
      

  3.   

    3.两个线程:
    package com.box.fruit;public class InFruitThread implements Runnable { private Box box;

    public InFruitThread(Box box){
    this.box = box;
    }

    @Override
    public void run() {
    while(true) {
    box.inFruits(fruitFactory.getFruits((int)(Math.random()*5)+1));
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    package com.box.fruit;public class OutFruitThread implements Runnable { private Box box;

    public OutFruitThread(Box box){
    this.box = box;
    }

    @Override
    public void run() {
    while(true) {
    box.outFruits(fruitFactory.getFruits((int)(Math.random()*5)+1));
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }4.主类:
    package com.box.fruit;public class MainClass { public static void main(String[] args) {
    Box box = new Box(10000,1000);
    InFruitThread in = new InFruitThread(box);
    OutFruitThread out = new OutFruitThread(box);
    new Thread(in).start();
    new Thread(out).start();
    }
    }
      

  4.   

    package allen;
    import java.util.Scanner;
    public class BoxFrurit{
             Scanner x = new Scanner(System.in);
              fruit [] Fruit= new fruit[100];
              int id = 1;
              public void addfruit()
             {
              System.out.println("请输入水果的重量");
              int weigth = x.nextInt();
              System.out.println("请输入水果的颜色");
              String color = x.next();
              System.out.println("请输入水果名");
              String name = x.next();
             fruit F = new fruit(weigth,color,name);
              Fruit[id-1] = F;
              id++; 
               }
         public void getfruit()
         {
         System.out.println("请输入水果名");
                String name = x.next();
                for(int i=0;i<id-1;i++)
                {
                 fruit Fr = Fruit[i];
                      String tempname =Fr.Getname(); 
                 if(tempname.equals(name))
                 {
                       System.out.println(Fruit[i]+" ");
                  }
              }
         }
         public void modif()
         {
         System.out.println("请输入你想要修改的水果名");
                String name = x.next();   
                 System.out.println("请输入你想要的水果名!");
                  String newname=x.next();
                  System.out.println("请输入你想要的水果颜色!");
                  String newcolor=x.next();
                  System.out.println("请输入你想要的水果重量!");
                       int newweigth=x.nextInt();    
                for(int i=0;i<id-1;i++)
                {
                 fruit Fr = Fruit[i];
                      String tempname =Fr.Getname(); 
                 if(tempname.equals(name))
                 {
                 
                     Fr.Setname(newname);
                     Fr.Setcolor(newcolor);
                     Fr.Setweigth(newweigth);
                     System.out.println("修改成功!");
                  }
           }
         }
         public void getAll()
         {
          for(int i=0;i<Fruit.length;i++)
              {
               if(Fruit[i]!=null)
                   System.out.println(Fruit[i]+" ");
              }
        
         }
         public void mianMenu()
         {
           Scanner x = new Scanner(System.in);
           while(true)
         {
         System.out.println("1 添加水果  2 取出单个水果  3 取出全部水果  4 修改水果  5退出");
         int n = x.nextInt();
         switch(n)
         {
         case 1: 
                  addfruit();
                  System.out.println("添加成功");
                  break;
            case 2:
                  getfruit();
                  break;         
         case 3:
                  getAll();
                  break;
              case 4:
                        modif();
                   break;
              case 5:
                      System.exit(0);  
         default:
                  
         }
         }
        }
            public static void main(String args[]){
                    new BoxFrurit().mianMenu();
             }

    }
    class fruit{
    private int weigth;
    private String color;
    private String name;
        public fruit(int weigth,String color,String name)
        {
              this.weigth = weigth;
              this.color = color;
              this.name = name;
         }
         public void Setname(String name)
         {
         this.name= name;
         }
         public String Getname()
         {
             return this.name;     
        }
       public void Setweigth(int weigth)
       {
        this.weigth = weigth;
        }
         public int Getweigth()
       {
        return this.weigth ;
        }
          public void Setcolor(String color)
       {
        this.color = color;
        }
          public String Getcolor()
       {
         return this.color;
        }
        public String toString()
        {
        String s=Getname()+"是"+Getcolor()+"有"+Getweigth()+"公斤";
        return s;
        }
    }

    “希望各位给点建议”