Добрый день!
1. Подскажите, пожалуйста, вывод на консоль верный или нет.
Я специально увеличил список, чтобы можно было проверить некоторые позиции одинаковые по наименованию, но разные по дате и прибыли.
Позиции, имеющие различные даты и прибыли: Fake Apple Inc., Fake Cisco Systems, Inc., Fake Nokia Corporation (ADR), Fake Sirius Satellite SIRI.
Вывод на консоль:
Fake Apple Inc. AAPL | 15-03-2035 open 125,64 and last 123,43
Fake Apple Inc. AAPL | 23-02-2029 open 126,64 and last 123,43
Fake Applied Materials, Inc. AMAT | 07-09-1976 change 0,26
Fake Applied Materials, Inc. AMAT | 24-09-1973 change 0,26
Fake Cisco Systems, Inc. CSCO | 30-11-2034 open 25,84 and last 24,3
Fake Cisco Systems, Inc. CSCO | 19-10-2023 open 25,84 and last 26,3
Fake Comcast Corporation CMCSA | 11-03-1983 change 0,5
Fake Comcast Corporation CMCSA | 18-05-1970 change 0,5
Fake Google Inc. GOOG | 04-06-2042 open 516,2 and last 512,6
Fake Google Inc. GOOG | 22-03-2034 open 516,2 and last 512,6
Fake Intel Corporation INTC | 08-05-2041 open 21,36 and last 21,53
Fake Intel Corporation INTC | 25-08-2033 open 21,36 and last 21,53
Fake Level 3 Communications, Inc. LVLT | 04-01-2049 open 5,55 and last 5,54
Fake Level 3 Communications, Inc. LVLT | 23-02-2047 open 5,55 and last 5,54
Fake Microsoft Corporation MSFT | 26-02-2033 open 29,56 and last 29,72
Fake Microsoft Corporation MSFT | 13-04-2032 open 29,56 and last 29,72
Fake Nokia Corporation (ADR) NOK | 04-05-1988 change 0,18
Fake Nokia Corporation (ADR) NOK | 27-08-1977 change 0,1
Fake Oracle Corporation ORCL | 12-12-1992 change 0,15
Fake Oracle Corporation ORCL | 07-05-1975 change 0,15
Fake Sirius Satellite SIRI | 21-10-1999 change 0,03
Fake Sirius Satellite SIRI | 14-03-1988 change -0,03
Fake Starbucks Corporation SBUX | 18-08-1995 change 0,03
Fake Starbucks Corporation SBUX | 21-09-1986 change 0,03
Fake Yahoo! Inc. YHOO | 17-09-1975 change 0,32
Fake Yahoo! Inc. YHOO | 05-09-1972 change 0,32
package com.javarush.task.task24.task2412;
import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/*
Знания - сила!
*/
public class Solution {
public static void main(String[] args) {
List<Stock> stocks = getStocks();
sort(stocks);
Date actualDate = new Date();
printStocks(stocks, actualDate);
}
public static void printStocks(List<Stock> stocks, Date actualDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
double[] filelimits = {0d, actualDate.getTime()};
String[] filepart = {"change {4}", "open {2} and last {3}"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
Format[] testFormats = {null, null, dateFormat, fileform};
MessageFormat pattform = new MessageFormat("{0} {1} | {5} {6}");
pattform.setFormats(testFormats);
for (Stock stock : stocks) {
String name = ((String) stock.get("name"));
String symbol = (String) stock.get("symbol");
double open = !stock.containsKey("open") ? 0 : ((double) stock.get("open"));
double last = !stock.containsKey("last") ? 0 : ((double) stock.get("last"));
double change = !stock.containsKey("change") ? 0 : ((double) stock.get("change"));
Date date = (Date) stock.get("date");
Object[] testArgs = {name, symbol, open, last, change, date, date.getTime()};
System.out.println(pattform.format(testArgs));
}
}
public static void sort(List<Stock> list) {
list.sort(new Comparator<Stock>() {
public int compare(Stock stock1, Stock stock2) {
int res = ((String) stock1.get("name")).compareTo((String) stock2.get("name"));
if (res < 0) {
return -1;
} else if (res == 0) {
long oneDay = 24 * 60 * 60 * 1000;
Long d1 = ((Date) stock1.get("date")).getTime() / oneDay;
Long d2 = ((Date) stock2.get("date")).getTime() / oneDay;
int resDate = d1.compareTo(d2);
if ( resDate < 0) {
return 1;
} else if (resDate == 0) {
if (stock1.containsKey("open") && stock1.containsKey("last") && stock2.containsKey("open") && stock2.containsKey("last")) {
double resProfit = ((double) stock1.get("last") - (double) stock1.get("open")) - ((double) stock2.get("last") - (double) stock2.get("open"));
if (resProfit < 0) {
return -1;
} else {
return 1;
}
} else if (stock1.containsKey("change") && stock2.containsKey("change") ) {
double resProfit = (double) stock1.get("change") - (double) stock1.get("change");
if (resProfit < 0) {
return 1;
} else {
return -1;
}
} else {
throw new RuntimeException("Incorrect data");
}
} else {
return -1;
}
} else {
return 1;
}
}
});
}
public static class Stock extends HashMap<String, Object> {
public Stock(String name, String symbol, double open, double last) {
put("name", name);
put("symbol", symbol);
put("open", open);
put("last", last);
put("date", getRandomDate(2020));
}
public Stock(String name, String symbol, double change, Date date) {
put("name", name);
put("symbol", symbol);
put("date", date);
put("change", change);
}
}
public static List<Stock> getStocks() {
List<Stock> stocks = new ArrayList<>();
stocks.add(new Stock("Fake Apple Inc.", "AAPL", 125.64, 123.43));
stocks.add(new Stock("Fake Cisco Systems, Inc.", "CSCO", 25.84, 26.3));
stocks.add(new Stock("Fake Google Inc.", "GOOG", 516.2, 512.6));
stocks.add(new Stock("Fake Intel Corporation", "INTC", 21.36, 21.53));
stocks.add(new Stock("Fake Level 3 Communications, Inc.", "LVLT", 5.55, 5.54));
stocks.add(new Stock("Fake Microsoft Corporation", "MSFT", 29.56, 29.72));
stocks.add(new Stock("Fake Apple Inc.", "AAPL", 126.64, 123.43));
stocks.add(new Stock("Fake Cisco Systems, Inc.", "CSCO", 25.84, 26.3));
stocks.add(new Stock("Fake Google Inc.", "GOOG", 516.2, 512.6));
stocks.add(new Stock("Fake Intel Corporation", "INTC", 21.36, 21.53));
stocks.add(new Stock("Fake Level 3 Communications, Inc.", "LVLT", 5.55, 5.54));
stocks.add(new Stock("Fake Microsoft Corporation", "MSFT", 29.56, 29.72));
stocks.add(new Stock("Fake Nokia Corporation (ADR)", "NOK", .1, getRandomDate()));
stocks.add(new Stock("Fake Oracle Corporation", "ORCL", .15, getRandomDate()));
stocks.add(new Stock("Fake Starbucks Corporation", "SBUX", .03, getRandomDate()));
stocks.add(new Stock("Fake Yahoo! Inc.", "YHOO", .32, getRandomDate()));
stocks.add(new Stock("Fake Applied Materials, Inc.", "AMAT", .26, getRandomDate()));
stocks.add(new Stock("Fake Comcast Corporation", "CMCSA", .5, getRandomDate()));
stocks.add(new Stock("Fake Sirius Satellite", "SIRI", -.03, getRandomDate()));
stocks.add(new Stock("Fake Nokia Corporation (ADR)", "NOK", .18, getRandomDate()));
stocks.add(new Stock("Fake Oracle Corporation", "ORCL", .15, getRandomDate()));
stocks.add(new Stock("Fake Starbucks Corporation", "SBUX", .03, getRandomDate()));
stocks.add(new Stock("Fake Yahoo! Inc.", "YHOO", .32, getRandomDate()));
stocks.add(new Stock("Fake Applied Materials, Inc.", "AMAT", .26, getRandomDate()));
stocks.add(new Stock("Fake Comcast Corporation", "CMCSA", .5, getRandomDate()));
stocks.add(new Stock("Fake Sirius Satellite", "SIRI", .03, getRandomDate()));
return stocks;
}
public static Date getRandomDate() {
return getRandomDate(1970);
}
public static Date getRandomDate(int startYear) {
int year = startYear + (int) (Math.random() * 30);
int month = (int) (Math.random() * 12);
int day = (int) (Math.random() * 28);
GregorianCalendar calendar = new GregorianCalendar(year, month, day);
return calendar.getTime();
}
}