Тема: З enum в arraylist
▼Прихований текст
package ua.lviv.lgs;
import java.time.Month;
import java.util.Scanner;
public class Task_2 {
static class Season {
public enum Seasons {
WINTER,
SPRING,
SUMMER,
AUTUMN;
public enum Month {
JANUARY(31, "WINTER"),
FEBRUARY(28, "SPRING"),
MARCH(31, "SPRING"),
APRIL(30, "SPRING"),
MAY(31, "SUMMER"),
JUNE(30, "SUMMER"),
JULY(31, "SUMMER"),
AUGUST(31, "AUTUMN"),
SEPTEMBER(30, "AUTUMN"),
OCTOBER(31, "AUTUMN"),
NOVEMBER(30, "WINTER"),
DECEMBER(31, "WINTER");
public int getInDays() {
return inDays;
}
public int setInDays(int inDays) {
return this.inDays = inDays;
}
public String getInSeasons() {
return inSeasons;
}
public void setInSeasons(String inSeasons) {
this.inSeasons = inSeasons;
}
public int inDays;
public String inSeasons;
Month(int inDays, String inSeasons) {
this.inDays = inDays;
this.inSeasons = inSeasons;
}
}
}
public int inDays;
public Month month;
public Seasons inSeasons;
public Season(Month month, Seasons inSeasons, int inDays) {
this.month = month;
this.inSeasons = inSeasons;
this.inDays = inDays;
}
class ProcessingMonth {
Scanner sc = new Scanner(System.in);
/**
*
* @return month equalized with the console entered month
*/
public Month readMonth() {
while (true) {
if (!(sc.hasNextInt())) {
String s = sc.nextLine().trim();
for (Month month: Month.values()) {
if (s.equalsIgnoreCase(month.name())) {
return month;
}
}
System.out.println("The word you entered is not \nthe name of the month, try again");
sc = new Scanner(System.in);
} else {
int m = sc.nextInt();
if (m > 12 || m < 1) {
System.out.println("You entered the wrong month, try again.");
sc = new Scanner(System.in);
} else {
for (Month month: Month.values()) {
if (m == month.ordinal() + 1) {
return month;
}
}
}
}
}
}
/**
*
* @param month console entered month
*/
public void sameSeasonMonths(Month month) {
Seasons season;
if (month.ordinal() >= 8 && month.ordinal() <= 10) {
season = Seasons.AUTUMN;
} else if (month.ordinal() >= 2 && month.ordinal() <= 4) {
season = Seasons.SPRING;
} else if (month.ordinal() >= 5 && month.ordinal() <= 7) {
season = Seasons.SUMMER;
} else {
season = Seasons.WINTER;
}
switch (season) {
case AUTUMN:
for (Month months: Month.values()) {
if (months.ordinal() == month.ordinal() || months.ordinal() < 8 || months.ordinal() > 10) {} else {
System.out.println(months);
}
}
break;
case SPRING:
for (Month months: Month.values()) {
if (months.ordinal() == month.ordinal() || months.ordinal() < 2 || months.ordinal() > 4) {} else {
System.out.println(months);
}
}
break;
case SUMMER:
for (Month months: Month.values()) {
if (months.ordinal() == month.ordinal() || months.ordinal() < 5 || months.ordinal() > 7) {} else {
System.out.println(months);
}
}
break;
case WINTER:
for (Month months: Month.values()) {
if (months.ordinal() == month.ordinal() || months.ordinal() < 11 && months.ordinal() > 1) {} else {
System.out.println(months);
}
}
break;
}
}
public void isEvenNumberOfDays(Month month, int days) {
if (days % 2 == 0) {
System.out.println(month.name() + " has an even number of days.");
} else {
System.out.println(month.name() + " has an odd number of days.");
}
}
}
class MethodsForComparison {
public void withSpecifiedNumberOfDays(int specifiedNumber) {
for (Month m: Month.values()) {
if (m.inDays == specifiedNumber) {
System.out.println(m.toString());
}
}
}
public void withGreaterNumberOfDays(int specifiedNumber) {
for (Month m: Month.values()) {
if (m.inDays > specifiedNumber) {
System.out.println(m.toString());
}
}
}
public void withSmallerNumberOfDays(int specifiedNumber) {
for (Month m: Month.values()) {
if (m.inDays < specifiedNumber) {
System.out.println(m.toString());
}
}
}
}
static class Main {
public void withSpecifiedNumberOfDays(int specifiedNumber) {
for (Month m: Month.values()) {
if (m.inDays == specifiedNumber) {
System.out.println(m.toString());
}
}
}
public void withGreaterNumberOfDays(int specifiedNumber) {
String s = null;
for (Month m: Month.values()) {
if (m.inDays > specifiedNumber) {
System.out.println(m.toString());
s = m.toString();
}
}
if (s == null) {
System.out.println("not more Great month");
}
}
public void withSmallerNumberOfDays(int specifiedNumber) {
String s = null;
for (Month m: Month.values()) {
if (m.inDays < specifiedNumber) {
System.out.println(m.toString());
s = m.toString();
}
}
if (s == null) {
System.out.println("not more Small month");
}
}
public void nexSeasons(Month month) {
int i = 0;
String[] d = {
"WINTER",
"SPRING",
"SUMMER",
"AUTUMN"
};
for (Seasons seas: Seasons.values()) {
i++;
if (month.inSeasons.equals(seas.name())) {
System.out.println("next seasons is " + d[i + 1]);
}
}
}
public void pastSeasons(Month month) {
int i = 0;
String[] d = {
"WINTER",
"SPRING",
"SUMMER",
"AUTUMN"
};
for (Seasons seas: Seasons.values()) {
i++;
if (month.inSeasons.equals(seas.name())) {
System.out.println("next past is " + d[i - 1]);
}
}
}
public void evenMonth() {
for (Month d: Month.values()) {
if (d.inDays % 2 == 0) {
System.out.println(d.name() + " day = " + d.inDays);
}
}
}
public void oddMonth() {
for (Month d: Month.values()) {
if (d.inDays % 2 == 1) {
System.out.println(d.name() + " day = " + d.inDays);
}
}
}
public static void main(String[] args) {
ProcessingMonth month = new ProcessingMonth();
System.out.println("take your pick :(only number)\n1 - Check for a month\n2 - show on display all months with the same time of year\n3 - show on display if put the console month has an even number of days\n4 - show on display all months that have the equal number of days\n5 - show on display all months that have more Great days\n6 - show on display all months that have fewer days\n7 - show on display Display the next season\n8 - show on display past season\n9 - show on display all months that have an odd number of days\n10 - show on display or put the console has an even number of month days\n11 - exit ");
int number;
boolean flag = true;
Main monthMain = new Main();
while (flag) {
Scanner scan = new Scanner(System.in);
if (scan.hasNextInt()) {
number = scan.nextInt();
if (number > 11 || number < 1) {
System.out.println("---------------------------------------------------------------------------");
System.out.println("your choice is incorrect");
System.out.println("---------------------------------------------------------------------------");
}
switch (number) {
case 1:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("1 - Check for a month");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
month.readMonth();
System.out.println("Select the next step");
break;
}
case 2:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("2 - show on display all months with the same time of year");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
month.sameSeasonMonths(month.readMonth());
System.out.println("Select the next step");
break;
}
case 3:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("3 - show on display if put the console month has an even number of days");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
month.isEvenNumberOfDays(month.readMonth(), inDays);
System.out.println("Select the next step");
break;
}
case 4:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("4 - show on display all months that have the equal number of days");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
monthMain.withSpecifiedNumberOfDays(month.readMonth().inDays);
System.out.println("Select the next step");
break;
}
case 5:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("5 - show on display all months that have more Great days");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
monthMain.withGreaterNumberOfDays(month.readMonth().inDays);
System.out.println("Select the next step");
break;
}
case 6:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("6 - show on display all months that have fewer days");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
monthMain.withSmallerNumberOfDays(month.readMonth().inDays);
System.out.println("Select the next step");
break;
}
case 7:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("7 - show on display the next season");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
monthMain.nexSeasons(month.readMonth());
System.out.println("Select the next step");
break;
}
case 8:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("8 - show on display past season");
System.out.println("---------------------------------------------------------------------------");
System.out.println("enter the month");
monthMain.nexSeasons(month.readMonth());
System.out.println("Select the next step");
break;
}
case 9:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("9 - show on display all months that have an odd number of days");
System.out.println("---------------------------------------------------------------------------");
monthMain.oddMonth();
System.out.println("Select the next step");
break;
}
case 10:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("10 - show on display or put the console has an even number of month days");
System.out.println("---------------------------------------------------------------------------");
monthMain.evenMonth();
System.out.println("Select the next step");
break;
}
case 11:
{
System.out.println("---------------------------------------------------------------------------");
System.out.println("11 - exit");
System.out.println("---------------------------------------------------------------------------");
flag = false;
break;
}
}
} else {
System.out.println("---------------------------------------------------------------------------");
System.out.println("error invalid input");
System.out.println("---------------------------------------------------------------------------");
}
}
}
}
}
}