Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created October 7, 2015 20:06
Show Gist options
  • Save AnnaBoro/f4886520e212b50838d7 to your computer and use it in GitHub Desktop.
Save AnnaBoro/f4886520e212b50838d7 to your computer and use it in GitHub Desktop.
package lesson3;
import java.util.Arrays;
public class Birds {
static String[][] birds = new String[2][4];
public static void main(String[] args) {
initShop();
System.out.println(Arrays.toString(birds[0]));
System.out.println(Arrays.toString(birds[1]));
System.out.println("Quantity Of Duck: " + getQuantityOfBirds("duck"));
System.out.println("Quantity Of Sold Birds: " + getQuantityOfSoldBirds());
System.out.println("Less Than 3: " + isBirdsLessThan3());
soldBirds("eagle", 2);
System.out.println("After sales of 2 eagles: " + Arrays.toString(birds[1]));
changePrice("duck", 40);
System.out.println("After the price change (duck): " + Arrays.toString(birds[0]));
System.out.println("Profit: " + getProfit());
System.out.println("Birds In Stock: " + getBirdsInStock());
}
static void initShop() {
addBirds("duck", 1, 2, 3);
addBirds("eagle", 4, 8, 7);
}
static int getQuantityOfSoldBirds() {
int result = 0;
for (int i = 0; i < birds.length; i++) {
if (birds[i][2] != null) {
result = result + Integer.parseInt(birds[i][2]);
}
}
return result;
}
static int getQuantityOfBirds(String type) {
int result = 0;
for (int i = 0; i < birds.length; i++) {
if (birds[i][0] != null && birds[i][0].equals(type)) {
result = Integer.parseInt(birds[i][1]);
}
}
return result;
}
static String isBirdsLessThan3() {
String result = "";
for (int i = 0; i < birds.length; i++) {
if (birds[i][1] != null && Integer.parseInt(birds[i][1]) < 3) {
result = birds[i][0];
}
}
return result;
}
static void addBirds(String nameOfBirds, int numberOfType, int numberSoldBirds, int priceOfBirds) {
for (int i = 0; i < birds.length; i++) {
if (birds[i][0] == null) {
birds[i][0] = nameOfBirds;
birds[i][1] = String.valueOf(numberOfType);
birds[i][2] = String.valueOf(numberSoldBirds);
birds[i][3] = String.valueOf(priceOfBirds);
return;
}
}
increaseShopVolume();
addBirds(nameOfBirds, numberOfType, numberSoldBirds, priceOfBirds);
}
static void increaseShopVolume() {
String[][] birds2 = new String[birds.length * 2][4];
System.arraycopy(birds, 0, birds2, 0, birds.length);
birds = birds2;
}
static void soldBirds(String nameOfBirds, int soldBirds) {
for (int i = 0; i < birds.length; i++) {
if (birds[i][0].equals(nameOfBirds) ) {
birds[i][1] = String.valueOf(Integer.parseInt(birds[i][1]) - soldBirds);
birds[i][2] = String.valueOf(Integer.parseInt(birds[i][2]) + soldBirds);
}
}
}
static void changePrice(String nameOfBirds, int newPrice) {
for (int i = 0; i < birds.length; i++) {
if (birds[i][0].equals(nameOfBirds) ) {
birds[i][3] = String.valueOf(newPrice);
}
}
}
static int getProfit() {
int profit = 0;
for (int i = 0; i < birds.length; i++) {
if (birds[i][0] != null) {
profit += Integer.parseInt(birds[i][2]) * Integer.parseInt(birds[i][3]);
}
}
return profit;
}
static int getBirdsInStock() {
int birdsInStock = 0;
for (int i = 0; i < birds.length; i++) {
birdsInStock += Integer.parseInt(birds[i][1]);
}
return birdsInStock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment