Advertisement
Guest User

code

a guest
Jun 16th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 KB | None | 0 0
  1. package auctionapi;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class AuctionFunc {
  7.  
  8. private ArrayList<Buyer> buyerList;
  9. private ArrayList<Auction> AuctionList;
  10. private Scanner input = new Scanner (System.in);
  11. public AuctionFunc(ArrayList<Buyer> buyerList, ArrayList<Auction> AuctionList) {
  12. this.buyerList = buyerList;
  13. this.AuctionList = AuctionList;
  14. }
  15.  
  16.  
  17. public void findNotFinalAuction(){
  18. for (int index = 0; index < getSizeAuctionList(); index++){
  19. if (getStateAuction(index)){
  20. System.out.println("Aukcja numer" + index + "jest aktywna");
  21. }
  22. }
  23. }
  24. public void findFinalAuction(){
  25. for (int index=0; index<getSizeAuctionList();index++){
  26. if (!getStateAuction(index)){
  27. System.out.println("Aukcja numer" + index + "jest nieaktywna");
  28. }
  29.  
  30. }
  31.  
  32. }
  33. public void findSeller(){
  34. System.out.println("Podaj login poszukiwanego sprzedawcy");
  35. String findedLogin = input.nextLine();
  36. for (int index=0; index < getSizeBuyerList(); index++ ){
  37. if(isItSeller(findedLogin,index)){
  38. System.out.println("Użytkownik o podanym loginie:" + findedLogin +"został odnaleziony");
  39. getInfoAuction(index);
  40. break;
  41. }
  42. }
  43. }
  44. public void addTrustyPoint (){
  45. System.out.println("Komu chcesz odjąc punkty?");
  46. String findedLogin = input.nextLine();
  47. for (int index=0; index <getSizeAuctionList(); index++){
  48. if (isItSeller(findedLogin,index)){
  49. System.out.println("Użytkownik został odnaleziony");
  50. System.out.println("Użytkownik ma " + getPointSeller(index) + ".pkt");
  51. System.out.println("Ile chcesz mu odjąć punktów?");
  52. int minusPoint = input.nextInt();
  53. if (buyerCan(minusPoint,index)){
  54. System.out.println("Odejmuje punkty");
  55. removePoint(index,minusPoint);
  56. }
  57. break;
  58. }
  59. }
  60.  
  61. }
  62. public void removeTrustyPoint(){
  63. System.out.println("Komu chcesz dodać punkty");
  64. String findedLogin = input.nextLine();
  65. for (int index =0; index < getSizeAuctionList(); index++){
  66. if(isItSeller(findedLogin,index)){
  67. System.out.println("Użytkownik został odnaleziony");
  68. System.out.println("Użytkownik ma"+ getPointSeller(index) + ".pkt");
  69. System.out.println("Ile chcesz mu dodać punktów?");
  70. int addPoint = input.nextInt();
  71. addPoint(addPoint,index);
  72. break;
  73. }
  74. }
  75. }
  76. private boolean getStateAuction(int index){
  77. Auction auction = AuctionList.get(index);
  78. return auction.isIsActive();
  79.  
  80. }
  81. private int getSizeAuctionList(){
  82. int sizeList = AuctionList.size();
  83. return sizeList;
  84. }
  85.  
  86. private int getSizeBuyerList(){
  87. int sizeList = buyerList.size();
  88. return sizeList;
  89. }
  90. private boolean isItSeller(String searchLogin, int index){
  91. Auction auction = AuctionList.get(index);
  92. String sellerLogin = auction.getLogin();
  93. if (sellerLogin.equals(searchLogin)){
  94. return true;
  95. }
  96. return false;
  97. }
  98. private String getInfoAuction (int index){
  99. Auction auction = AuctionList.get(index);
  100. return auction.toString();
  101. }
  102. private int getPointSeller(int index){
  103. Auction auction = AuctionList.get(index);
  104. return auction.getTrustyPoint();
  105. }
  106. private int getPointBuyer(int index){
  107. Buyer buyer = buyerList.get(index);
  108. return buyer.getTrustyPoint();
  109. }
  110. private boolean buyerCan (int index, int inputPoints){
  111. if (getPointSeller(index)<inputPoints){
  112. return false;
  113. }
  114. return true;
  115. }
  116. private void removePoint (int points, int index){
  117. int nowPoints = getPointSeller(index);
  118. int afterPoints = nowPoints - points;
  119. setPointsSeller(index,afterPoints);
  120. }
  121. private void setPointsSeller(int index, int afterPoints){
  122. Auction auction = AuctionList.get(index);
  123. auction.setTrustyPoint(afterPoints);
  124. }
  125. private void addPoint (int points, int index){
  126. int nowPoints = getPointSeller(index);
  127. int afterPoints = nowPoints+points;
  128. setPointsSeller(index,afterPoints);
  129. }
  130.  
  131. public boolean initAuction (){
  132. System.out.println("Przystępujemy do aukcji");
  133. ArrayList<Double> prizeList = new ArrayList<Double>();
  134. ArrayList<Boolean> decisionList = new ArrayList<Boolean>();
  135. for (int i= 0; i < getSizeAuctionList(); i++){
  136. if (!getStateAuction(i)){
  137. continue;
  138. }
  139. else{
  140. int LongTime = getLongTimeAuction(i);
  141. for (int x = 0; x < LongTime ; x++){
  142. for (int index =0; index < getSizeBuyerList(); index++){
  143. boolean decision = heWantTakePartAuction();
  144. decisionList.add(decision);
  145. if (isCanTakePartAuction(index) && decision){
  146. double prize = takeYourPrize();
  147. if (getModeAuction(i) == 3 ){
  148. System.out.println("Ten tryb aukcji pozwala odrazu na odebranie przedmiotu");
  149. System.out.println("Wystarczy, się zgodzić na podaną cenę");
  150. System.out.println("Obecna cena przedmiotu to: " + getPrizeAuction(i) + "zł");
  151. if(BuyerDecision()){
  152. setIsActive(i);
  153. System.out.println("Kupiec wziął przedmiot");
  154. break;
  155. }
  156. if (!BuyerDecision()){
  157. changePrize(i);
  158. }
  159.  
  160. }
  161. prizeList.add(prize);
  162.  
  163. }
  164. if (!isCanTakePartAuction(index)){
  165. double prize = 0;
  166. prizeList.add(prize);
  167. continue;
  168. }
  169. }
  170. if (getModeAuction(i) == 1 && x == LongTime){
  171. double winnerOffer = whoWin(LongTime,prizeList);
  172. System.out.println("Zwycięzca oferty o " + getDescriptionAuction(i));
  173. System.out.println("Wygrał pan z ofertą : " + winnerOffer + " gratulujemy, zapraszamy po odbiór");
  174. setIsActive(i);
  175. }
  176. if (getModeAuction(i) == 2 && x == LongTime){
  177. if (!buyersTakeOffer(decisionList, LongTime)){
  178. double winnerOffer = whoWin(LongTime,prizeList);
  179. System.out.println("Zwyciężcą oferty o " + getDescriptionAuction(i));
  180. System.out.println("Wygrał pan z ofertą : " + winnerOffer + " gratulujemy, zapraszamy po odbiór");
  181. setIsActive(i);
  182. }
  183. LongTime ++;
  184. }
  185.  
  186. }
  187.  
  188.  
  189. }
  190.  
  191. }
  192.  
  193. return true;
  194. }
  195.  
  196. private boolean isCanTakePartAuction(int index){
  197. int minTrustPoint = getTrustyPointAuction(index);
  198. int buyerTrustPoint = getPointBuyer(index);
  199. if (minTrustPoint > buyerTrustPoint){
  200. System.out.println("Użytkownik o podanym loginie: " + getLoginBuyer(index) + " nie może wziąść udział aukcji");
  201. return false;
  202. }
  203. return true;
  204. }
  205. private int takeYourPrize (){
  206. System.out.println("Podaj swoją cenę:");
  207. int myPrize = input.nextInt();
  208. return myPrize;
  209. }
  210. private boolean heWantTakePartAuction (){
  211. System.out.println("Czy chcesz złożyć ofertę?TAK/NIE");
  212. String decision = input.nextLine();
  213. if (decision.equals("TAK")||decision.equals("tak")){
  214. return true;
  215. }
  216. return false;
  217. }
  218. private String getLoginBuyer(int index){
  219. Buyer buyer = buyerList.get(index);
  220. return buyer.getLogin();
  221. }
  222. private int getTrustyPointAuction(int index){
  223. Auction auction = AuctionList.get(index);
  224. return auction.getMinTrustyPoint();
  225. }
  226. private int getLongTimeAuction(int index){
  227. Auction auction = AuctionList.get(index);
  228. return auction.getLongTimeAuction();
  229. }
  230.  
  231. private int getModeAuction (int index){
  232. Auction auction = AuctionList.get(index);
  233. return auction.getModeAuction();
  234. }
  235. private String getDescriptionAuction(int index){
  236. Auction auction = AuctionList.get(index);
  237. return auction.getDescriptionAuction();
  238. }
  239. private void setIsActive (int index){
  240. Auction auction = AuctionList.get(index);
  241. auction.setIsActive(false);
  242. }
  243. private Double whoWin(int LongTime ,ArrayList<Double>prizeList){
  244. ArrayList<Double> lastPrizeList = new ArrayList<Double>();
  245. for (int x1=0; x1 < getSizeBuyerList(); x1++){
  246. Double LastPrizeList = prizeList.get(LongTime*getSizeBuyerList()-getSizeBuyerList()+x1);
  247. lastPrizeList.add(LastPrizeList);
  248. }
  249. double winnerOffer = lastPrizeList.get(0);
  250. for (int x2=0; x2 < lastPrizeList.size(); x2++){
  251. if (winnerOffer < lastPrizeList.get(x2))
  252. {
  253. winnerOffer = lastPrizeList.get(x2);
  254. }
  255.  
  256. }
  257. return winnerOffer;
  258. }
  259. private boolean buyersTakeOffer (ArrayList<Boolean>decisionList, int LongTime){
  260. ArrayList<Boolean> lastRound = new ArrayList<Boolean>();
  261. for (int x1=0; x1 < getSizeBuyerList(); x1++){
  262. boolean lastDecision = decisionList.get(LongTime*getSizeBuyerList()-getSizeBuyerList()+x1);
  263. lastRound.add(lastDecision);
  264. }
  265. boolean buyersWantPlay = false;
  266. for (int x2=0; x2<lastRound.size(); x2++){
  267. if (lastRound.get(x2)){
  268. buyersWantPlay = true;
  269. }
  270. }
  271. return buyersWantPlay;
  272. }
  273. private Double getPrizeAuction(int index){
  274. Auction auction = AuctionList.get(index);
  275. return auction.getStartPrize();
  276. }
  277. private boolean BuyerDecision(){
  278. System.out.println("Czy decydujesz się na kupno przedmiotu?TAK/NIE");
  279. String decision = input.nextLine();
  280. if (decision.equals("TAK")||decision.equals("tak")){
  281. return true;
  282. }
  283. return false;
  284.  
  285. }
  286. private void changePrize(int index){
  287. Auction auction = AuctionList.get(index);
  288. double nowPrize = auction.getStartPrize();
  289. auction.setStartPrize(nowPrize -1 );
  290. }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement