In: Computer Science
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date. Also it needs to be able to change from Autumn, to another season depending on the amount of years it takes. So if the course will take 3 years, the student will finish "Summer/2025". How can I implement this?
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
Code:
package Test;
import java.util.Scanner;
public class CourseTerm {
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
String seasonAndYear,season;
int year ;
while(true){ //loop until proper
input is given
System.out.print("What season and year did you start your degree?
(eg. Autumn/2022) :");//asking for input
seasonAndYear=input.nextLine();
//checking for
proper input
if(seasonAndYear.contains("/")) {
String[] arrOfStr =
seasonAndYear.split("/");
if(arrOfStr.length==2) {//checking for proper
input
try
{
year=Integer.parseInt(arrOfStr[1]);
season=arrOfStr[0];
//checking
for proper input
if(season.equalsIgnoreCase("Autumn") ||
season.equalsIgnoreCase("Summer")||season.equalsIgnoreCase("Spring")||season.equalsIgnoreCase("Winter"))
{
season=getEndSeason(season); //calling function
to get end of the season
System.out.println("For 2 year course you will
end in :"+(year+2)+"/"+season);//Printing for 2 year course
System.out.println("For 3 year course you will
end in :"+(year+3)+"/"+season);//Printing for 3 year course
break;
}else
{//Printing error message
System.out.println("Invlaid Input. Please give
again... Format is Autumn/2022");
}
}catch(Exception e)
{//Printing error message
System.out.println("Invlaid Input. Please give again... Format is
Autumn/2022");
}
}else {//Printing error message
System.out.println("Invlaid
Input. Please give again... Format is Autumn/2022");
}
}else
{//Printing error message
System.out.println("Invlaid Input. Please give
again... Format is Autumn/2022");
}
}
input.close();
}
private static String getEndSeason(String season)
{
season=season.toLowerCase();
//returning season based on start
season
if(season.equalsIgnoreCase("autumn")) {
return
"Summer";
}else
if(season.equalsIgnoreCase("summer")) {
return
"Spring";
}else
if(season.equalsIgnoreCase("spring")) {
return
"Winter";
}else
if(season.equalsIgnoreCase("winter")) {
return
"Autumn";
}
return null;
}
}
Output: