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
12 changes: 7 additions & 5 deletions src/edu/androidclub/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,32 @@ private void test() {
// Создадим нашу витрину (коробку продуктов) на основании схемы
ItemBox itemBox = new ProductsBox(itemScheme);

VendorScreen screen = new VendorScreen();

// Протестируем схему - заставим витрину выдавать объекты на заданных координатах
System.out.println( // Напечатать в консоль
itemBox.emit( // Выдать предмет
new Coordinates(1, 1) // Указываем координату
)
.getName() // Получить имя предмета
); // OK
System.out.println(itemBox.emit(new Coordinates(1, 1)).getName()); // OK
System.out.println(itemBox.emit(new Coordinates(1, 1)).getName()); // OK
screen.printText(itemBox.emit(new Coordinates(1, 1)).toString());
screen.printText(itemBox.emit(new Coordinates(1, 1)).toString());
screen.printText(itemBox.emit(new Coordinates(1, 1)).toString());

System.out.println(itemBox.emit(new Coordinates(1, 1)).getName()); // FAIL - предметы кончились в ячейке
}

// Опишем Колу как подкласс Предмета
public static class Cola extends Item {
public Cola() {
super("Cola"); // Вызов конструктора класса-родителя (класса Item)
super("Cola", 10); // Вызов конструктора класса-родителя (класса Item)
}
}

// Опишем Спрайт как подкласс Предмета
public static class Sprite extends Item {
public Sprite() {
super("Sprite");
super("Sprite", 10);
}
}
}
23 changes: 18 additions & 5 deletions src/edu/androidclub/ProductsBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,27 @@ public ProductsBox(ItemInfoScheme scheme) {
public Item emit(Coordinates coordinates) {
// Получить стопку предметов на позиции
Stack<Item> items = scheme.get(coordinates);
if(hasItem(items)) {
// Возвращаем верхний элемент и удаляем его с вершины стопки
return items.pop();

// Если пусто - возвращаем пустоту
if (items.empty()) {
return null;
}
Item noItem = new noItem();
return noItem;
}

@Override
public boolean hasItem(Stack<Item> items) {
if(items.empty()) {
return false;
}
return true;
}

// Возвращаем верхний элемент и удаляем его с вершины стопки
return items.pop();
public static class noItem extends Item{
public noItem(){
super("There is no items", 0);
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/edu/androidclub/VendorScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.androidclub;

import edu.androidclub.domain.AppScreen;

public class VendorScreen implements AppScreen {
@Override
public void printText(String text) {
String name = text.substring(0, text.lastIndexOf(" "));
String cost = text.substring(text.lastIndexOf(" ")+1, text.length());
System.out.println(" ____________________________________________ ");
System.out.println("||| Name: |||||||||||||||||||| Cost: |||||||");
System.out.println(" -------------------------------------------- ");
System.out.print("||| "); System.out.printf("%-18.18s", name);
System.out.print("||||||||| "); System.out.printf( "%-5.5s", cost); System.out.println(" |||||||");
System.out.println(" -------------------------------------------- ");
}

@Override
public void clear() {
printText(" ");
}
}
8 changes: 7 additions & 1 deletion src/edu/androidclub/domain/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
// Он тоже неизменяемый - т.к. все поля final
public abstract class Item {
private final String name;
private final int cost;

public Item(String name) {
public Item(String name, int cost) {
this.name = name;
this.cost = cost;
}

// Получить имя
public String getName() {
return name;
}

public String toString(){
return name + " " + cost;
}
}
3 changes: 3 additions & 0 deletions src/edu/androidclub/domain/ItemBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import edu.androidclub.Coordinates;

import java.util.Stack;

/*
Интерфейс, описывающий функционал (что умеет) нашей витрины (коробки с продуктами)
* */
public interface ItemBox {

/* Выдать предмет по его координатам */
Item emit(Coordinates coordinates);
boolean hasItem(Stack<Item> items);

}