Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package Objects;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Vector;
  8.  
  9. public class PlateFactory {
  10. static int supportedColors=11;
  11. static PlateFactory pf=null;
  12. static Map<Integer,Vector<Plate>> Garbage=new HashMap<Integer,Vector<Plate>>();
  13. public static PlateFactory getInstance()
  14. {
  15. if(pf==null)
  16. {
  17. pf=new PlateFactory();
  18. }
  19. return pf;
  20. }
  21. /**
  22. * number of supported plate colors 1-11
  23. * @param n
  24. */
  25. public void setSupportedColors(int n)
  26. {
  27. supportedColors=n;
  28. }
  29. public int getSupportedColors()
  30. {
  31. return supportedColors;
  32. }
  33.  
  34.  
  35. public void addToGarbage(Plate garbage)
  36. {
  37. Vector<Plate>v=new Vector<Plate>();
  38.  
  39. if(garbage.getType().compareTo("platewithbase")==0)
  40. {
  41. if(Garbage.get(0)!=null)
  42. v=Garbage.get(0);
  43. v.add(garbage);
  44. Garbage.put(0, v);
  45. return ;
  46. }
  47.  
  48. if(garbage.getType().compareTo("platewithoutbase")==0)
  49. {
  50. if(Garbage.get(1)!=null)
  51. v=Garbage.get(1);
  52. v.add(garbage);
  53. Garbage.put(1, v);
  54. return;
  55. }
  56.  
  57. if(garbage.getType().compareTo("platewithdeepbase")==0)
  58. {
  59. if(Garbage.get(2)!=null)
  60. v=Garbage.get(2);
  61. v.add(garbage);
  62. Garbage.put(2, v);
  63. return;
  64. }
  65.  
  66. if(garbage.getType().compareTo("pot")==0)
  67. {
  68. if(Garbage.get(3)!=null)
  69. v=Garbage.get(3);
  70. v.add(garbage);
  71. Garbage.put(3, v);
  72. return;
  73. }
  74.  
  75. }
  76. /**
  77. *
  78. * @return random plate with random color;
  79. * @throws IOException
  80. */
  81. public Plate GenerateRandomPlate() throws IOException
  82. {
  83. int plateType=(int) (Math.random()*4);
  84. int plateColor=(int)(Math.random()*supportedColors);
  85. //System.out.println(plateColor+ " "+plateType);
  86. Plate a=null;
  87. if(Garbage.get(plateType)==null||Garbage.get(plateType).size()==0)
  88. {
  89. if(plateType==0)//BasedPlate
  90. {
  91. a=new BasedPlate(plateColor);
  92. }
  93. else if(plateType==1)//nonBasedPlate
  94. {
  95. a=new NonBasedPlate(plateColor);
  96. }
  97. else if(plateType==2)//deepPlate
  98. {
  99. a=new DeepPlate(plateColor);
  100. }
  101. else//potplate
  102. {
  103. a=new PotPlate(plateColor);
  104. }
  105. }
  106. else
  107. {
  108.  
  109. //System.out.println("ReUsed");
  110. Vector<Plate>v=new Vector<Plate>();
  111. v=Garbage.get(plateType);
  112. a=v.firstElement();
  113. v.remove(0);
  114. Garbage.put(plateType, v);
  115. }
  116.  
  117. return a;
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement