Kihagyás

Pub simulator

Pub Simulator

The task presented here is essentially designed to practice object-oriented programming, but only the basics. The task consists of two parts: first, you need to create traditional classes, and inheritance is introduced in the second part.

If you haven't heard about inheritance in class yet, only solve the first part. Before solving the task, read through the entire assignment and create a UML class diagram for it.

Pub Simulator part 1

public class Person {
    private String name;
    private int age;
    private boolean male;

  // constructors

  // getters and setters
}

Let's extend the existing Person class with private money and drunkenness int variables, and an inPub boolean variable. There should be a new constructor that, in addition to the existing parameters, accepts a money parameter, which sets the Person's money. Drunkenness should default to 0, and inPub to false. The Person should have a drink(Bartender bartender) method that expects a Bartender. When this is called, if the person is in the pub, their money should decrease by 1, their drunkenness should increase by 1, they should generate 1 dirty glass, and give 1 money to the bartender received as a parameter. We'll see later where to store the glass and what the Bartender is. If the person is not in the pub, it should display a message about this. There should also be a sleep() method that resets drunkenness to zero and prints that the person fell asleep, a goHome() method that sets inPub to false, and a come() method that sets it to true. Messages should be displayed for these actions as well.

There should also be a Bartender class. They should also have private money, which can be specified in the constructor. All bartenders share the same dirty glasses (static), and there should be a wash() method that reduces the number of dirty glasses by one and prints that they washed a glass. If there are no dirty glasses, it should print accordingly.

There should also be a Drink class with the following private properties: price, alcoholContent.

The Person should also have a drink method with the header drink(Bartender bartender, Drink drink), meaning it can also accept a drink. Then the Person transfers the price of the drink to the Bartender instead of 1. The Person's drunkenness increases with the drink's alcohol content.

If drunkenness reaches 40, the Person should automatically fall asleep with both drink() functions.

All private variables of the classes should have getter and setter methods, and the classes should have meaningful toString methods.

There should be a main function, say in a class called Main, where you write a short run in which you play around a bit with the people, demonstrating a few cases, having them drink, sleep, etc...

Pub Simulator part 2

Let's inherit our existing Bartender class from Person, since bartenders are also people. This automatically gives it the data members (variables) declared in the Person class. Let's write a new parameterized constructor for the Bartender that also calls the constructor of the Person (i.e., the parent) (using the super keyword). Theoretically, the Bartender will work like this, if not, make it operational with further troubleshooting.

Let's also inherit a Student from our Person class, who will be a special person. The student should have a private scholarship variable that indicates how much money they earn by studying. They should have a study() method that increases their money by the amount of their scholarship. They should also have a parameterized study(int howMuch) method with which you can set how much they should study, and they receive money accordingly.

A Bouncer should also derive from Person. They should have a private boolean working data member. They should also have a static variable that will indicate how many bouncers are currently working. This is 0 by default. In the constructor of new bouncer instances, let's include increasing this variable, and always change it appropriately if the value of the working variable can change (setter, startShift(), endShift()). So there should be startShift() and endShift() methods that set the value of working. Let's override the drink function to check if the bouncer is currently working, as they cannot drink alcoholic beverages while on duty, but they can drink ones with 0 alcohol content. If they are not on duty, call the Person (i.e., super) drink function.

The Student and the Bouncer should also have parameterized constructors.

Override the toString() of the Student, Bartender, and Bouncer, call the Person's toString() in it, and also print out other data characteristic of the given class.

The Person should have a pickFight(Person withWhom) method that expects another person as a parameter. We need to check if there is a bouncer on duty; if so, we send the first person home. If not, we write that they had a good fight.

Drinks

Let's inherit a Beer, Wine, and a Mixed class from the Drink class.

The Wine should have a private int vintage variable. The Beer should not have a new variable. These should get appropriate constructors.

The Mixed class should have 2, 3, 4 parameter constructors that expect drinks as parameters, for example: public Mixed(Drink dr1, Drink dr2, Drink dr3, Drink dr4)

These drinks can be simple Drink, Beer, or Wine types; we don't need to handle this separately because inheritance is good for this too, as they can all be treated as Drink.

The constructor should set the alcohol content of the Mixed drink to the average alcohol content of the specified drinks. An example of calling the constructor:

Mixed mixed1 = new Mixed(beer1, beer2, wine1, drink1);

All private data members of each class should have getters and setters.

In the main function, let's also play a bit with the built-in new features.