├── .gitignore └── src └── com └── javalesson ├── interfaces ├── Size.java ├── Deliverable.java ├── Orderable.java ├── Pricable.java ├── CellPhone.java ├── Fridge.java ├── InterfaceRunner.java ├── Electronics.java └── Pizza.java ├── inheritance ├── EngineType.java ├── Piston.java ├── InheritanceMain.java ├── Engine.java ├── FuelAuto.java ├── Truck.java ├── ElectricCar.java ├── Bus.java └── Auto.java ├── innerclasses ├── Main.java ├── Display.java ├── CellPhone.java └── RadioModule.java ├── oop ├── Size.java ├── Main.java └── Dog.java ├── exceptions ├── InvalidInputParamException.java └── ExceptionHandlingMain.java ├── constructors └── ConstructorsMain.java └── domainmodel └── Employee.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | out/ 4 | out.txt 5 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Size.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public enum Size { 4 | 5 | S, M, L, XL; 6 | } 7 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public enum EngineType { 4 | 5 | PETROL, DIESEL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Deliverable.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public interface Deliverable { 4 | 5 | int calcDeliveryPrice(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Orderable.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | 4 | @FunctionalInterface 5 | public interface Orderable { 6 | 7 | int calcOrderPrice(); 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Pricable.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public interface Pricable extends Deliverable, Orderable { 4 | 5 | default int calcPrice(){ 6 | return calcOrderPrice() + calcDeliveryPrice(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/com/javalesson/innerclasses/Main.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.innerclasses; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | CellPhone phone = new CellPhone("Motorola", "XT1575"); 7 | phone.turnOn(); 8 | phone.call("1234567890"); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/com/javalesson/oop/Size.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.oop; 2 | 3 | public enum Size { 4 | 5 | VERY_SMALL("XS"), SMALL("S"), AVERAGE("M"), BIG("L"), VERY_BIG("XL"), UNDEFINED(""); 6 | 7 | Size(String abbreviation){ 8 | this.abbreviation = abbreviation; 9 | } 10 | 11 | private String abbreviation; 12 | 13 | public String getAbbreviation() { 14 | return abbreviation; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/CellPhone.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public class CellPhone extends Electronics { 4 | 5 | public CellPhone(String make, String model, int quantity, int price) { 6 | super(make, model, quantity, price); 7 | } 8 | 9 | @Override 10 | public int calcDeliveryPrice() { 11 | if(getPrice()>=150){ 12 | return 0; 13 | } else { 14 | return 10; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Fridge.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public class Fridge extends Electronics { 4 | 5 | 6 | public Fridge(String make, String model, int quantity, int price) { 7 | super(make, model, quantity, price); 8 | } 9 | 10 | @Override 11 | public int calcDeliveryPrice() { 12 | if (getPrice() >= 300) { 13 | return 0; 14 | } else { 15 | return 25; 16 | } 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/com/javalesson/exceptions/InvalidInputParamException.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.exceptions; 2 | 3 | public class InvalidInputParamException extends RuntimeException { 4 | 5 | public InvalidInputParamException() { 6 | } 7 | 8 | public InvalidInputParamException(String message) { 9 | super(message); 10 | } 11 | 12 | public InvalidInputParamException(String message, Throwable cause) { 13 | super(message, cause); 14 | } 15 | 16 | public InvalidInputParamException(Throwable cause) { 17 | super(cause); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/Piston.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public class Piston { 4 | 5 | private double volume; 6 | private int pistonNumber; 7 | 8 | public Piston(double volume, int pistonNumber) { 9 | this.volume = volume; 10 | this.pistonNumber = pistonNumber; 11 | } 12 | 13 | public void movePiston(){ 14 | System.out.println("Piston #"+pistonNumber+" is moving"); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "Piston{" + 20 | "volume=" + volume + 21 | ", pistonNumber=" + pistonNumber + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/javalesson/constructors/ConstructorsMain.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.constructors; 2 | 3 | import com.javalesson.domainmodel.Employee; 4 | 5 | public class ConstructorsMain { 6 | 7 | public static void main(String[] args) { 8 | 9 | 10 | Employee employee = new Employee(); 11 | Employee employee1 = new Employee(); 12 | Employee employee2 = new Employee(); 13 | // Employee employee2 = new Employee("Alex", "developer", 100); 14 | // Employee employee1 = new Employee("John", "developer", 100); 15 | // 16 | System.out.println(employee); 17 | System.out.println(employee1); 18 | System.out.println(employee2); 19 | 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/InterfaceRunner.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public class InterfaceRunner { 4 | 5 | public static void main(String[] args) { 6 | Pricable pizza = new Pizza("Neapolitana", 1, 20, Size.L); 7 | Pricable phone = new CellPhone("Motorola", "XT1575", 1, 250); 8 | Pricable fridge = new Fridge("LG","E9090", 1, 300); 9 | 10 | 11 | 12 | printDeliveryPrice(pizza); 13 | printDeliveryPrice(phone); 14 | printDeliveryPrice(fridge); 15 | 16 | } 17 | 18 | private static void printDeliveryPrice(Pricable del){ 19 | System.out.println("Delivery prise "+del.calcDeliveryPrice()); 20 | System.out.println("Order prise "+del.calcOrderPrice()); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/InheritanceMain.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public class InheritanceMain { 4 | 5 | public static void main(String[] args) { 6 | Engine truckEngine = new Engine(6.0, EngineType.DIESEL, 900); 7 | Engine busEngine = new Engine(3.5, EngineType.DIESEL, 150); 8 | Auto bus = new Bus("Mercedes", "Sprinter", busEngine, 30, 75, 12); 9 | Auto truck = new Truck("Volvo", "VNL 300", truckEngine, 300, 500, 1000); 10 | Auto car = new ElectricCar("Tesla", "Model S", 4, 100500); 11 | // Auto auto = new Auto("VW", "Polo", busEngine); 12 | 13 | runCar(bus); 14 | runCar(truck); 15 | runCar(car); 16 | // runCar(auto); 17 | 18 | 19 | } 20 | 21 | private static void runCar(Auto auto) { 22 | auto.start(); 23 | auto.stop(); 24 | auto.energize(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/javalesson/interfaces/Electronics.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.interfaces; 2 | 3 | public abstract class Electronics implements Pricable { 4 | 5 | private String make; 6 | private String model; 7 | private int quantity; 8 | private int price; 9 | 10 | public Electronics(String make, String model, int quantity, int price) { 11 | this.make = make; 12 | this.model = model; 13 | this.quantity = quantity; 14 | this.price = price; 15 | } 16 | 17 | public String getMake() { 18 | return make; 19 | } 20 | 21 | public String getModel() { 22 | return model; 23 | } 24 | 25 | public int getQuantity() { 26 | return quantity; 27 | } 28 | 29 | public int getPrice() { 30 | return price; 31 | } 32 | 33 | @Override 34 | public int calcOrderPrice() { 35 | return getQuantity() * getPrice(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/Engine.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Engine { 7 | 8 | private double volume; 9 | private EngineType engineType; 10 | private int power; 11 | private List pistons = new ArrayList<>(); 12 | 13 | public Engine() { 14 | } 15 | 16 | public Engine(double volume, EngineType engineType, int power) { 17 | this.volume = volume; 18 | this.engineType = engineType; 19 | this.power = power; 20 | for(int i=1; i<=5; i++) 21 | this.pistons.add(new Piston(0.3, i)); 22 | } 23 | 24 | public double getVolume() { 25 | return volume; 26 | } 27 | 28 | public EngineType getEngineType() { 29 | return engineType; 30 | } 31 | 32 | public int getPower() { 33 | return power; 34 | } 35 | 36 | public List getPistons() { 37 | return pistons; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/FuelAuto.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | 4 | public abstract class FuelAuto extends Auto { 5 | 6 | private int availablePetrol; 7 | private int tankVolume; 8 | 9 | public FuelAuto(String producer, String model, Engine engine, int availablePetrol, int tankVolume) { 10 | super(producer, model, engine); 11 | this.availablePetrol = availablePetrol; 12 | this.tankVolume = tankVolume; 13 | } 14 | 15 | void fuelUp(int petrolVolume){ 16 | availablePetrol+=petrolVolume; 17 | System.out.println("Adding fuel"); 18 | } 19 | 20 | 21 | 22 | public int getAvailablePetrol() { 23 | return availablePetrol; 24 | } 25 | 26 | public void setAvailablePetrol(int availablePetrol) { 27 | this.availablePetrol = availablePetrol; 28 | } 29 | 30 | public int getTankVolume() { 31 | return tankVolume; 32 | } 33 | 34 | public void setTankVolume(int tankVolume) { 35 | this.tankVolume = tankVolume; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/javalesson/oop/Main.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.oop; 2 | 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | System.out.println("Dog's count "+ Dog.getDogsCount()); 9 | 10 | Dog lab = new Dog(); 11 | lab.setName("Charley"); 12 | lab.setBreed("Lab"); 13 | lab.setSize(Size.AVERAGE); 14 | lab.bite(); 15 | 16 | Dog sheppard = new Dog(); 17 | sheppard.setName("Mike"); 18 | sheppard.setBreed("Sheppard"); 19 | sheppard.setSize(Size.BIG); 20 | sheppard.bite(); 21 | 22 | 23 | Dog doberman = new Dog(); 24 | doberman.setName("Jack"); 25 | doberman.setBreed("Doberman"); 26 | doberman.setSize(Size.BIG); 27 | doberman.bite(); 28 | 29 | 30 | Size s = Size.SMALL; 31 | Size s1 = Size.valueOf("BIG"); 32 | // System.out.println(s1); 33 | Size[] values = Size.values(); 34 | for(int i=0; i 1) 27 | return 0; 28 | else { 29 | return 7; 30 | } 31 | } 32 | 33 | public int getQuantity() { 34 | return quantity; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public double getPrice() { 42 | return price; 43 | } 44 | 45 | public Size getSize() { 46 | return size; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/javalesson/innerclasses/Display.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.innerclasses; 2 | 3 | public class Display { 4 | 5 | private static final int DISPLAY_HEIGHT = 1920; 6 | private static final int DISPLAY_WIDTH = 1280; 7 | private int x = 0; 8 | 9 | public Display() { 10 | Pixel pixel = new Pixel(10, 10, Color.BLUE); 11 | } 12 | 13 | 14 | private class Pixel { 15 | private int x; 16 | private int y; 17 | private Color color; 18 | 19 | private Pixel(int x, int y, Color color) { 20 | if (0 <= x && x <= DISPLAY_WIDTH && 0 <= y && y <= DISPLAY_HEIGHT) { 21 | this.x = x; 22 | this.y = y; 23 | this.color = color; 24 | System.out.println("Creating " + color + " pixel at (" + x + "," + y + ")"); 25 | } else { 26 | throw new IllegalArgumentException("Coordinates x and why should be between 0-" + DISPLAY_WIDTH + " and 0-" + DISPLAY_HEIGHT); 27 | } 28 | } 29 | } 30 | 31 | public enum Color { 32 | RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/Truck.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | 4 | public class Truck extends FuelAuto { 5 | 6 | private int cargoWeight; 7 | 8 | public Truck(String producer, String model, Engine engine, int availablePetrol, int tankVolume, int cargoWeight) { 9 | super(producer, model, engine, availablePetrol, tankVolume); 10 | this.cargoWeight = cargoWeight; 11 | System.out.println("Constructing truck"); 12 | } 13 | 14 | public int getCargoWeight() { 15 | return cargoWeight; 16 | } 17 | 18 | public void setCargoWeight(int cargoWeight) { 19 | this.cargoWeight = cargoWeight; 20 | } 21 | 22 | public void load(){ 23 | System.out.println("Cargo loaded"); 24 | } 25 | 26 | public void unload(){ 27 | System.out.println("Cargo unloaded"); 28 | } 29 | 30 | @Override 31 | public void start() { 32 | isRunning = true; 33 | setCurrentSpeed(10); 34 | System.out.println("Truck is starting"); 35 | } 36 | 37 | @Override 38 | public void stop() { 39 | isRunning = false; 40 | setCurrentSpeed(0); 41 | System.out.println("Truck has stopped"); 42 | } 43 | 44 | @Override 45 | public void energize() { 46 | fuelUp(getTankVolume() - getAvailablePetrol()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/javalesson/innerclasses/CellPhone.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.innerclasses; 2 | 3 | public class CellPhone { 4 | 5 | private String make; 6 | private String model; 7 | private Display display; 8 | private RadioModule gsm; 9 | private AbstractPhoneButton button; 10 | 11 | public interface AbstractPhoneButton{ 12 | void click(); 13 | } 14 | 15 | public CellPhone(String make, String model) { 16 | this.make = make; 17 | this.model = model; 18 | } 19 | 20 | 21 | public void turnOn(){ 22 | initDisplay(); 23 | gsm = new RadioModule(); 24 | initButton(); 25 | } 26 | 27 | public void initButton(){ 28 | button = new AbstractPhoneButton() { 29 | @Override 30 | public void click() { 31 | System.out.println("Button clicked"); 32 | } 33 | }; 34 | } 35 | 36 | public void call(String number){ 37 | button.click(); 38 | gsm.call(number); 39 | } 40 | 41 | private void initDisplay(){ 42 | display = new Display(); 43 | } 44 | 45 | public String getMake() { 46 | return make; 47 | } 48 | 49 | public String getModel() { 50 | return model; 51 | } 52 | 53 | public Display getDisplay() { 54 | return display; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/ElectricCar.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public class ElectricCar extends Auto{ 4 | 5 | private int batteryVolume; 6 | private int passengersNumber; 7 | 8 | public ElectricCar(String producer, String model, int batteryVolume, int passengersNumber) { 9 | super(producer, model, new Engine()); 10 | this.batteryVolume = batteryVolume; 11 | this.passengersNumber = passengersNumber; 12 | } 13 | 14 | private void charge(){ 15 | System.out.println("Battery is charging"); 16 | } 17 | 18 | public int getBatteryVolume() { 19 | return batteryVolume; 20 | } 21 | 22 | public void setBatteryVolume(int batteryVolume) { 23 | this.batteryVolume = batteryVolume; 24 | } 25 | 26 | public int getPassengersNumber() { 27 | return passengersNumber; 28 | } 29 | 30 | public void setPassengersNumber(int passengersNumber) { 31 | this.passengersNumber = passengersNumber; 32 | } 33 | 34 | @Override 35 | public void energize() { 36 | charge(); 37 | } 38 | 39 | @Override 40 | public void start() { 41 | isRunning = true; 42 | setCurrentSpeed(10); 43 | System.out.println("Car is starting"); 44 | } 45 | 46 | @Override 47 | public void stop() { 48 | isRunning = false; 49 | setCurrentSpeed(0); 50 | System.out.println("Car has stopped"); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/com/javalesson/innerclasses/RadioModule.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.innerclasses; 2 | 3 | public class RadioModule { 4 | 5 | public RadioModule(){ 6 | System.out.println("RadioModule initialized"); 7 | } 8 | 9 | public void call(String number){ 10 | int length = 10; 11 | 12 | class GSMModule{ 13 | private String phoneNumber; 14 | private int validNumber; 15 | 16 | public GSMModule(String phoneNumber) { 17 | this.phoneNumber = phoneNumber; 18 | } 19 | 20 | public boolean isValid(){ 21 | if(phoneNumber.length()!=length){ 22 | return false; 23 | } else { 24 | try{ 25 | validNumber = Integer.parseInt(phoneNumber); 26 | return true; 27 | } catch (NumberFormatException e){ 28 | return false; 29 | } 30 | } 31 | } 32 | 33 | public void dialIn(){ 34 | if(isValid()){ 35 | System.out.println("Calling phone number "+validNumber); 36 | } else { 37 | System.out.println("Phone number is invalid. Please correct phone number"); 38 | } 39 | } 40 | } 41 | 42 | GSMModule module = new GSMModule(number); 43 | module.dialIn(); 44 | 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/javalesson/oop/Dog.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.oop; 2 | 3 | public class Dog { 4 | 5 | private static int dogsCount; 6 | 7 | public static final int PAWS = 4; 8 | public static final int TAIL = 1; 9 | private String name; 10 | private String breed; 11 | private Size size = Size.UNDEFINED; 12 | 13 | public Dog() { 14 | dogsCount++; 15 | System.out.println("Dog's count is " + dogsCount); 16 | } 17 | 18 | public static int getDogsCount() { 19 | return dogsCount; 20 | } 21 | 22 | public Size getSize() { 23 | return size; 24 | } 25 | 26 | public void setSize(Size size) { 27 | this.size = size; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | 39 | public String getBreed() { 40 | return breed; 41 | } 42 | 43 | public void setBreed(String breed) { 44 | this.breed = breed; 45 | } 46 | 47 | public void bark() { 48 | switch (size) { 49 | case BIG: 50 | case VERY_BIG: 51 | System.out.println("Wof - Wof"); 52 | break; 53 | case AVERAGE: 54 | System.out.println("Raf - Raf"); 55 | break; 56 | case SMALL: 57 | case VERY_SMALL: 58 | System.out.println("Tiaf - tiaf "); 59 | break; 60 | default: 61 | System.out.println("Dog's size is undefined"); 62 | } 63 | } 64 | 65 | public void bite() { 66 | if (dogsCount > 2) { 67 | System.out.println("Dogs are biting you"); 68 | } else { 69 | bark(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/com/javalesson/domainmodel/Employee.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.domainmodel; 2 | 3 | public class Employee { 4 | 5 | private static int id; 6 | private int employeeId; 7 | private String name; 8 | private String position; 9 | private int salary; 10 | private String department; 11 | 12 | 13 | static { 14 | id = 1001; 15 | System.out.println("Static init block called"); 16 | } 17 | 18 | { 19 | department = "IT"; 20 | System.out.println("Non-static init block called"); 21 | } 22 | 23 | 24 | public Employee() { 25 | this("A", "B",1); 26 | System.out.println("Empty constructor called"); 27 | } 28 | 29 | public Employee(String name, String position, int salary){ 30 | this(name, position, salary, "IT"); 31 | System.out.println("Constructor with 3 params called"); 32 | } 33 | 34 | private Employee(String name, String position, int salary, String department) { 35 | employeeId = id++; 36 | this.name = name; 37 | this.position = position; 38 | this.salary = salary; 39 | this.department = department; 40 | System.out.println("Constructor with 4 params called"); 41 | } 42 | 43 | public int getEmployeeId() { 44 | return employeeId; 45 | } 46 | 47 | public static int getId() { 48 | return id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public String getPosition() { 56 | return position; 57 | } 58 | 59 | public int getSalary() { 60 | return salary; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return "Employee{" + 66 | "employeeId=" + employeeId + 67 | ", name='" + name + '\'' + 68 | ", position='" + position + '\'' + 69 | ", salary=" + salary + 70 | '}'; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/Bus.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public class Bus extends FuelAuto { 4 | 5 | private int passengerNumber; 6 | 7 | public Bus(String producer, String model, Engine engine, int availablePetrol, int tankVolume, int passengerNumber) { 8 | super(producer, model, engine, availablePetrol, tankVolume); 9 | this.passengerNumber = passengerNumber; 10 | System.out.println("Bus was initialized"); 11 | } 12 | 13 | public void fuelUp(){ 14 | int volume = getTankVolume() - getAvailablePetrol(); 15 | fuelUp(volume); 16 | } 17 | 18 | @Override 19 | public void start() { 20 | isRunning = true; 21 | setCurrentSpeed(10); 22 | System.out.println("Bus is starting"); 23 | } 24 | 25 | @Override 26 | public void stop() { 27 | isRunning = false; 28 | setCurrentSpeed(0); 29 | System.out.println("Bus has stopped"); 30 | } 31 | 32 | @Override 33 | public void fuelUp(int petrolVolume) { 34 | int volume = getAvailablePetrol()+petrolVolume; 35 | if(volume>getTankVolume()){ 36 | setAvailablePetrol(getTankVolume()); 37 | } 38 | System.out.println("Adding DIESEL"); 39 | } 40 | 41 | public int getPassengerNumber() { 42 | return passengerNumber; 43 | } 44 | 45 | public void setPassengerNumber(int passengerNumber) { 46 | this.passengerNumber = passengerNumber; 47 | } 48 | 49 | public void pickUpPassengers(int passengerNum){ 50 | this.passengerNumber+=passengerNum; 51 | System.out.println("Picking up "+passengerNum+" passengers"); 52 | } 53 | 54 | public void releasePassengers(){ 55 | if(isRunning){ 56 | stop(); 57 | } 58 | passengerNumber = 0; 59 | System.out.println("Passengers released"); 60 | } 61 | 62 | @Override 63 | public void energize() { 64 | fuelUp(getTankVolume() - getAvailablePetrol()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/javalesson/inheritance/Auto.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.inheritance; 2 | 3 | public abstract class Auto { 4 | 5 | private String producer; 6 | private String model; 7 | private Engine engine; 8 | private int currentSpeed; 9 | protected boolean isRunning; 10 | 11 | public Auto(String producer, String model, Engine engine) { 12 | this.producer = producer; 13 | this.model = model; 14 | this.engine = engine; 15 | System.out.println("Auto was initialized"); 16 | } 17 | 18 | public abstract void energize(); 19 | 20 | public static void doSmth(){} 21 | 22 | public void start() { 23 | isRunning = true; 24 | currentSpeed = 10; 25 | System.out.println("Auto is starting"); 26 | } 27 | 28 | public void stop() { 29 | isRunning = false; 30 | currentSpeed = 0; 31 | System.out.println("Auto has stopped"); 32 | } 33 | 34 | public void accelerate(int kmPerHour) { 35 | currentSpeed += kmPerHour; 36 | System.out.println("Accelerating. Current speed is " + currentSpeed + " kmPerHour"); 37 | } 38 | 39 | public String getProducer() { 40 | return producer; 41 | } 42 | 43 | public String getModel() { 44 | return model; 45 | } 46 | 47 | public int getCurrentSpeed() { 48 | return currentSpeed; 49 | } 50 | 51 | public boolean isRunning() { 52 | return isRunning; 53 | } 54 | 55 | public Engine getEngine() { 56 | return engine; 57 | } 58 | 59 | public void setCurrentSpeed(int currentSpeed) { 60 | this.currentSpeed = currentSpeed; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return "Auto{" + 66 | "producer='" + producer + '\'' + 67 | ", model='" + model + '\'' + 68 | ", engine=" + engine + 69 | ", currentSpeed=" + currentSpeed + 70 | ", isRunning=" + isRunning + 71 | '}'; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/com/javalesson/exceptions/ExceptionHandlingMain.java: -------------------------------------------------------------------------------- 1 | package com.javalesson.exceptions; 2 | 3 | import java.io.*; 4 | import java.util.ArrayList; 5 | import java.util.InputMismatchException; 6 | import java.util.List; 7 | import java.util.Scanner; 8 | 9 | public class ExceptionHandlingMain { 10 | public static void main(String[] args) { 11 | 12 | try { 13 | doEverything(); 14 | } catch (InvalidInputParamException e) { 15 | System.out.println("InvalidInputParamException"); 16 | e.printStackTrace(); 17 | } 18 | } 19 | 20 | private static void doEverything() { 21 | Scanner scanner = new Scanner(System.in); 22 | boolean continueLoop = true; 23 | do { 24 | try (PrintWriter writer = new PrintWriter(new FileWriter("out.txt"))) { 25 | System.out.println("Please enter numerator"); 26 | int numerator = scanner.nextInt(); 27 | System.out.println("Please enter denominator"); 28 | int denominator = scanner.nextInt(); 29 | System.out.println(divide(numerator, denominator)); 30 | // int[] intArray = new int[1]; 31 | // int i = intArray[2]; 32 | writer.println("Result = " + divide(numerator, denominator)); 33 | continueLoop = false; 34 | } catch (ArithmeticException | InputMismatchException e) { 35 | System.out.println("Exception : " + e); 36 | scanner.nextLine(); 37 | System.out.println("Only integer non-zero parameters allowed"); 38 | } catch (IOException e) { 39 | System.out.println("Unable to open file"); 40 | e.printStackTrace(); 41 | } catch (IndexOutOfBoundsException e) { 42 | System.out.println("All Exceptions here"); 43 | throw new InvalidInputParamException("Index out of bound. thrown in doEverything " + e); 44 | } finally { 45 | System.out.println("Finally block called"); 46 | } 47 | System.out.println("Try catch block finished"); 48 | } while (continueLoop); 49 | } 50 | 51 | private static int divide(int numerator, int denominator) throws ArithmeticException, NullPointerException { 52 | return numerator / denominator; 53 | } 54 | 55 | private void doSMthing() { 56 | long l = 12345678910L; //Если не указывать L литерал, то возникает ошибка Integer number to long. 57 | // Т.е. здесь нужно указывать, так как компилятор воспринимает число как интеджер. 58 | double d = 0.12345645; // здесь можно не указывать. 59 | float f = 0.12345645f; // Тут нужно указывать, иначе будет ошибка о несовместимости типов. 60 | 61 | 62 | 63 | } 64 | 65 | 66 | } 67 | --------------------------------------------------------------------------------