diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_1.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_1.java new file mode 100644 index 00000000..b48ceea1 --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_1.java @@ -0,0 +1,31 @@ +package ru.tn.courses.vbuvshov.v2.task1; +import java.util.ArrayList; +import java.util.Arrays; +public class subtask_1 +{ + public static void main(String[] args) + { + int arr[] = gArr(0, 4, 0, 7, 0, 32, 1, 6, 0); + + for (int anArr : arr) System.out.print(anArr + " "); + } + + private static int[] gArr(int ... nums) + { + ArrayList Nam = new ArrayList<>(); + for (int i = 0; i < nums.length; i++) + { + if (nums[i] == 0) Nam.add(i); + } + + int arr[] = new int[Nam.size()]; + + for (int i = 0; i < Nam.size(); i++) arr[i] = Nam.get(i); + + return arr; + } + + +} + + diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_2.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_2.java new file mode 100644 index 00000000..097f1660 --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_2.java @@ -0,0 +1,23 @@ +package ru.tn.courses.vbuvshov.v2.task1; +import java.util.Arrays; +public class subtask_2 +{ + public static void main(String args[]) + { + double mas[] = {67.2, 68.05, 65.2, 67, 68.55, 67.45}; + double max = 0; + int Num = 0; + for (int i = 0; i < mas.length; i++) + { + if (mas[i] > max) + { + max = mas[i]; + Num = i; + } + } + + System.out.println("Наилучший курс на данный момент в банке № = "); + System.out.println(Num); + } + +} diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_3.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_3.java new file mode 100644 index 00000000..6bd23f2e --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_3.java @@ -0,0 +1,33 @@ +package ru.tn.courses.vbuvshov.v2.task1; +import java.util.Arrays; +public class subtask_3 +{ + public static void main(String[] args) + { + sortirovka(); + } + + private static void sortirovka() + { + int[] Mas = {41,32,46,7,53,311,1,85,34,6}; + System.out.print("Данный массив = "); + for (int aMas : Mas) System.out.print(aMas + " "); + int count = 0; + System.out.print("счетчик равен "); + System.out.print(count); + for (int i = 0; i < Mas.length - 1; i++) + for (int j = 0; j < Mas.length - i - 1; j++) + if (Mas[j] < Mas[j+1]) + { + int temp = Mas[j]; + Mas[j] = Mas[j+1]; + Mas[j+1] = temp; + count++; + } + System.out.print(" конечный массив = "); + for (int aMas : Mas) System.out.print(aMas + " "); + System.out.print("счетчик равен = "); + System.out.print(count); + } +} + diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/ConvertInterface.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/ConvertInterface.java new file mode 100644 index 00000000..c4b643d9 --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/ConvertInterface.java @@ -0,0 +1,2 @@ +package ru.tn.courses.vbuvshov.v2.task2; +public interface ConvertInterface { String convert();} diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/S_1.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/S_1.java new file mode 100644 index 00000000..54e63834 --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/S_1.java @@ -0,0 +1,16 @@ +package ru.tn.courses.vbuvshov.v2.task2; +public class S_1 { + public static void main(String[] args) { + TheCar car = new TheCarText(0L, TheCar.Type.PassengerCar, "LADA", "Новый автомобиль LADA в наличии."); + print(car); + car = new TheCarJson(1L, TheCar.Type.Truck, "ShockWave 36", "Новый грузовик ShockWave 36 в наличии."); + print(car); + car = new TheCarXml(2L, TheCar.Type.Bike, "Kawasaki Z1000", "Новый мотоцикл Kawasaki Z1000 в наличии."); + print(car); + } + private static String convert(ConvertInterface obj) {return obj.convert();} + private static void print(TheCar theCar) { + if (theCar instanceof TheCarText) {System.out.println("Готово");} + System.out.println(convert(theCar)); + } +} \ No newline at end of file diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCar.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCar.java new file mode 100644 index 00000000..d75739eb --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCar.java @@ -0,0 +1,27 @@ +package ru.tn.courses.vbuvshov.v2.task2; + +public abstract class TheCar implements ru.tn.courses.vbuvshov.v2.task2.ConvertInterface { + private Long id; + private Type type; + private String name; + private String description; + public TheCar() {} + public TheCar(TheCar theCar) { + this(theCar.getId(), theCar.getType(), theCar.getName(), theCar.getDescription()); + } + public TheCar(Long id, Type type, String name, String description) { + this.id = id; + this.type = type; + this.name = name; + this.description = description; + } + public Long getId() {return this.id;} + public Type getType() {return this.type;} + public String getName() {return this.name;} + public String getDescription() {return this.description;} + public TheCar setId(Long id) {this.id = id;return this;} + public TheCar setType(Type type) {this.type = type;return this;} + public TheCar setName(String name) {this.name = name;return this;} + public TheCar setDescription(String description) {this.description = description;return this;} + public enum Type {PassengerCar, Truck, Bike} +} \ No newline at end of file diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarJson.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarJson.java new file mode 100644 index 00000000..dfee5f3a --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarJson.java @@ -0,0 +1,12 @@ +package ru.tn.courses.vbuvshov.v2.task2; + +public class TheCarJson extends ru.tn.courses.vbuvshov.v2.task2.TheCar { + public TheCarJson(Long id, Type type, String name, String description) {super(id, type, name, description);} + public TheCarJson(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);} + @Override + public String convert() { + return new StringBuilder().append("{\n\t\"id\": ").append(getId()).append(",\n") + .append("\t\"type\": \"").append(getType()).append("\",\n").append("\t\"Name\": \"").append(getName()).append("\",\n") + .append("\t\"Description\": \"").append(getDescription()).append("\",\n}").toString(); + } +} diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarText.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarText.java new file mode 100644 index 00000000..47d1428d --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarText.java @@ -0,0 +1,11 @@ +package ru.tn.courses.vbuvshov.v2.task2; +public class TheCarText extends ru.tn.courses.vbuvshov.v2.task2.TheCar { + public TheCarText() {super();} + public TheCarText(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);} + public TheCarText(Long id, Type type, String name, String description) {super(id, type, name, description);} + @Override + public String convert() { + return new StringBuilder().append("ID: ").append(getId()).append("\nType: ").append(getType()) + .append("\nName: ").append(getName()).append("\nDescription: ").append(getDescription()).toString(); + } +} \ No newline at end of file diff --git a/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarXml.java b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarXml.java new file mode 100644 index 00000000..a594c566 --- /dev/null +++ b/src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarXml.java @@ -0,0 +1,23 @@ +package ru.tn.courses.vbuvshov.v2.task2; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.StringWriter; + +@XmlRootElement(name = "the-car") +public class TheCarXml extends ru.tn.courses.vbuvshov.v2.task2.TheCar { + public TheCarXml() {super();} + public TheCarXml(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);} + public TheCarXml(Long id, Type type, String name, String description) {super(id, type, name, description);} + @Override + public String convert() { + try { + StringWriter res = new StringWriter(); + Marshaller marshaller = JAXBContext.newInstance(TheCarXml.class).createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(this, res);return res.toString(); + } catch (JAXBException e) {e.printStackTrace();}return null; + } +}