Created
October 4, 2015 19:35
-
-
Save andrewZee/f8203d0343ef16a3fb44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Birds; | |
public class Birds { | |
static String[][] store = new String[100][3]; | |
static String[][] sales = new String[1000][4]; | |
static int storeLength = 0; | |
static int salesLength = 0; | |
public static void main(String[] args) { | |
initializer(); | |
System.out.println("Report1: Printing store (less then 3):"); | |
printStore(3); | |
System.out.println(); | |
System.out.println("Report2: How many birds was saled:"); | |
System.out.println(totalBirdSaled()); | |
System.out.println(); | |
System.out.println("Report3: How many Eagles in the store:"); | |
System.out.println(birdAmount("Eagle")); | |
System.out.println(); | |
System.out.println("Report4: Total income:"); | |
System.out.println(totalIncome() + "$"); | |
System.out.println(); | |
System.out.println("Report5: month №6 sales:"); | |
System.out.println(monthIncome(6)); | |
System.out.println(); | |
} | |
static void addToStore(String birdName, int amount){ | |
int i = getBirdId(birdName); | |
if (i>=0){ | |
//add amount (only change) | |
store[i][2]=String.valueOf(Integer.valueOf(store[i][2])+amount); | |
} else { | |
//add row | |
store[storeLength][0]=birdName; | |
store[storeLength][1]="0"; | |
store[storeLength][2]=String.valueOf(amount); | |
storeLength++; | |
} | |
} | |
static int getBirdId(String birdName){ | |
for (int i = 0; i<storeLength;i++){ | |
if (store[i][0].equals(birdName)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
static void changePrice(String birdName, int newPrice){ | |
int i = getBirdId(birdName); | |
if (i>=0) { | |
store[i][1] = String.valueOf(newPrice); | |
} | |
} | |
static void initializer(){ | |
System.out.println("Initial data for store:"); | |
addToStore("Duck",5); changePrice("Duck",30); | |
addToStore("Eagle",3); changePrice("Eagle",40); | |
addToStore("Parrot",8); changePrice("Parrot",20); | |
addToStore("Chiken",2); changePrice("Chiken",10); | |
addToStore("Pinguine",6); changePrice("Pinguine",50); | |
addToStore("Parrot",2); | |
addToStore("Chiken",8); | |
printStore(0); | |
System.out.println(); | |
System.out.println("Change price for Duck (set 25):"); | |
changePrice("Duck",25); | |
printStore(0); | |
System.out.println(); | |
System.out.println("Initial data for sales:"); | |
System.out.println(makeSale("Duck",2,20150604)); | |
System.out.println(makeSale("Duck",3,20150605)); | |
System.out.println(makeSale("Duck",1,20150606)); | |
System.out.println(makeSale("Pinguine",1,20150707)); | |
System.out.println(makeSale("dfgerkguerg",10,20150808)); | |
System.out.println(); | |
System.out.println("Printing all store:"); | |
printStore(0); | |
System.out.println(); | |
System.out.println("Printing all sales:"); | |
printSales(); | |
System.out.println(); | |
} | |
static void printStore(int lessThen) { | |
for (int i = 0; i < storeLength; i++) { | |
if (lessThen == 0 || Integer.valueOf(store[i][2]) < lessThen) { | |
System.out.println(store[i][0] + " " + store[i][1] + " " + store[i][2]); | |
} | |
} | |
} | |
static void printSales(){ | |
for (int i = 0; i < salesLength;i++){ | |
System.out.println(sales[i][0] + " "+ sales[i][1] + " " + sales[i][2] + " " + sales[i][3]); | |
} | |
} | |
static String makeSale(String birdName, int amount, int date){ | |
int i = getBirdId(birdName); | |
if (i>=0) { | |
if (Integer.valueOf(store[i][2])>=amount){ | |
store[i][2]=String.valueOf(Integer.valueOf(store[i][2])-amount); | |
sales[salesLength][0] = String.valueOf(date); | |
sales[salesLength][1] = birdName; | |
sales[salesLength][2] = String.valueOf(amount); | |
sales[salesLength][3] = String.valueOf(Integer.valueOf(store[i][1])*amount); | |
salesLength++; | |
return amount + " " + birdName + " was saled successfuly." ; | |
} else { | |
return "Not anought " + birdName + "s in the store. Sale cancelled!"; | |
} | |
} | |
return "Unknown bird name: " + birdName + "."; | |
} | |
static int totalBirdSaled(){ | |
int sum = 0; | |
for (int i = 0; i < salesLength;i++){ | |
sum += Integer.valueOf(sales[i][2]); | |
} | |
return sum; | |
} | |
static int birdAmount(String birdName){ | |
int i = getBirdId(birdName); | |
if (i>=0) { | |
return Integer.valueOf(store[i][2]); | |
} | |
return 0; | |
} | |
static int totalIncome(){ | |
int sum = 0; | |
for (int i = 0; i < salesLength;i++){ | |
sum += Integer.valueOf(sales[i][3]); | |
} | |
return sum; | |
} | |
static long monthIncome(int month){ | |
long income = 0; | |
for (int i = 0; i<salesLength; i++){ | |
long temp = Integer.valueOf(sales[i][0])/100; | |
if (temp%100 == month){ | |
income += Integer.valueOf(sales[i][3]); | |
} | |
} | |
return income; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment