Anonymní profil Pavel Černý – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Pavel Černý – Programujte.comAnonymní profil Pavel Černý – Programujte.com

 

Příspěvky odeslané z IP adresy 94.113.212.–

Pavel Černý
Java › Ukázkové příklady
7. 2. 2020   #385425

   

                                                                                Game Reviews



public class Author {
    private String nick;
    private String email;

    public Author(String nick, String email) {
        this.nick = nick;
        this.email = email;
    }

    public String getNick() {
        return nick;
    }

    public String getEmail() {
        return email;
    }

    //@Override
    public String getInfo() {
        return "Author{" +
                "nick='" + nick + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}




public class Game {
    private int id;
    private String name;
    private Genre genre;

    public Game(int id, String name, Genre genre) {
        this.id = id;
        this.name = name;
        this.genre = genre;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Genre getGenre() {
        return genre;
    }

    //@Override
    public String getInfo() {
        return "Game{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", genre='" + genre + '\'' +
                '}';
    }
}




public class Review {
    private int id;
    private Game game;
    private Author author;
    private String text;
    private int score; //0-5


    public Review(int id, Game game, Author author, String text, int score) {
        this.id = id;
        this.game = game;
        this.author = author;
        this.text = text;
        if(score < 6) {this.score = score;}
        else this.score = 0; //System.out.println("only 0-5");

    }

    public int getId() {
        return id;
    }

    public Game getGame() {
        return game;
    }

    public Author getAuthor() {
        return author;
    }

    public String getText() {
        return text;
    }

    public int getScore() {
        return score;
    }

    //@Override
    public String getInfo() {
        return "Review{" +
                "id=" + id +
                ", game=" + game +
                ", author=" + author +
                ", text='" + text + '\'' +
                ", score=" + score +
                '}';
    }
}




public class ReviewDatabase {
    private Review[] reviews;
    private int lastIndex = 0;
    private int defaultCapacityThis = 0;

    public ReviewDatabase(int defaultCapacity) {
        reviews = new Review[defaultCapacity];
        this.defaultCapacityThis = defaultCapacity;
    }

    public void addNew(Review review) {
        if(lastIndex > defaultCapacityThis) {
            System.out.println("unable to add.");
        }
        else {
            reviews[lastIndex] = review;
            lastIndex++;
            System.out.println("Added.");
        }
    }

    public double averageScore(String gameName) { //prumerne hodnoceni hry
        double temp = 0;
        double count = 0;
        for(int i = 0; i < reviews.length; i++) {
            if(reviews[i].getGame().equals(gameName))
            {
                temp += reviews[i].getScore();
                count++;
            }
        }
        double average = temp / count;
        return average;
    }

    public Game getWorstGame() { //hra s nejnizsim hodnocenim
        int tempCount = 0;
        Game tempGame = reviews[0].getGame();
        int tempReviewScore = reviews[tempCount].getScore();
        for(int i = 0; i < reviews.length; i++) {
            if(reviews[i].getScore() < tempReviewScore) {
                tempGame = reviews[i].getGame();

            }
            tempCount++;
        }
        return tempGame;
    }

    public void printAllReviewFromReviewDatabase(Review[] reviewsToPrint) {
        for (int i = 0; i < reviewsToPrint.length; i++) {
            System.out.println(reviewsToPrint[i].getInfo());
        }
    }

    public int getDefaultCapacityThis() {
        return defaultCapacityThis;
    }
}




public class Main {

    public static void main(String[] args) {
	// write your code here
        Author author1 = new Author("nick1","nick1@email.cz");
        Author author2 = new Author("nick2","nick2@email.cz");
        Author author3 = new Author("nick3","nick3@email.cz");

        Game game1 = new Game(0,"gameName1",Genre.RPG);
        Game game2 = new Game(1,"gameName2",Genre.ACTION);

        Review review1 = new Review(0,game1,author1,"sampleText1",1);
        Review review2 = new Review(1,game1,author2,"sampleText2",3);
        Review review3 = new Review(1,game1,author3,"sampleText3",4);

        Review review4 = new Review(1,game2,author1,"sampleText4",2);
        Review review5 = new Review(1,game2,author2,"sampleText5",0);
        Review review6 = new Review(1,game2,author3,"sampleText6",5);

        ReviewDatabase reviewDatabase = new ReviewDatabase(6);
        reviewDatabase.addNew(review1);
        reviewDatabase.addNew(review2);
        reviewDatabase.addNew(review3);
        reviewDatabase.addNew(review4);
        reviewDatabase.addNew(review5);
        reviewDatabase.addNew(review6);

        double getAverageScore = reviewDatabase.averageScore("gameName1");
        System.out.println(getAverageScore);

        Game getWorstGame = reviewDatabase.getWorstGame();
        System.out.println("Worst Game: "+getWorstGame.getInfo());

        for (int i = 0; i < reviewDatabase.getDefaultCapacityThis(); i++) {
            //System.out.println(reviewDatabase[i].);
        }

        System.out.println();
    }
}












                                                                       Person



public class Person {
    private String name;
    private String surname;
    private int age;

    public Person(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", surname='" + surname + '\'' +
                ", age=" + age +
                '}';
    }
}




public class PersonDatabase {

    private Person[] persons;
    private int indexCount = 0;

    public PersonDatabase(int databaseCapacity)
    {
        persons = new Person[databaseCapacity];
    }

    public void addPerson(Person person){
        if(indexCount >= persons.length){
                System.out.println("Unable to add.");
        }
        else {
            persons[indexCount] = person;
            indexCount++;

            System.out.println("Person added.");
        }

    }

    //TODO


    public Person searchPerson(String name){
        for(int i = 0; i < persons.length; i++)
        {
            if(persons[i].getName().equals(name)) { return persons[i]; }
        }
        return null;
    }

    public Person searchPerson(String name, String surname){
        for(int i = 0; i < persons.length; i++)
        {
            if(persons[i].getName().equals(name)){
                if (persons[i].getSurname().equals(surname)) { return persons[i]; }
            }
        }
        return null;
    }

    public Person searchPerson(int age){
        for (int i = 0; i < persons.length; i++)
        {
            if(persons[i].getAge() == age) { return persons[i]; }
        }
        return null;
    }

}




public class Main {

    public static void main(String[] args) {
	// write your code here
        /*
        1. dopište chybějící metody ve třídě PersonDatabase - TODO
        2. vytvořte databázi PersonDatabase
        3. naplňte ji daty o osobách
        4. otestujte funkčnost metod
         */

        PersonDatabase personDatabase = new PersonDatabase(7);
        personDatabase.addPerson(new Person("jmeno1", "přijmeni1",1));
        personDatabase.addPerson(new Person("jmeno2", "přijmeni2",2));
        personDatabase.addPerson(new Person("jmeno3", "přijmeni3",3));
        personDatabase.addPerson(new Person("jmeno4", "přijmeni4",4));
        personDatabase.addPerson(new Person("jmeno5", "přijmeni5",5));
        personDatabase.addPerson(new Person("jmeno6", "přijmeni6",6));
        personDatabase.addPerson(new Person("jmeno7", "přijmeni7",7));
        personDatabase.addPerson(new Person("jmeno8", "přijmeni8",8));

        //Person searchByName = personDatabase.searchPerson("jmeno1");
        //Person searchByNameAndSurname = personDatabase.searchPerson("jmeno3", "přijmeni3");
        //Person searchByAge = personDatabase.searchPerson(7);

        System.out.println(personDatabase.searchPerson(8));
        System.out.println(personDatabase.searchPerson("jmeno8"));
        System.out.println(personDatabase.searchPerson("jmeno1","přijmeni1"));

        System.out.println();
    }
}










                                                                  Message




public class Message {
    private String text;
    private String nickName;

    public Message(String text, String nickName) {
        this.text = text;
        this.nickName = nickName;
    }

    public String getText() {
        return text;
    }

    public String getNickName() {
        return nickName;
    }

    public String getInfo() {
        return "Message{" +
                "text='" + text + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}




public class Chat {
    private Message[] messages;
    private int indexCount = 0;

    public Chat() {
        messages = new Message[10];
    }

    public void addNewMessage(Message message) {
        messages[indexCount] = message;
        indexCount++;
    }

    public Message[] getAllMessages() {
        Message[] tempMessages = new Message[indexCount];
        for(int i = 0; i < indexCount; i++) {
            tempMessages[i] = messages[i];
        }
        return tempMessages;
    }

    public Message[] getMessagesFromNickName(String requiredNickName) {
        int pom = getMessageCountByNickName(requiredNickName);
        int couter = 0;
        Message[] tempMessage = new Message[pom];
        for (int i = 0; i < indexCount; i++) {
            if(messages[i].getNickName().equals(requiredNickName)) {
                tempMessage[couter] = messages[i];
                couter++;
            }
        }
        return tempMessage;
    }

    public Message getLongestMessage() {
        Message temp = messages[0];
        for (int i = 0; i < indexCount; i++) {
            if (messages[i].getText().length() > temp.getText().length()) {
                temp = messages[i];
            }
        }
        return temp;
    }

    public int getMessageCountByNickName(String requiredNickName) {
        int count = 0;
        for(int i = 0; i < indexCount; i++) {
            if(messages[i].getNickName().equals(requiredNickName)) {
                count++;
            }
        }
        return count;
    }

    public void printMessage(Message[] messagesToPrint) {
        for (int i = 0; i < messagesToPrint.length; i++) {
            System.out.println(messagesToPrint[i].getInfo());
        }
    }
}




public class Main {

    public static void main(String[] args) {
	// write your code here

        Chat chat = new Chat();
        chat.addNewMessage(new Message("text","David"));
        chat.addNewMessage(new Message("text","Kuba"));
        chat.addNewMessage(new Message("text","David"));
        chat.addNewMessage(new Message("text","Tomaš"));
        chat.addNewMessage(new Message("textt","Kuba"));
        chat.addNewMessage(new Message("text","David"));

        Message[] getAllMessage = chat.getAllMessages();
        Message[] getMessageFromNickName = chat.getMessagesFromNickName("Kuba");
        Message getLongestMessage = chat.getLongestMessage();
        int getMessageCountByNickName = chat.getMessageCountByNickName("David");

        chat.printMessage(getAllMessage);

        System.out.println();

        chat.printMessage(getMessageFromNickName);

        System.out.println();

        System.out.println(getLongestMessage.getInfo());

        System.out.println();

        System.out.println(getMessageCountByNickName);

        System.out.println();
    }
}










                                                                      Billiard



public class Ball {
    private String color;
    private int number;
    private String type;

    public Ball(BallColor ballColor, int number, BallType ballType) {
        this.color = ballColor.toString();
        this.number = number;
        this.type = ballType.toString();
    }

    public String getColor() {
        return color;
    }

    public int getNumber() {
        return number;
    }

    public String getType() {
        return type;
    }

    //@Override
    public String getInfo() {
        return "Ball{" +
                "color='" + color + '\'' +
                ", number=" + number +
                ", type='" + type + '\'' +
                '}';
    }
}




public enum BallColor {
    Red,
    Green,
    Purple,
    Blue,
    Yellow,
    Orange,
    Black,
    Brown,
    White
}




public enum BallType {
    Solid,
    Stripes
}




import java.util.Random;

public class BilliardBalls {
    private Ball[] balls;

    public BilliardBalls() {
        balls = new Ball[17];
    }

    public void generateAllBalls() {
        balls[0] = new Ball(BallColor.Black,1,BallType.Solid);
        balls[1] = new Ball(BallColor.Blue,2,BallType.Solid);
        balls[2] = new Ball(BallColor.Brown,3,BallType.Solid);
        balls[3] = new Ball(BallColor.Green,4,BallType.Solid);
        balls[4] = new Ball(BallColor.Orange,5,BallType.Solid);
        balls[5] = new Ball(BallColor.Purple,6,BallType.Solid);
        balls[6] = new Ball(BallColor.Red,7,BallType.Solid);
        balls[7] = new Ball(BallColor.Yellow,8,BallType.Solid);

        balls[8] = new Ball(BallColor.Black,1,BallType.Stripes);
        balls[9] = new Ball(BallColor.Blue,2,BallType.Stripes);
        balls[10] = new Ball(BallColor.Brown,3,BallType.Stripes);
        balls[11] = new Ball(BallColor.Green,4,BallType.Stripes);
        balls[12] = new Ball(BallColor.Orange,5,BallType.Stripes);
        balls[13] = new Ball(BallColor.Purple,6,BallType.Stripes);
        balls[14] = new Ball(BallColor.Red,7,BallType.Stripes);
        balls[15] = new Ball(BallColor.Yellow,8,BallType.Stripes);

        balls[16] = new Ball(BallColor.White,0,BallType.Solid);
    }

    public void shakeAllBalls() {
        Random random = new Random();
        for(int i = 0; i < balls.length; i++) {
            int temp = random.nextInt(balls.length);
            swapBalls(i, temp);
        }
    }

    private void swapBalls(int leftIndex, int rightIndex) {
        Ball temp = balls[leftIndex];
        balls[leftIndex] = balls[rightIndex];
        balls[rightIndex] = temp;
    }

    public void printAllBalls() {
        for(int i = 0; i < balls.length; i++) {
            System.out.println(balls[i].getInfo());
        }
    }

    public Ball[] getBallsWithColor(String requiredColor) {
        Ball[] temp = new Ball[2];
        int pom = 0;
        for (int i = 0; i < balls.length; i++) {
            if (balls[i].getColor().equals(requiredColor)) {
                temp[i] = balls[i];
                pom++;
            }
        }
        return temp;
    }

    public Ball[] getRandomBalls(int count) {
        Ball[] temp = new Ball[count];
        Random random = new Random();
        int pom = 0;
        for(int i = 0; i < balls.length; i++) {
            if(pom < count) {
                temp[i] = balls[random.nextInt(balls.length)];
                pom++;
            }
        }
        return temp;
    }

    public int getNumberSumOfBalls() {
        int temp = 0;
        for(int i = 0; i < balls.length; i++) {
            temp = temp + balls[i].getNumber();
        }
        return temp;
    }

    public int getNumberSumOfBalls(String requiredType) {
        int temp = 0;
        for(int i = 0; i < balls.length; i++) {
            if(balls[i].getType().equals(requiredType)){
                temp = temp + balls[i].getNumber();
            }
        }
        return temp;
    }

    public void printBalls(Ball[] ballsToPrint) {
        for(int i = 0; i < ballsToPrint.length; i++) {
            System.out.println(ballsToPrint[i].getInfo());
        }
    }
}




public class Main {

    public static void main(String[] args) {
	// write your code here
        BilliardBalls billiardBalls = new BilliardBalls();
        billiardBalls.generateAllBalls();
        billiardBalls.printAllBalls();

        System.out.println();

        billiardBalls.shakeAllBalls();
        billiardBalls.printAllBalls();

        System.out.println();

        //billiardBalls.printBalls(billiardBalls.getBallsWithColor("Yellow"));

        System.out.println();

        billiardBalls.printBalls(billiardBalls.getRandomBalls(5));

        System.out.println();

        System.out.println(billiardBalls.getNumberSumOfBalls());

        System.out.println();

        System.out.println(billiardBalls.getNumberSumOfBalls("Solid"));
    }
}










                                                                                       Casino



public class Cart {
    private String color;
    private String rank;
    private String shape;

    public Cart(String color, String rank, String shape) {
        this.color = color;
        this.rank = rank;
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public String getRank() {
        return rank;
    }

    public String getShape() {
        return shape;
    }

    //@Override
    public String printInfo() {
        return "Cart{" +
                "color='" + color + '\'' +
                ", rank='" + rank + '\'' +
                ", shape='" + shape + '\'' +
                '}';
    }
}




import java.util.Random;

public class CartShaker {
    private Cart[] carts;

    public CartShaker() {
        carts = new Cart[32];
    }

    public void generateAllCart() {
        carts[0] = new Cart("Black","7","Clovers");
        carts[1] = new Cart("Black","8","Clovers");
        carts[2] = new Cart("Black","9","Clovers");
        carts[3] = new Cart("Black","10","Clovers");
        carts[4] = new Cart("Black","J","Clovers");
        carts[5] = new Cart("Black","Q","Clovers");
        carts[6] = new Cart("Black","K","Clovers");
        carts[7] = new Cart("Black","A","Clovers");

        carts[8] = new Cart("Black","7","Pikes");
        carts[9] = new Cart("Black","8","Pikes");
        carts[10] = new Cart("Black","9","Pikes");
        carts[11] = new Cart("Black","10","Pikes");
        carts[12] = new Cart("Black","J","Pikes");
        carts[13] = new Cart("Black","Q","Pikes");
        carts[14] = new Cart("Black","K","Pikes");
        carts[15] = new Cart("Black","A","Pikes");

        carts[16] = new Cart("Red","7","Hearts");
        carts[17] = new Cart("Red","8","Hearts");
        carts[18] = new Cart("Red","9","Hearts");
        carts[19] = new Cart("Red","10","Hearts");
        carts[20] = new Cart("Red","J","Hearts");
        carts[21] = new Cart("Red","Q","Hearts");
        carts[22] = new Cart("Red","K","Hearts");
        carts[23] = new Cart("Red","A","Hearts");

        carts[24] = new Cart("Red","7","Diamonds");
        carts[25] = new Cart("Red","8","Diamonds");
        carts[26] = new Cart("Red","9","Diamonds");
        carts[27] = new Cart("Red","10","Diamonds");
        carts[28] = new Cart("Red","J","Diamonds");
        carts[29] = new Cart("Red","Q","Diamonds");
        carts[30] = new Cart("Red","K","Diamonds");
        carts[31] = new Cart("Red","A","Diamonds");
    }

    public void shakeAllCards() {
        Random random = new Random();
        for(int i = 0; i < carts.length; i++) {
            int temp = random.nextInt(carts.length);

            swapCard(i,temp);
        }
    }

    private void swapCard(int leftIndex, int rightIndex) {
        Cart temp = carts[leftIndex];
        carts[leftIndex] = carts[rightIndex];
        carts[rightIndex] = temp;
    }

    public void printAllCards() {
        for(int i = 0; i < carts.length; i++) {
            System.out.println(carts[i].printInfo());
        }
    }

    public Cart[] getCartsWitchColor(String requiredColor) {
        Cart[] tempCards = new Cart[16];
        int count = 0;
        for(int i = 0; i < carts.length; i++) {
            if(carts[i].getColor().equals(requiredColor)) {
                tempCards[count] = carts[i];
                count++;
            }
        }
        return tempCards;
    }

    public Cart[] getCartsWithRank(String requiredRank) {
        Cart[] tempCards = new Cart[4];
        int count = 0;
        for(int i = 0; i < carts.length; i++) {
            if(carts[i].getRank().equals(requiredRank)) {
                tempCards[count] = carts[i];
                count++;
            }
        }
        return tempCards;
    }

    public Cart[] getCartsWithShape(String requiredShape) {
        Cart[] tempCards = new Cart[8];
        int count = 0;
        for(int i = 0; i < carts.length; i++) {
            if(carts[i].getShape().equals(requiredShape)) {
                tempCards[count] = carts[i];
                count++;
            }
        }
        return tempCards;
    }

    public Cart[] getRandomCarts(int count) {
        Cart[] tempCarts = new Cart[count];
        Random random = new Random();
        int tempCounter = 0;
        for(int i = 0; i < carts.length; i++) {
            if(tempCounter < count) {
                tempCarts[tempCounter] = carts[random.nextInt(carts.length)];
                tempCounter++;
            }
        }
        return tempCarts;
    }

    public void printCarts(Cart[] cartsToPrint) {
        for(int i = 0; i < cartsToPrint.length; i++) {
            System.out.println(cartsToPrint[i].printInfo());
        }
    }
}




public class Main {

    public static void main(String[] args) {
	// write your code here
        CartShaker cartShaker = new CartShaker();
        cartShaker.generateAllCart();
        cartShaker.printAllCards();

        System.out.println();

        cartShaker.shakeAllCards();
        cartShaker.printAllCards();

        System.out.println();

        cartShaker.printCarts(cartShaker.getCartsWitchColor("Red"));

        System.out.println();

        cartShaker.printCarts(cartShaker.getCartsWithRank("A"));

        System.out.println();

        cartShaker.printCarts(cartShaker.getCartsWithShape("Hearts"));

        System.out.println();

        cartShaker.printCarts(cartShaker.getRandomCarts(5));

        System.out.println();
    }
}


 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý