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.70 KB | None | 0 0
  1. package Objects;
  2.  
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Vector;
  7.  
  8. public class PlateFactory {
  9. private static PlateFactory pf=null;
  10. static Map<Integer,Vector<Plate>> Garbage=new HashMap<Integer,Vector<Plate>>();
  11. public static int SupportedColors=11;
  12. public static PlateFactory getUniqueInstance()
  13. {
  14. if(pf==null)
  15. {
  16. pf=new PlateFactory();
  17. }
  18. return pf;
  19. }
  20. private PlateFactory() {
  21. // TODO Auto-generated constructor stub
  22. }
  23. public static void setSupportedColors(int n)
  24. {
  25. SupportedColors=n;
  26. }
  27. public static int getSupportedColors()
  28. {
  29. return SupportedColors;
  30. }
  31. /**
  32. *
  33. *
  34. * @return random plate with random color;
  35. * @throws IOException
  36. */
  37. public void addToGarbage(Plate garbage)
  38. {
  39. Vector<Plate>v=new Vector<Plate>();
  40.  
  41. if(garbage.getType().compareTo("platewithbase")==0)
  42. {
  43. if(Garbage.get(0)!=null)
  44. v=Garbage.get(0);
  45. v.add(garbage);
  46. Garbage.put(0, v);
  47. return ;
  48. }
  49.  
  50. if(garbage.getType().compareTo("platewithoutbase")==0)
  51. {
  52. if(Garbage.get(1)!=null)
  53. v=Garbage.get(1);
  54. v.add(garbage);
  55. Garbage.put(1, v);
  56. return;
  57. }
  58.  
  59. if(garbage.getType().compareTo("platewithdeepbase")==0)
  60. {
  61. if(Garbage.get(2)!=null)
  62. v=Garbage.get(2);
  63. v.add(garbage);
  64. Garbage.put(2, v);
  65. return;
  66. }
  67.  
  68. if(garbage.getType().compareTo("pot")==0)
  69. {
  70. if(Garbage.get(3)!=null)
  71. v=Garbage.get(3);
  72. v.add(garbage);
  73. Garbage.put(3, v);
  74. return;
  75. }
  76.  
  77. }
  78. public Plate GenerateRandomPlate(String s) throws IOException
  79. {
  80. int plateType=(int) (Math.random()*4);
  81. int plateColor=(int)(Math.random()*SupportedColors);
  82. Plate a=null;
  83. if(Garbage.get(plateType)==null||Garbage.get(plateType).size()==0)
  84. {
  85. if(plateType==0)//BasedPlate
  86. {
  87. a=new BasedPlate(plateColor);
  88.  
  89. }
  90. else if(plateType==1)//nonBasedPlate
  91. {
  92. a=new NonBasedPlate(plateColor);
  93. }
  94. else if(plateType==2)//deepPlate
  95. {
  96. a=new DeepPlate(plateColor);
  97. }
  98. else//potplate
  99. {
  100. a=new PotPlate(plateColor);
  101. }
  102. if(s=="left") {
  103. a.setX(-150);
  104. a.setY(75-a.getHeight());
  105. a.setType(s);
  106. }
  107. else {
  108. a.setX(1610);
  109. a.setY(75-a.getHeight());
  110. a.setType(s);
  111. }
  112.  
  113. a.setColor(plateColor);
  114. }
  115. else
  116. {
  117.  
  118. //System.out.println("ReUsed");
  119. Vector<Plate>v=new Vector<Plate>();
  120. v=Garbage.get(plateType);
  121. a=v.firstElement();
  122. v.remove(0);
  123. Garbage.put(plateType, v);
  124. if(s=="left") {
  125. a.setX(-150);
  126. a.setY(75-a.getHeight());
  127. a.setType(s);
  128. }
  129. else {
  130. a.setX(1610);
  131. a.setY(75-a.getHeight());
  132. a.setType(s);
  133. }
  134. }
  135.  
  136. return a;
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement