I need help with this java programming task, where I´m supposed to create 4 vers
ID: 3669284 • Letter: I
Question
I need help with this java programming task, where I´m supposed to create 4 versions of a program. (I`ll make sure to give you a "thumb`s up"):
Version 0.1:
A college is going to get a new key card system, and a system designer has proposed an idea to how to create the program to operate the system:
An abstract class Card is going to contain the cardholders first and last name, PIN code, card number and an access code that specifies wheter the card is cancelled or not.
The class is going to have at least the following constructor and methods:
-A constructor that puts all data members to the preferred values. Let the class itself generate unique card numbers (use a static variable). The variable cancelledCard is set to false, which specifies that a brand new card is open.
-The method String getName() which returns the name of the cardholder.
-The method boolean IsCancelled() which returns whether the card is cancelled or not.
-A String toString() method that prints all the data members of the card.
-An abstract method boolean checkPIN(int pin).
Write the program code for the Card class.
Version 0.2:
The program is further developed, there has been proposed new suggestions, and you (as a developer) is making a prototype.
-There is a need to be able to distinguish between employees and guests. Guests are handed a card where the PIN code is always 9999, but the card is time limited; that is, it`s only valid one week after it`s made.
-An employees card is always valid throughout the office hours (between 07:00 and 17:00 from Monday to Friday). Besides office hours the employees has to type their PIN code (the card is not expiring due to time limitations).
-Cancelled cards can not give access to employees nor guests.
Based on this information; create two new classes Employee and Guest. Create the method boolean checkPIN(int pin) which works as described above. Add extra data members and methods/constructors in the classes to make this work, if necessary.
Version 0.3:
Define an interface Constants with the following methods:
- setFirstname(String) and String getFirstname() to be able to set and get the first name.
-setLastname(String) and String getLastname() to be able to set and get the last name.
-setFullName(String) and String getFullName() to be able to set and get the persons full name. The argument of the set method is going to be the first name and the last name separated by blank space.
-double computeCredit() is computing the credit as a constant multiplied with salary earned per hour.
-double computeBonus() is computed as a constant multiplied with the number of years the employee has worked there ("seniority").
The Employee class is going to implement the interface Constants. Add new data members in the Employee class if necessary.
Version 0.4
To be able to compare Card objects, e.g to sort a list of employees and guests, the Card class is going to implement the interfaces Comparable<Card> and Cloneable.
The Card objects is compared based on full name, first by last name, and then by first names if the last names are identical. This can be done by letting the Card class implement the method int compareTo (Card k) with the following logic:
-If the last names are different the method is:
-returning a negative integer if the first last name is alphabetically ordered before the other.
-or a positive integer if the opposite.
-If the last names is identical, the method is going to do the same as above for the first names. If the first names also is identical, the method is returning 0.
Hint: To compare two String objects one can use the String class`s built in Comparable method int compareTo(String streng2). This method is comparing one String with another alphabetically.
Explanation / Answer
Version 0.1
public abstract class Card {
String firstName;
String lastName;
String pin;
long cardNumber;
boolean accessCode;
static long card = 1;
public Card() {
firstName = "Alex";
lastName = "Mitti";
pin ="1120";
cardNumber = card;
card++;
accessCode = false;
}
String getName(){
return firstName + " "+lastName;
}
abstract boolean checkPIN(int pin);
@Override
public String toString() {
return "[Name: "+firstName+" "+lastName+",PIN: "+pin+",Card Number: "+cardNumber+",IsCancelled: "+accessCode+"]";
}
boolean isCancelled(){
return accessCode;
}
}
Version 0.2
class Employee extends Card{
@Override
boolean checkPIN(int pin) {
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
if(hour>=7 && hour<=17 && !isCancelled() && pin!=9999){
return true;
}
return false;
}
}
class Guest extends Card{
Date issueData;
public Guest(){
pin =9999;
issueData = new Date();
}
@Override
boolean checkPIN(int pin) {
Date currentDate = new Date();
int diffInDays = (int) ((currentDate.getTime() - issueData.getTime()) / (1000 * 60 * 60 * 24));
if(pin == 999 && diffInDays <=7 && (!isCancelled())){
return true;
}
return false;
}
}
version 0.3
import java.util.Calendar;
import java.util.GregorianCalendar;
public interface Constants {
void setFirstname(String firstName);
String getFirstname();
void setLastname(String lastName);
String getLastname();
void setFullName(String fullName);
String getFullName();
double computeCredit();
double computeBonus();
}
class Employee extends Card implements Constants{
double salaryPerHe;
GregorianCalendar issueData ;
public Employee() {
salaryPerHe =123.43;
issueData = new GregorianCalendar();
}
@Override
boolean checkPIN(int pin) {
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
year = date.get(Calendar.YEAR);
day = date.get(Calendar.DAY_OF_MONTH);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
if(hour>=7 && hour<=17 && !isCancelled() && pin!=9999){
return true;
}
return false;
}
@Override
public void setFirstname(String firstName) {
this.firstName= firstName;
}
@Override
public String getFirstname() {
return firstName;
}
@Override
public void setLastname(String lastName) {
this.lastName =lastName;
}
@Override
public String getLastname() {
return lastName;
}
@Override
public void setFullName(String fullName) {
String names[] = fullName.split("\s+");
firstName = names[0];
lastName=names[1];
}
@Override
public String getFullName() {
return firstName+" "+lastName;
}
@Override
public double computeCredit() {
return salaryPerHe*45; // constant 45
}
@Override
public double computeBonus() {
GregorianCalendar date = new GregorianCalendar();
int currentYear = date.get(Calendar.YEAR);
int startYear = issueData.get(Calendar.YEAR);
return (currentYear-startYear)*45; // 45 constant
}
}
version 0.4
abstract class Card implements Comparable<Card>,Cloneable {
String firstName;
String lastName;
int pin;
long cardNumber;
boolean accessCode;
static long card = 1;
public Card() {
firstName = "Alex";
lastName = "Mitti";
pin = 0;
cardNumber = card;
card++;
accessCode = false;
}
String getName(){
return firstName + " "+lastName;
}
abstract boolean checkPIN(int pin);
@Override
public String toString() {
return "[Name: "+firstName+" "+lastName+",PIN: "+pin+",Card Number: "+cardNumber+",IsCancelled: "+accessCode+"]";
}
boolean isCancelled(){
return accessCode;
}
@Override
public int compareTo(Card o) {
if(this.lastName.compareTo(o.firstName) !=0)
return -1;
return this.firstName.compareTo(o.firstName);
}
}