Skip to content

Commit

Permalink
3. Массивы и модели;
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Nov 28, 2023
1 parent a67c211 commit d794091
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/ru/j4j/pojo/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.j4j.pojo;

public class Book {
private final String nameBook;
private final int countPages;

public Book(String nameBook, int countPages) {
this.nameBook = nameBook;
this.countPages = countPages;
}

public String getNameBook() {
return nameBook;
}

public int getCountPages() {
return countPages;
}
}
34 changes: 34 additions & 0 deletions src/main/java/ru/j4j/pojo/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.j4j.pojo;

public class Library {
public static void main(String[] args) {
Book one = new Book("Clean code", 800);
Book two = new Book("Java", 1600);
Book three = new Book("PMBOK", 1200);
Book four = new Book("SCRUM", 15200);

Book[] books = new Book[4];
books[0] = one;
books[1] = two;
books[2] = three;
books[3] = four;

for (Book book : books) {
System.out.printf("Book: %s, pages: %s;" + System.lineSeparator(), book.getNameBook(), book.getCountPages());
}
System.out.println("Replace book with index 0 and 3");
Book temp = books[0];
books[0] = books[3];
books[3] = temp;

for (Book book : books) {
System.out.printf("Book: %s, pages: %s" + System.lineSeparator(), book.getNameBook(), book.getCountPages());
}
System.out.println("Find book \"Clean code\"");
for (Book book : books) {
if (book.getNameBook().equals("Clean code")) {
System.out.printf("Book: %s, pages: %s" + System.lineSeparator(), book.getNameBook(), book.getCountPages());
}
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/ru/j4j/pojo/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.j4j.pojo;

public class Product {
private final String name;
private final int count;

public Product(String name, int count) {
this.name = name;
this.count = count;
}

public String getName() {
return name;
}

public int getCount() {
return count;
}
}
30 changes: 30 additions & 0 deletions src/main/java/ru/j4j/pojo/Store.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.j4j.pojo;

public class Store {
public static void main(String[] args) {
Product milk = new Product("Milk", 10);
Product bread = new Product("Bread", 4);
Product egg = new Product("Egg", 19);

Product[] prods = new Product[3];
prods[0] = milk;
prods[1] = bread;
prods[2] = egg;

for (Product pr : prods) {
System.out.println(pr.getName() + " - " + pr.getCount());
}
System.out.println("Replace milk to oil.");
Product oil = new Product("Oil", 11);
prods[0] = oil;
for (Product pr : prods) {
System.out.println(pr.getName() + " - " + pr.getCount());
}
System.out.println("Shown only product.count > 10");
for (Product pr : prods) {
if (pr.getCount() > 10) {
System.out.println(pr.getName() + " - " + pr.getCount());
}
}
}
}

0 comments on commit d794091

Please sign in to comment.