Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For the following code, I have date validation checks in place. However, it just prints out at the end of the program that the year,

For the following code, I have date validation checks in place. However, it just prints out at the end of the program that the year, month, or day is invalid. I would like to have it tell the user when they enter it, any thoughts?

import java.util.Scanner;

public class Employees3 {

public static class Name { public String first=" "; public String last=" "; public Name() { first=" "; last =" "; } public String getFirst() { return first; } public String getLast() { return last; } public void setName(String first,String last) { this.first = first; this.last = last; } } public static class Date { public int month=0; public int day=0; public int year=0; public Date() { month=0; day=0; year=0; } public int getMonth() { return month; }

public void setMonth(int day) { //if clause to check the date

if(month <1 || month>12)

System.out.println("Invalid Month");

else

this.day = day;

} public int getDay() { return day; } public void setDay(int day) { //if clause to check the date

if(day<1 || day>31)

System.out.println("Invalid Day");

else

this.day = day;

} public int getYear() { return year; }

public void setYear(int year) {//if clause to check the validity of year

if(year<1900 || year>2020)

System.out.println("Invalid year");

else

this.year = year;

} public void setDate(int month,int day,int year) { setMonth(month); setDay(day); setYear(year); } } public static class Address { public String streetNum=" "; public String street=" "; public String streetType=" "; public String city=" "; public String state=" "; public int zip=0;

public Address() { streetNum=" "; street=" "; streetType=" "; city=" "; state=" "; zip=0; } public String getStreetNum() { return streetNum; } public String getStreet() { return street; } public String getStreetType() { return streetType; } public String getCity() { return city; } public String getState() { return state; } public int getZip() { return zip; } public void setAddress(String streetNum, String street, String streetType, String city,String state,int zip) { this.streetNum=streetNum; this.street=street; this.streetType=streetType; this.city =city; this.state =state; this.zip =zip; } } public static class Employee { private int EmpNumb=0; private Name EmpName; private Address EmpAddress; private Date HireDate; public Employee() { EmpNumb=0; EmpName=new Name(); EmpAddress=new Address(); HireDate =new Date(); } public int getEmpNumb() { return EmpNumb; } public Name getEmpName() { EmpName.first=EmpName.getFirst(); EmpName.last =EmpName.getLast(); return EmpName; } public Address getEmpAddress() { EmpAddress.streetNum=EmpAddress.getStreetNum(); EmpAddress.street=EmpAddress.getStreet(); EmpAddress.streetType=EmpAddress.getStreetType(); EmpAddress.city =EmpAddress.getCity(); EmpAddress.state =EmpAddress.getState(); EmpAddress.zip =EmpAddress.getZip(); return EmpAddress; } public Date getHireDate() { HireDate.month=HireDate.getMonth(); HireDate.day =HireDate.getDay(); HireDate.year =HireDate.getYear(); return HireDate; } public void setEmpId(int Id) { EmpNumb=Id; } public void setEmpName(String first,String last) { EmpName.setName(first,last); } public void setHireDate(int month,int day,int year) { HireDate.setDate(month, day, year); } public void setEmpAddress(String streetNum, String street, String streetType, String city,String state,int zip) { EmpAddress.setAddress(streetNum, street, streetType, city, state, zip); } } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter the Number of Employee: "); Scanner scnr = new Scanner(System.in); int n=scnr.nextInt(); //enter the values System.out.println("Enter the Data in Employee Array "); int Id; String first,last; int month,day,year; String streetNum, street, streetType, city,state; int zip; //Now creating Employee array of Size n Employee []EmpArr; EmpArr=new Employee[n]; for(int i=0;i

streetNum=scnr.next(); street=scnr.next(); streetType=scnr.next(); city =scnr.next(); state =scnr.next(); zip =scnr.nextInt(); EmpArr[i].setEmpId(Id); EmpArr[i].setEmpName(first, last); EmpArr[i].setHireDate(month, day, year); EmpArr[i].setEmpAddress(streetNum, street, streetType, city, state, zip); } //Now Printing the Data of Employee Name xName; Address xAddr; Date xHireDate; for(int i=0;i

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

12. Someone who likes to ski

Answered: 1 week ago

Question

Create a Fishbone diagram with the problem being coal "mine safety

Answered: 1 week ago

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago