0 Replies - 622 Views - Last Post: 13 April 2009 - 01:48 AM Rate Topic: -----

#1 rabbitvt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 14-October 08

Don't know how to get the require output format

Posted 13 April 2009 - 01:48 AM

Design a class name Employee the class should kept :employee name,employee number in format XXX-L where X=digit in arrange 0-9 L=letter in arrange A-M,hire date as a field.

Wrie a class ProductionWorker that extends Employee class and should have the : shift (an int) ,hourly pay rate (a double) as a field.

workday divide to 2 shift : day and night.
The shift field will be an integer value representing shift that employee works.
The day shift is 1 and night shift is 2.

I dont know how to get the correct format of the employee number.the format require like this: 999-A,123-ABC,098-ASD.


import java.util.Scanner;
import java.text.DecimalFormat;


public class Employee
{
private String employeeName;
private String employeeNumber;
private String hireDate;
private double hourlyPayRate;
private String shift;

DecimalFormat formatter = new DecimalFormat("#0.00");

public Employee()
{
employeeName=null;
employeeNumber=null;
hireDate=null;
hourlyPayRate=0.00;

}

public Employee(String nm,String num,String hd,double hpr)
{
employeeName=nm;
employeeNumber=num;
hireDate=hd;
hourlyPayRate=hpr;

}

public String getEmployeeName()
{
return employeeName;
}

public void setEmployeeName(String nm)
{
employeeName=nm;

}

public String getEmployeeNumber()
{
return employeeNumber;
}

public void setEmployeeNumber(String num)
{
employeeNumber=num;
}

public String getHireDate()
{
return hireDate;
}

public void setHireDate(String hd)
{
hireDate=hd;
}

public void setShift(String shift)
	{
	shift = shift;
	}

public String getShift()
	{
	return shift;
	}


public double getHourlyPayRate()
{
	return hourlyPayRate;
}

public void setHourlyPayRate(double hpr)
{
	hourlyPayRate=hpr;
}

public String toString()
	{

	return("\nEmployee name: "+getEmployeeName()+
			"\nEmployee number: "+getEmployeeNumber()+
			"\nEmployee hired date: "+getHireDate()+
			"\nEmployee shift: "+getShift()+
			"\nEmployee hourly pay:$ "+formatter.format(getHourlyPayRate()));
		}
	}




import java.util.Scanner;

public class EmployeeData
{
	public static void main(String[] args)
	{
		String employeeName;
		String employeeNumber;
		String hireDate;

		Employee em=new Employee();

		Scanner keyboard=new Scanner(System.in);

		System.out.println("Enter employee name : ");
		employeeName=keyboard.next();
		em.setEmployeeName(employeeName);

		System.out.println("Enter employee number in the format XXX-L (X-digit 0-9 , L-letter A-M): ");
		employeeNumber=keyboard.next();
		em.setEmployeeNumber(employeeNumber);



		if(isValid(employeeNumber))
		{
			System.out.println("That is a valid number.");
		}
		else
		{
			System.out.println("That is not proper format of a employee number."+
								"Example : 000-A");

			System.out.println("Enter employee number in the format XXX-L (X-digit 0-9 , L-letter A-M): ");
			employeeNumber=keyboard.next();
			em.setEmployeeNumber(employeeNumber);
		}


		System.out.println("Enter the hire date : ");
		hireDate=keyboard.next();
		em.setHireDate(hireDate);


		StringBuffer strbuff=new StringBuffer(employeeNumber);
		strbuff.insert(3,'-');
		System.out.println(employeeName);
		System.out.println(employeeNumber);


}		private static boolean isValid(String employeeNumber)
		{
			boolean goodSoFar=true;
			int i=0;

			if(employeeNumber.length()!=4)
			goodSoFar=false;

			while(goodSoFar&&i<3)
			{
				if(!Character.isDigit(employeeNumber.charAt(i)))
				goodSoFar=false;
				i++;
			}

			while(goodSoFar&&i<4)
			{
				if(!Character.isLetter(employeeNumber.charAt(i)))
				goodSoFar=false;
				i++;
			}

			return goodSoFar;
		}
	}



import java.util.Scanner;

public class PorductionWorker
{
	public static void main(String[] args)
	{
		String name;
		String number;
		String hireDate;
		String shift;
		String payRate;
		double hourlyPr;
		boolean error =true;

		Employee ep=new Employee();

		Scanner keyboard=new Scanner(System.in);

		System.out.println("Enter employee name : ");
		name=keyboard.next();
		ep.setEmployeeName(name.toUpperCase());

		System.out.println("Enter employee number in the format XXX-L (X-digit 0-9 , L-letter A-M): ");
		number=keyboard.next();
		ep.setEmployeeNumber(number);

		System.out.println("Enter the hire date : ");
		hireDate=keyboard.next();
		ep.setHireDate(hireDate);

		do
		{
			System.out.print("Enter the employee working shift(1=day shift, 2=night shift): ");
			shift = keyboard.nextLine();
			if(shift==1)
			{
				ep.setShift(shift);
				continue;
			}

			else if(shift==2)
			{
				ep.setShift(shift);
				continue;
			}

			else
			{
				System.out.println("\nERROR!!! Please choose either 1 or 2.");
				error = false;
			}

		}
		while(error==false);


			do
			{
				System.out.print("Enter the employee hourly payRate:$ ");
				payRate =keyboard.nextLine();
				char ch=payRate.charAt(0);

				if((Character.isDigit(ch))&&(Double.parseDouble(payRate) > 0))
				{
				hourlyPr = Double.parseDouble(payRate);
				ep.setHourlyPayRate(hourlyPr);
				error = true;
				}
				else
				{
				System.out.print("\nERROR!!!"+"\nThe value must greater than 0 or is a digit.\n");
				error = false;
				}
			}while(error==false);


		System.out.println(ep.toString());
	}
}







This post has been edited by rabbitvt: 13 April 2009 - 01:52 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1