Skip to content

Instantly share code, notes, and snippets.

@DmytroMitin
Last active August 29, 2015 14:25
Show Gist options
  • Save DmytroMitin/8bb9dd55694030cf11a3 to your computer and use it in GitHub Desktop.
Save DmytroMitin/8bb9dd55694030cf11a3 to your computer and use it in GitHub Desktop.
public class Birds {
static int numberOfBirdTypes = 0;
static String[][] birds = new String[numberOfBirdTypes][4];
public static void main(String[] args) {
printBirdInfo(); // array is empty
addBirdType("Eagle"); // initializing
printBirdInfo("Eagle");
setPrice("Eagle", 5);
printBirdInfo("Eagle");
addBirds("Eagle", 20);
printBirdInfo("Eagle");
addBirdType("Duck");
setPrice("Duck", 2);
addBirds("Duck", 10);
printBirdInfo("Duck");
addBirdType("Duck"); // once again
addBirdType("Woodpecker");
setPrice("Woodpecker", 30);
addBirds("Woodpecker", 5);
printBirdInfo("Woodpecker");
sellBirds("Eagle", 18);
printBirdInfo("Eagle");
sellBirds("Duck", 8);
printBirdInfo("Duck");
sellBirds("Woodpecker", 100);
printBirdInfo("Woodpecker");
printBirdInfo("hgcihgcgh");
System.out.println("Birds sold totally: " + calcBirdsSoldTotally());
System.out.println("Profit: $" + calcProfit());
System.out.println("Birds less than 3: " + findBirdsLessThan3());
System.out.println("Birds total: " + birdsTotal());
printBirdInfo();
}
static void addBirdType(String birdType) {
for (String[] i : birds) {
if (i[0].equals(birdType)) {
System.out.println("This bird type already exists!");
return;
}
}
numberOfBirdTypes ++;
String[][] birdsNew = new String[numberOfBirdTypes][4];
for (int i = 0; i < birds.length; i++) {
birdsNew[i][0] = birds[i][0];
birdsNew[i][1] = birds[i][1];
birdsNew[i][2] = birds[i][2];
birdsNew[i][3] = birds[i][3];
}
birdsNew[birds.length][0] = birdType;
birdsNew[birds.length][1] = "0";
birdsNew[birds.length][2] = null;
birdsNew[birds.length][3] = "0";
birds = birdsNew;
}
static int getBirdIndex(String birdType) {
for (int i = 0; i < birds.length; i++) {
if (birds[i][0].equals(birdType)) {
return i;
}
}
System.out.println("There is no such bird type!");
return -1;
}
static void setPrice(String birdType, int price) {
int i = getBirdIndex(birdType);
birds[i][2] = String.valueOf(price);
}
static void addBirds(String birdType, int num) {
int i = getBirdIndex(birdType);
birds[i][1] = String.valueOf(Integer.valueOf(birds[i][1]) + num);
}
static int calcBirdsSoldTotally() {
int sum = 0;
for (String[] i : birds) {
sum += Integer.valueOf(i[3]);
}
return sum;
}
static String findBirdsLessThan3() {
String result = "";
for (String[] i : birds) {
if (Integer.valueOf(i[1]) < 3) {
result = result + i[0] + " ";
}
}
return result;
}
static void sellBirds(String birdType, int num) {
int i = getBirdIndex(birdType);
if (num <= Integer.valueOf(birds[i][1])) {
birds[i][1] = String.valueOf(Integer.valueOf(birds[i][1]) - num);
birds[i][3] = String.valueOf(Integer.valueOf(birds[i][3]) + num);
} else {
System.out.println("Not enough birds of this type!");
}
}
static void printBirdInfo(String birdType) {
int i = getBirdIndex(birdType);
if (i != -1) {
System.out.println("Bird type: " + birds[i][0] + ", number: "
+ birds[i][1] + ", price: $" + birds[i][2]
+ ", birds sold: " + birds[i][3]);
}
}
static void printBirdInfo() {
if (birds == null || birds.length == 0) {
System.out.println("There are no birds in array!");
} else {
System.out.println("*************************");
for (String[] i : birds) {
System.out.println("Bird type: " + i[0] + ", number: " + i[1]
+ ", price: $" + i[2] + ", birds sold: " + i[3]);
}
System.out.println("*************************");
}
}
static int calcProfit() {
int sum = 0;
for (String[] i : birds) {
sum += Integer.valueOf(i[3]) * Integer.valueOf(i[2]);
}
return sum;
}
static int birdsTotal() {
int sum = 0;
for (String[] i : birds) {
sum += Integer.valueOf(i[1]);
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment