Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/ru/skypro/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
package ru.skypro;

import ru.skypro.products.Recept;
import ru.skypro.products.Tovar;

import java.util.*;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args){
Map<String,String> TelefonniiSpravochnik = new HashMap<>();
for (int i=0;i<=20;i++) {
TelefonniiSpravochnik.put("f"+i+" i"+i,i+""+i);
}
System.out.println(TelefonniiSpravochnik);
//Part2
Set<Tovar> SpisokTovarov = new HashSet<>();
Tovar Banan = new Tovar("Банан", 120,10);
Tovar Apelsin = new Tovar("Апельсин", 240,10);
try {
Tovar test = new Tovar("Тест", 240,null);
} catch (RuntimeException e){
System.out.println(e);
}
try {
addToSet(SpisokTovarov,Banan);
addToSet(SpisokTovarov,Apelsin);
addToSet(SpisokTovarov,Banan);
} catch (RuntimeException e){
System.out.println(e);
}
try {
remFromSet(SpisokTovarov,Banan);
remFromSet(SpisokTovarov,Banan);
} catch (RuntimeException e){
System.out.println(e);
}
System.out.println(List.of(SpisokTovarov.toArray()));
addToSet(SpisokTovarov,Banan);
Set<Recept> SpisokReceptov = new HashSet<>();
Recept recept1 = new Recept("Рецепт 1");
Recept recept2 = new Recept("Рецепт 2");
recept1.addToSet(Banan,2);
recept1.addToSet(Apelsin,1);
recept2.addToSet(Banan,1);
recept2.addToSet(Apelsin,3);
try {
addToSet(SpisokReceptov, recept1);
addToSet(SpisokReceptov, recept2);
addToSet(SpisokReceptov, recept1);
} catch (RuntimeException e){
System.out.println(e);
}
System.out.println(List.of(SpisokReceptov.toArray()));
//part3
collection.put("str1",2);
addToCollection("str2",1);
try {
addToCollection("str1",2);
} catch (RuntimeException e){
System.out.println(e);
}
addToCollection("str1",5);
System.out.println(collection);
//part 2 1
var r= new Random();
for (int i = 0; i<5;i++){
String key = "key"+i;
List<Integer> list = new ArrayList<>();
for (int j = 0; j<3;j++){
list.add(r.nextInt(1000));
}
collection2.put(key,list);
}
System.out.println(collection2);
Map<String,Integer> newCollection = collection2.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey,e->e.getValue().stream().mapToInt(i-> i).sum())
);
System.out.println(newCollection);
//part 2 2
Map<Integer, String> orderedHashMap = new LinkedHashMap<>();
for (int i = 0; i<10;i++){
orderedHashMap.put(i,"text"+i+" asd");
}
for (Map.Entry<Integer, String> el:orderedHashMap.entrySet()) {
System.out.println(el.getKey()+":"+el.getValue());
}
}
static Map<String, List<Integer>> collection2 = new HashMap<>();
static Map<String, Integer> collection = new HashMap<>();
private static void addToCollection(String key, Integer val ){
if (!collection.containsKey(key)){
collection.put(key,val);
} else if (Objects.equals(collection.get(key), val)) {
throw new RuntimeException();
} else {
collection.replace(key,val);
}

}

private static <T> void addToSet(Set<T> set, T val){
if (!set.add(val)){
throw new RuntimeException("необходимо выбросить исключение");
}
}
private static <T> void remFromSet(Set<T> set, T val) {
if (!set.remove(val)) {
throw new RuntimeException("Ошибка удаления товара");
}
}

}
46 changes: 46 additions & 0 deletions src/ru/skypro/Passport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.skypro;

import java.util.Objects;

public class Passport {
private final String NomerPasporta;
private final String Familia;
private final String Imya;
private final String Otchestvo;
private final String DataRozhdenia;

public Passport(String nomerPasporta, String familia, String imya, String otchestvo, String dataRozhdenia) {
NomerPasporta = nomerPasporta;
Familia = familia;
Imya = imya;
Otchestvo = otchestvo;
DataRozhdenia = dataRozhdenia;
}

public String getNomerPasporta() {
return NomerPasporta;
}

@Override
public String toString() {
return "Passport{" +
"НомерПаспорта='" + NomerPasporta + '\'' +
", Фамилия='" + Familia + '\'' +
", Имя='" + Imya + '\'' +
", Отчество='" + Otchestvo + '\'' +
", ДатаРождения='" + DataRozhdenia + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Passport passport)) return false;
return Objects.equals(NomerPasporta, passport.NomerPasporta);
}

@Override
public int hashCode() {
return Objects.hash(NomerPasporta);
}
}
52 changes: 52 additions & 0 deletions src/ru/skypro/products/Recept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.skypro.products;

import java.util.*;

public class Recept {

Map<Tovar,Integer> mnozhestvoProductov = new HashMap<>();
private final String name;
public Integer sumCostTovar(){
int i=0;
for (Map.Entry<Tovar,Integer> tovar: mnozhestvoProductov.entrySet()) {

i+=tovar.getKey().getCost()*tovar.getValue();
}
return i;
}

public Recept(String name) {
this.name = name;
}

@Override
public String toString() {
return "Рецепт{" +
"МножествоПродуктов=" + mnozhestvoProductov +
", Название='" + name + '\'' +
", СуммарнаяСтоимостьПродуктов='" + sumCostTovar() + '\'' +
'}';
}

public void addToSet(Tovar val, Integer count){
if (count<1){
throw new RuntimeException("Количество продукта в рецепте не может быть меньше 1.");
}
mnozhestvoProductov.put(val,count);
}
public void remFromSet(Tovar val){
mnozhestvoProductov.remove(val);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Recept recept)) return false;
return Objects.equals(mnozhestvoProductov, recept.mnozhestvoProductov) && Objects.equals(name, recept.name);
}

@Override
public int hashCode() {
return Objects.hash(mnozhestvoProductov, name);
}
}
52 changes: 52 additions & 0 deletions src/ru/skypro/products/Tovar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.skypro.products;

import java.util.Objects;

public class Tovar {

private final String name;
private final Integer cost;
private final Integer weight;

public Tovar(String name, Integer cost, Integer weight) {
if (name == null|| cost == null || weight == null){
throw new RuntimeException("Заполните карточку товара полностью");
}
this.name = name;
this.cost = cost;
this.weight = weight;
}

@Override
public String toString() {
return "Товар{" +
"Название='" + name + '\'' +
", Цена=" + cost +
", ВесВКг=" + weight +
'}';
}

public String getName() {
return name;
}

public Integer getCost() {
return cost;
}

public Integer getWeight() {
return weight;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tovar tovar)) return false;
return Objects.equals(cost, tovar.cost) && Objects.equals(weight, tovar.weight);
}

@Override
public int hashCode() {
return Objects.hash(cost, weight);
}
}