Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package auctionapi;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class AuctionFunc {
- private ArrayList<Buyer> buyerList;
- private ArrayList<Auction> AuctionList;
- private Scanner input = new Scanner (System.in);
- public AuctionFunc(ArrayList<Buyer> buyerList, ArrayList<Auction> AuctionList) {
- this.buyerList = buyerList;
- this.AuctionList = AuctionList;
- }
- public void findNotFinalAuction(){
- for (int index = 0; index < getSizeAuctionList(); index++){
- if (getStateAuction(index)){
- System.out.println("Aukcja numer" + index + "jest aktywna");
- }
- }
- }
- public void findFinalAuction(){
- for (int index=0; index<getSizeAuctionList();index++){
- if (!getStateAuction(index)){
- System.out.println("Aukcja numer" + index + "jest nieaktywna");
- }
- }
- }
- public void findSeller(){
- System.out.println("Podaj login poszukiwanego sprzedawcy");
- String findedLogin = input.nextLine();
- for (int index=0; index < getSizeBuyerList(); index++ ){
- if(isItSeller(findedLogin,index)){
- System.out.println("Użytkownik o podanym loginie:" + findedLogin +"został odnaleziony");
- getInfoAuction(index);
- break;
- }
- }
- }
- public void addTrustyPoint (){
- System.out.println("Komu chcesz odjąc punkty?");
- String findedLogin = input.nextLine();
- for (int index=0; index <getSizeAuctionList(); index++){
- if (isItSeller(findedLogin,index)){
- System.out.println("Użytkownik został odnaleziony");
- System.out.println("Użytkownik ma " + getPointSeller(index) + ".pkt");
- System.out.println("Ile chcesz mu odjąć punktów?");
- int minusPoint = input.nextInt();
- if (buyerCan(minusPoint,index)){
- System.out.println("Odejmuje punkty");
- removePoint(index,minusPoint);
- }
- break;
- }
- }
- }
- public void removeTrustyPoint(){
- System.out.println("Komu chcesz dodać punkty");
- String findedLogin = input.nextLine();
- for (int index =0; index < getSizeAuctionList(); index++){
- if(isItSeller(findedLogin,index)){
- System.out.println("Użytkownik został odnaleziony");
- System.out.println("Użytkownik ma"+ getPointSeller(index) + ".pkt");
- System.out.println("Ile chcesz mu dodać punktów?");
- int addPoint = input.nextInt();
- addPoint(addPoint,index);
- break;
- }
- }
- }
- private boolean getStateAuction(int index){
- Auction auction = AuctionList.get(index);
- return auction.isIsActive();
- }
- private int getSizeAuctionList(){
- int sizeList = AuctionList.size();
- return sizeList;
- }
- private int getSizeBuyerList(){
- int sizeList = buyerList.size();
- return sizeList;
- }
- private boolean isItSeller(String searchLogin, int index){
- Auction auction = AuctionList.get(index);
- String sellerLogin = auction.getLogin();
- if (sellerLogin.equals(searchLogin)){
- return true;
- }
- return false;
- }
- private String getInfoAuction (int index){
- Auction auction = AuctionList.get(index);
- return auction.toString();
- }
- private int getPointSeller(int index){
- Auction auction = AuctionList.get(index);
- return auction.getTrustyPoint();
- }
- private int getPointBuyer(int index){
- Buyer buyer = buyerList.get(index);
- return buyer.getTrustyPoint();
- }
- private boolean buyerCan (int index, int inputPoints){
- if (getPointSeller(index)<inputPoints){
- return false;
- }
- return true;
- }
- private void removePoint (int points, int index){
- int nowPoints = getPointSeller(index);
- int afterPoints = nowPoints - points;
- setPointsSeller(index,afterPoints);
- }
- private void setPointsSeller(int index, int afterPoints){
- Auction auction = AuctionList.get(index);
- auction.setTrustyPoint(afterPoints);
- }
- private void addPoint (int points, int index){
- int nowPoints = getPointSeller(index);
- int afterPoints = nowPoints+points;
- setPointsSeller(index,afterPoints);
- }
- public boolean initAuction (){
- System.out.println("Przystępujemy do aukcji");
- ArrayList<Double> prizeList = new ArrayList<Double>();
- ArrayList<Boolean> decisionList = new ArrayList<Boolean>();
- for (int i= 0; i < getSizeAuctionList(); i++){
- if (!getStateAuction(i)){
- continue;
- }
- else{
- int LongTime = getLongTimeAuction(i);
- for (int x = 0; x < LongTime ; x++){
- for (int index =0; index < getSizeBuyerList(); index++){
- boolean decision = heWantTakePartAuction();
- decisionList.add(decision);
- if (isCanTakePartAuction(index) && decision){
- double prize = takeYourPrize();
- if (getModeAuction(i) == 3 ){
- System.out.println("Ten tryb aukcji pozwala odrazu na odebranie przedmiotu");
- System.out.println("Wystarczy, się zgodzić na podaną cenę");
- System.out.println("Obecna cena przedmiotu to: " + getPrizeAuction(i) + "zł");
- if(BuyerDecision()){
- setIsActive(i);
- System.out.println("Kupiec wziął przedmiot");
- break;
- }
- if (!BuyerDecision()){
- changePrize(i);
- }
- }
- prizeList.add(prize);
- }
- if (!isCanTakePartAuction(index)){
- double prize = 0;
- prizeList.add(prize);
- continue;
- }
- }
- if (getModeAuction(i) == 1 && x == LongTime){
- double winnerOffer = whoWin(LongTime,prizeList);
- System.out.println("Zwycięzca oferty o " + getDescriptionAuction(i));
- System.out.println("Wygrał pan z ofertą : " + winnerOffer + " gratulujemy, zapraszamy po odbiór");
- setIsActive(i);
- }
- if (getModeAuction(i) == 2 && x == LongTime){
- if (!buyersTakeOffer(decisionList, LongTime)){
- double winnerOffer = whoWin(LongTime,prizeList);
- System.out.println("Zwyciężcą oferty o " + getDescriptionAuction(i));
- System.out.println("Wygrał pan z ofertą : " + winnerOffer + " gratulujemy, zapraszamy po odbiór");
- setIsActive(i);
- }
- LongTime ++;
- }
- }
- }
- }
- return true;
- }
- private boolean isCanTakePartAuction(int index){
- int minTrustPoint = getTrustyPointAuction(index);
- int buyerTrustPoint = getPointBuyer(index);
- if (minTrustPoint > buyerTrustPoint){
- System.out.println("Użytkownik o podanym loginie: " + getLoginBuyer(index) + " nie może wziąść udział aukcji");
- return false;
- }
- return true;
- }
- private int takeYourPrize (){
- System.out.println("Podaj swoją cenę:");
- int myPrize = input.nextInt();
- return myPrize;
- }
- private boolean heWantTakePartAuction (){
- System.out.println("Czy chcesz złożyć ofertę?TAK/NIE");
- String decision = input.nextLine();
- if (decision.equals("TAK")||decision.equals("tak")){
- return true;
- }
- return false;
- }
- private String getLoginBuyer(int index){
- Buyer buyer = buyerList.get(index);
- return buyer.getLogin();
- }
- private int getTrustyPointAuction(int index){
- Auction auction = AuctionList.get(index);
- return auction.getMinTrustyPoint();
- }
- private int getLongTimeAuction(int index){
- Auction auction = AuctionList.get(index);
- return auction.getLongTimeAuction();
- }
- private int getModeAuction (int index){
- Auction auction = AuctionList.get(index);
- return auction.getModeAuction();
- }
- private String getDescriptionAuction(int index){
- Auction auction = AuctionList.get(index);
- return auction.getDescriptionAuction();
- }
- private void setIsActive (int index){
- Auction auction = AuctionList.get(index);
- auction.setIsActive(false);
- }
- private Double whoWin(int LongTime ,ArrayList<Double>prizeList){
- ArrayList<Double> lastPrizeList = new ArrayList<Double>();
- for (int x1=0; x1 < getSizeBuyerList(); x1++){
- Double LastPrizeList = prizeList.get(LongTime*getSizeBuyerList()-getSizeBuyerList()+x1);
- lastPrizeList.add(LastPrizeList);
- }
- double winnerOffer = lastPrizeList.get(0);
- for (int x2=0; x2 < lastPrizeList.size(); x2++){
- if (winnerOffer < lastPrizeList.get(x2))
- {
- winnerOffer = lastPrizeList.get(x2);
- }
- }
- return winnerOffer;
- }
- private boolean buyersTakeOffer (ArrayList<Boolean>decisionList, int LongTime){
- ArrayList<Boolean> lastRound = new ArrayList<Boolean>();
- for (int x1=0; x1 < getSizeBuyerList(); x1++){
- boolean lastDecision = decisionList.get(LongTime*getSizeBuyerList()-getSizeBuyerList()+x1);
- lastRound.add(lastDecision);
- }
- boolean buyersWantPlay = false;
- for (int x2=0; x2<lastRound.size(); x2++){
- if (lastRound.get(x2)){
- buyersWantPlay = true;
- }
- }
- return buyersWantPlay;
- }
- private Double getPrizeAuction(int index){
- Auction auction = AuctionList.get(index);
- return auction.getStartPrize();
- }
- private boolean BuyerDecision(){
- System.out.println("Czy decydujesz się na kupno przedmiotu?TAK/NIE");
- String decision = input.nextLine();
- if (decision.equals("TAK")||decision.equals("tak")){
- return true;
- }
- return false;
- }
- private void changePrize(int index){
- Auction auction = AuctionList.get(index);
- double nowPrize = auction.getStartPrize();
- auction.setStartPrize(nowPrize -1 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement