Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this code, I wanted to validate the user entered a valid date month (1-12), day (1-31), and year (1900-2020) but with the method as

For this code, I wanted to validate the user entered a valid date month (1-12), day (1-31), and year (1900-2020) but with the method as is it never exits or accepts either a valid date or a re-entered date after an invalid entry. Please help.

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 month)

{ //if clause to check the date

if(month <1 || month>12)

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

else

this.month = month;

}

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

EmpArr[i]=new Employee();

for(int i=0;i

{

System.out.println("Enter the Employee ID ");

Id=scnr.nextInt();

System.out.println("Enter the Employee First Name and Last Name ");

first=scnr.next();

last=scnr.next();

while(true)

{

System.out.println("Enter the Employee Hire Date "

+ ",i.e 3 11 2018, separated by a space");

month=scnr.nextInt();

day=scnr.nextInt();

year=scnr.nextInt();

if(month < 1 || month > 12 || day < 1 || day > 31 ||

year < 1900 || year > 2020)

System.out.println("Invalid date. Please input again ");

else

break;

}

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

{

Id=EmpArr[i].getEmpNumb();

xName=EmpArr[i].getEmpName();

xAddr=EmpArr[i].getEmpAddress();

xHireDate=EmpArr[i].getHireDate();

System.out.println("Id "+Id+" Name "+xName.first+" "+xName.last);

System.out.println("Address : "+xAddr.streetNum+" "+xAddr.street+" "

+xAddr.streetType+", "+xAddr.city+" , "+xAddr.state+","+xAddr.zip);

System.out.println("Hire Date: "+xHireDate.month+"-"

+xHireDate.day+"-"+xHireDate.year);

System.out.println();

System.out.println();

}

}

}

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

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

Draw a labelled diagram of the Dicot stem.

Answered: 1 week ago