initial commit with lab 01 completed
This commit is contained in:
13 files changed
+1337
No files matched your search
@@ -0,0 +1,299 @@
|
||||
package ca.bcit.comp2522.lab01;
|
||||
|
||||
/**
|
||||
* BankAccount information with simple deposit and withdraw methods
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @author Nico Agostini
|
||||
* @author Trishaan Shetty
|
||||
* @author Calvin Arifianto
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public final class BankAccount {
|
||||
private static final double DEFAULT_BALANCE = 0.0;
|
||||
private static final int DEFAULT_PIN = 1000;
|
||||
private static final int MIN_ACCOUNT_NUMBER_LENGTH = 6;
|
||||
private static final int MAX_ACCOUNT_NUMBER_LENGTH = 7;
|
||||
|
||||
private final BankClient client;
|
||||
private final int pin;
|
||||
private final String accountNumber;
|
||||
private final Date accountOpened;
|
||||
private final Date accountClosed;
|
||||
|
||||
private double balanceUSD;
|
||||
|
||||
|
||||
/*
|
||||
validateObject ensures that an input object
|
||||
is not null.
|
||||
*/
|
||||
private static void validateObject(
|
||||
final Object object,
|
||||
final String objectTitle
|
||||
)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (object == null) {
|
||||
throw new IllegalArgumentException("Object cannot be null: " + objectTitle);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validateAccountNumber ensures that the input string is not null
|
||||
and within a valid length range.
|
||||
*/
|
||||
private static void validateAccountNumber(final String accountNumber)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (accountNumber == null ||
|
||||
accountNumber.length() < MIN_ACCOUNT_NUMBER_LENGTH ||
|
||||
accountNumber.length() > MAX_ACCOUNT_NUMBER_LENGTH)
|
||||
{
|
||||
throw new IllegalArgumentException("Invalid account number: " + accountNumber);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validatePin ensures the pin is in the valid range of the DEFAULT_PIN
|
||||
*/
|
||||
private static void validatePin(final int pin)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (pin < DEFAULT_PIN) {
|
||||
throw new IllegalArgumentException("Ping must be greater than or equal to " + DEFAULT_PIN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor
|
||||
* @param client Client object
|
||||
* @param balanceUSD double
|
||||
* @param pin int
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
* @param accountClosed Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final double balanceUSD,
|
||||
final int pin,
|
||||
final String accountNumber,
|
||||
final Date accountOpened,
|
||||
final Date accountClosed
|
||||
) {
|
||||
|
||||
validateObject(client, "client");
|
||||
validateObject(accountOpened, "accountOpenedDate");
|
||||
|
||||
validateAccountNumber(accountNumber);
|
||||
validatePin(pin);
|
||||
|
||||
this.client = client;
|
||||
this.balanceUSD = balanceUSD;
|
||||
this.pin = pin;
|
||||
this.accountNumber = accountNumber;
|
||||
this.accountOpened = accountOpened;
|
||||
this.accountClosed = accountClosed;
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default balance
|
||||
* @param client Client
|
||||
* @param pin int
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
* @param accountClosed Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final int pin,
|
||||
final String accountNumber,
|
||||
final Date accountOpened,
|
||||
final Date accountClosed
|
||||
) {
|
||||
|
||||
this(client, DEFAULT_BALANCE, pin, accountNumber, accountOpened, accountClosed);
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default pin
|
||||
* @param client Client
|
||||
* @param balanceUSD int
|
||||
* @param accountNumber String accountNumber
|
||||
* @param accountOpened Date
|
||||
* @param accountClosed Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final double balanceUSD,
|
||||
final String accountNumber,
|
||||
final Date accountOpened,
|
||||
final Date accountClosed
|
||||
) {
|
||||
|
||||
this(client, balanceUSD, DEFAULT_PIN, accountNumber, accountOpened, accountClosed);
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default pin and default balance
|
||||
* @param client Client
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
* @param accountClosed Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final String accountNumber,
|
||||
final Date accountOpened,
|
||||
final Date accountClosed
|
||||
) {
|
||||
|
||||
this(client, DEFAULT_BALANCE, DEFAULT_PIN, accountNumber, accountOpened, accountClosed);
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default balance and no account close date
|
||||
* @param client Client
|
||||
* @param pin int
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final int pin,
|
||||
final String accountNumber,
|
||||
final Date accountOpened
|
||||
) {
|
||||
|
||||
this(client, DEFAULT_BALANCE, pin, accountNumber, accountOpened, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default pin and no account closed date
|
||||
* @param client Client Object
|
||||
* @param balanceUSD double
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final double balanceUSD,
|
||||
final String accountNumber,
|
||||
final Date accountOpened
|
||||
) {
|
||||
|
||||
this(client, balanceUSD, DEFAULT_PIN, accountNumber, accountOpened, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* BankAccount constructor with default balance and default pin and no account closed date
|
||||
* @param client Client
|
||||
* @param accountNumber String
|
||||
* @param accountOpened Date
|
||||
*/
|
||||
public BankAccount(
|
||||
final BankClient client,
|
||||
final String accountNumber,
|
||||
final Date accountOpened
|
||||
) {
|
||||
|
||||
this(client, DEFAULT_BALANCE, DEFAULT_PIN, accountNumber, accountOpened, null);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* @param withdrawUSD
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
private void validateWithdrawAmount(final double withdrawUSD)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (withdrawUSD > this.balanceUSD) {
|
||||
throw new IllegalArgumentException("Insufficient funds");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* deposit to balanceUSD
|
||||
* @param amountUSD to deposit
|
||||
*/
|
||||
public final void deposit(final double amountUSD) {
|
||||
|
||||
this.balanceUSD += amountUSD;
|
||||
}
|
||||
|
||||
/**
|
||||
* withdraw from balance
|
||||
* @param amountUSD to withdraw as a double
|
||||
*/
|
||||
public final void withdraw(final double amountUSD) {
|
||||
|
||||
validateWithdrawAmount(amountUSD);
|
||||
|
||||
this.balanceUSD -= amountUSD;
|
||||
}
|
||||
|
||||
/**
|
||||
* withdraw from balance
|
||||
* @param amountUSD to withdraw as a double
|
||||
* @param pinToMatch int
|
||||
*/
|
||||
public final void withdraw(
|
||||
final double amountUSD,
|
||||
final int pinToMatch
|
||||
) {
|
||||
|
||||
if (pinToMatch != this.pin) {
|
||||
throw new IllegalArgumentException("Invalid pin ");
|
||||
}
|
||||
|
||||
validateWithdrawAmount(amountUSD);
|
||||
|
||||
this.balanceUSD -= amountUSD;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of account
|
||||
* @return account details
|
||||
*/
|
||||
public final String getDetails() {
|
||||
|
||||
final StringBuilder details;
|
||||
|
||||
details = new StringBuilder();
|
||||
|
||||
details.append(this.client.getFullName());
|
||||
details.append(" had $");
|
||||
details.append(this.balanceUSD);
|
||||
details.append(" USD in account #");
|
||||
details.append(this.accountNumber);
|
||||
details.append(" which was opened on ");
|
||||
details.append(this.accountOpened.getDayOfWeek());
|
||||
details.append(" ");
|
||||
details.append(this.accountOpened.getMonth());
|
||||
details.append(" ");
|
||||
details.append(this.accountOpened.getDay());
|
||||
details.append(", ");
|
||||
details.append(this.accountOpened.getYear());
|
||||
|
||||
if (this.accountClosed != null) {
|
||||
details.append(" and closed ");
|
||||
details.append(this.accountClosed.getDayOfWeek());
|
||||
details.append(" ");
|
||||
details.append(this.accountClosed.getMonth());
|
||||
details.append(" ");
|
||||
details.append(this.accountClosed.getDay());
|
||||
details.append(", ");
|
||||
details.append(this.accountClosed.getYear());
|
||||
}
|
||||
|
||||
details.append(".");
|
||||
|
||||
return details.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package ca.bcit.comp2522.lab01;
|
||||
|
||||
/**
|
||||
* BankClient holds information of a client
|
||||
* such as name, dateBorn, dateDied, clientID,
|
||||
* and signup date.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @author Nico Agostini
|
||||
* @author Trishaan Shetty
|
||||
* @author Calvin Arifianto
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public final class BankClient {
|
||||
private static final int MIN_CLIENT_ID_LENGTH = 6;
|
||||
private static final int MAX_CLIENT_ID_LENGTH = 7;
|
||||
|
||||
private final Name name;
|
||||
private final Date dateBorn;
|
||||
private final Date dateDied;
|
||||
private final String clientID;
|
||||
private final Date signupDate;
|
||||
|
||||
/*
|
||||
validateObject ensures an input object is not null.
|
||||
*/
|
||||
private static void validateObject(
|
||||
final Object object,
|
||||
final String objectTitle
|
||||
)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (object == null) {
|
||||
throw new IllegalArgumentException("Object cannot be null: " + objectTitle);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validateClientID ensures the input string is not null
|
||||
and within the proper length range.
|
||||
*/
|
||||
private static void validateClientID(final String clientID)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (
|
||||
clientID == null ||
|
||||
clientID.length() < MIN_CLIENT_ID_LENGTH ||
|
||||
clientID.length() > MAX_CLIENT_ID_LENGTH
|
||||
) {
|
||||
|
||||
throw new IllegalArgumentException("Invalid clientID: " + clientID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* BankClient constructor
|
||||
* @param name Name
|
||||
* @param dateBorn Date
|
||||
* @param dateDied Date
|
||||
* @param clientID String
|
||||
* @param signupDate Date
|
||||
*/
|
||||
public BankClient(
|
||||
final Name name,
|
||||
final Date dateBorn,
|
||||
final Date dateDied,
|
||||
final String clientID,
|
||||
final Date signupDate
|
||||
) {
|
||||
|
||||
validateObject(name, "name");
|
||||
validateObject(dateBorn, "dateBorn");
|
||||
validateObject(signupDate, "signupDate");
|
||||
|
||||
validateClientID(clientID);
|
||||
|
||||
this.name = name;
|
||||
this.dateBorn = dateBorn;
|
||||
this.dateDied = dateDied;
|
||||
this.clientID = clientID;
|
||||
this.signupDate = signupDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFullName of client Name
|
||||
* @return full name of client
|
||||
*/
|
||||
public final String getFullName() {
|
||||
|
||||
return this.name.getFullName();
|
||||
}
|
||||
|
||||
/**
|
||||
* getDetails of Bank Client
|
||||
* @return details including name, dates, and IDs
|
||||
*/
|
||||
public final String getDetails() {
|
||||
|
||||
final StringBuilder details;
|
||||
final String aliveStatus;
|
||||
|
||||
if (this.dateDied == null) {
|
||||
aliveStatus = "(alive)";
|
||||
} else {
|
||||
aliveStatus = "(not alive)";
|
||||
}
|
||||
|
||||
details = new StringBuilder();
|
||||
|
||||
details.append(this.name.getFullName());
|
||||
details.append(" client #");
|
||||
details.append(this.clientID);
|
||||
details.append(" ");
|
||||
details.append(aliveStatus);
|
||||
details.append(" joined the bank on ");
|
||||
details.append(this.signupDate.getDayOfWeek());
|
||||
details.append(", ");
|
||||
details.append(this.signupDate.getMonth());
|
||||
details.append(" ");
|
||||
details.append(this.signupDate.getDay());
|
||||
details.append(", ");
|
||||
details.append(this.signupDate.getYear());
|
||||
|
||||
return details.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package ca.bcit.comp2522.lab01;
|
||||
|
||||
/**
|
||||
* Date contains useful methods about a given date.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @author Nico Agostini
|
||||
* @author Trishaan Shetty
|
||||
* @author Calvin Arifianto
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public final class Date {
|
||||
// Domain of dates
|
||||
private static final int MIN_YEAR = 1800;
|
||||
private static final int MAX_YEAR = 2025;
|
||||
private static final int MIN_DAY = 1;
|
||||
|
||||
// Constants for calculating leap year, and days in given month
|
||||
private static final int YEARS_BTWN_LEAP_YEARS = 4;
|
||||
private static final int YEARS_PER_CENTURY = 100;
|
||||
private static final int YEARS_BTWN_LEAP_CENTURIES = 400;
|
||||
private static final int LEAP_YEAR_MOD_RESULT = 0;
|
||||
private static final int LEAP_YEAR_DAYS = 29;
|
||||
private static final int NON_LEAP_YEAR_DAYS = 28;
|
||||
private static final int MONTH_MOST_DAYS = 31;
|
||||
private static final int MONTH_REGULAR_DAYS = 30;
|
||||
private static final int EVEN = 2;
|
||||
private static final int IS_DIVISIBLE = 1;
|
||||
|
||||
// Constants to get next or last value
|
||||
private static final int GET_NEXT = 1;
|
||||
private static final int GET_PREVIOUS = 1;
|
||||
|
||||
// Constants to add to sum for calculating day of week
|
||||
private static final int CENTURY_CODE_YEAR_EIGHTEEN_HUNDREDS = 2;
|
||||
private static final int CENTURY_CODE_YEAR_TWO_THOUSANDS = 6;
|
||||
private static final int CENTURY_CODE_AND_MONTH_CODE = 12;
|
||||
|
||||
// Century definitions
|
||||
private static final int YEAR_EIGHTEEN_HUNDREDS = 1800;
|
||||
private static final int YEAR_NINETEEN_HUNDREDS = 1900;
|
||||
private static final int YEAR_TWO_THOUSANDS = 2000;
|
||||
|
||||
// Months
|
||||
private static final int JANUARY = 1;
|
||||
private static final int FEBRUARY = 2;
|
||||
private static final int MARCH = 3;
|
||||
private static final int APRIL = 4;
|
||||
private static final int MAY = 5;
|
||||
private static final int JUNE = 6;
|
||||
private static final int JULY = 7;
|
||||
private static final int AUGUST = 8;
|
||||
private static final int SEPTEMBER = 9;
|
||||
private static final int OCTOBER = 10;
|
||||
private static final int NOVEMBER = 11;
|
||||
private static final int DECEMBER = 12;
|
||||
|
||||
// Days of week
|
||||
private static final int SATURDAY = 0;
|
||||
private static final int SUNDAY = 1;
|
||||
private static final int MONDAY = 2;
|
||||
private static final int TUESDAY = 3;
|
||||
private static final int WEDNESDAY = 4;
|
||||
private static final int THURSDAY = 5;
|
||||
private static final int FRIDAY = 6;
|
||||
|
||||
// Constants used to calculate day of week
|
||||
private static final int DIVISION_BY_TWELVE = 12;
|
||||
private static final int DIVISION_BY_FOUR = 4;
|
||||
private static final int DAYS_OF_WEEK = 7;
|
||||
private static final String MONTH_CODES = "144025036146";
|
||||
|
||||
private final int year;
|
||||
private final int month;
|
||||
private final int day;
|
||||
|
||||
/*
|
||||
isLeapYear calculates if a given year is a leap year or not.
|
||||
|
||||
a) A year is a leap year if it is divisible by LEAP_YEAR_MOD_FOUR,
|
||||
|
||||
b) If the year is also divisible by LEAP_YEAR_MOD_ONE_HUNDRED then
|
||||
it is not a leap year.
|
||||
|
||||
c) If the year is divisible by LEAP_YEAR_MOD_ONE_HUNDRED and is
|
||||
divisible by LEAP_YEAR_MOD_FOUR_HUNDRED, then it is a leap year.
|
||||
*/
|
||||
private static boolean isLeapYear(final int year) {
|
||||
|
||||
final boolean isLeapYear;
|
||||
final boolean leapYearRuleOne;
|
||||
final boolean leapYearRuleTwo;
|
||||
|
||||
leapYearRuleOne = (year % YEARS_BTWN_LEAP_CENTURIES) == LEAP_YEAR_MOD_RESULT;
|
||||
leapYearRuleTwo = (year % YEARS_BTWN_LEAP_YEARS == LEAP_YEAR_MOD_RESULT && year % YEARS_PER_CENTURY != LEAP_YEAR_MOD_RESULT);
|
||||
|
||||
isLeapYear = leapYearRuleOne || leapYearRuleTwo;
|
||||
|
||||
return isLeapYear;
|
||||
}
|
||||
|
||||
/*
|
||||
daysInMonth calculates the max number of days in a given month.
|
||||
|
||||
If the month is February, we must know if it is a leap year or
|
||||
not, as the numbers of days changes.
|
||||
|
||||
Otherwise, for any other month, we determine if the month comes
|
||||
before August, as every odd month from January to July, has
|
||||
MONTH_MOST_DAYS, and every even month has MONTH_REGULAR_DAYS.
|
||||
|
||||
If the month is after August, then from August to December every
|
||||
odd month hsa MONTH_MOST_DAYS, and every even month has
|
||||
MONTH_REGULAR_DAYS.
|
||||
*/
|
||||
private static int daysInMonth(
|
||||
final int month,
|
||||
final boolean isLeapYear
|
||||
) {
|
||||
|
||||
if (month == FEBRUARY) {
|
||||
if (isLeapYear) {
|
||||
return LEAP_YEAR_DAYS;
|
||||
} else {
|
||||
return NON_LEAP_YEAR_DAYS;
|
||||
}
|
||||
}
|
||||
|
||||
final boolean longMonth;
|
||||
|
||||
if (month <= JULY) {
|
||||
longMonth = (month % EVEN) == IS_DIVISIBLE;
|
||||
} else {
|
||||
longMonth = ((month + GET_NEXT) % EVEN) == IS_DIVISIBLE;
|
||||
}
|
||||
|
||||
if (longMonth) {
|
||||
return MONTH_MOST_DAYS;
|
||||
} else {
|
||||
return MONTH_REGULAR_DAYS;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validateYear checks input year is in valid range
|
||||
*/
|
||||
private static void validateYear(final int year)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (year < MIN_YEAR || year > MAX_YEAR) {
|
||||
throw new IllegalArgumentException("invalid year");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validateMonth checks input month is in valid range
|
||||
*/
|
||||
private static void validateMonth(final int month)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
if (month < JANUARY || month > DECEMBER) {
|
||||
throw new IllegalArgumentException("invalid month");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
validateDay ensures the day given is less than the max day
|
||||
calculated by daysInMonth and greater than the MIN_DAY
|
||||
*/
|
||||
private static void validateDay(
|
||||
final int day,
|
||||
final int month,
|
||||
final int year
|
||||
)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
|
||||
final boolean leapYear;
|
||||
final int maxDays;
|
||||
|
||||
leapYear = isLeapYear(year);
|
||||
maxDays = daysInMonth(month, leapYear);
|
||||
|
||||
if (day < MIN_DAY || day > maxDays) {
|
||||
throw new IllegalArgumentException("invalid day");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Date constructor
|
||||
* @param year int
|
||||
* @param month int
|
||||
* @param day int
|
||||
*/
|
||||
public Date(
|
||||
final int year,
|
||||
final int month,
|
||||
final int day
|
||||
) {
|
||||
|
||||
validateYear(year);
|
||||
validateMonth(month);
|
||||
validateDay(day, month, year);
|
||||
|
||||
this.year = year;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
/**
|
||||
* getYear of Date
|
||||
* @return year
|
||||
*/
|
||||
public final int getYear() {
|
||||
|
||||
return this.year;
|
||||
}
|
||||
|
||||
/**
|
||||
* getMonth of Date
|
||||
* @return month
|
||||
*/
|
||||
public final String getMonth() {
|
||||
|
||||
final String month;
|
||||
|
||||
if (this.month == JANUARY) {
|
||||
month = "January";
|
||||
} else if (this.month == FEBRUARY) {
|
||||
month = "February";
|
||||
} else if (this.month == MARCH) {
|
||||
month = "March";
|
||||
} else if (this.month == APRIL) {
|
||||
month = "April";
|
||||
} else if (this.month == MAY) {
|
||||
month = "May";
|
||||
} else if (this.month == JUNE) {
|
||||
month = "June";
|
||||
} else if (this.month == JULY) {
|
||||
month = "July";
|
||||
} else if (this.month == AUGUST) {
|
||||
month = "August";
|
||||
} else if (this.month == SEPTEMBER) {
|
||||
month = "September";
|
||||
} else if (this.month == OCTOBER) {
|
||||
month = "October";
|
||||
} else if (this.month == NOVEMBER) {
|
||||
month = "November";
|
||||
} else if (this.month == DECEMBER) {
|
||||
month = "December";
|
||||
} else {
|
||||
throw new IllegalArgumentException("invalid month");
|
||||
}
|
||||
|
||||
return month;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDay of Date
|
||||
* @return day
|
||||
*/
|
||||
public final int getDay() {
|
||||
|
||||
return this.day;
|
||||
}
|
||||
|
||||
/**
|
||||
* getYyyyMmDd formatted as string
|
||||
* @return date in YyyyMmDd format
|
||||
*/
|
||||
public final String getYyyyMmDd() {
|
||||
|
||||
final String date;
|
||||
|
||||
date = this.year + "-" + this.month + "-" + this.day;
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDayOfWeek
|
||||
*
|
||||
* To get the day of the week, do the following seven steps for dates in the 1900s:
|
||||
*
|
||||
* e.g. October 31 1977:
|
||||
* step 1: calculate the number of twelves in 77:
|
||||
* 6
|
||||
* step 2: calculate the remainder from step 1: 77 - 12*6 = 77 - 72 =
|
||||
* 5
|
||||
* step 3: calculate the number of fours in step 2: 5/4 = 1.25, so
|
||||
* 1
|
||||
* step 4: add the day of the month to each step above: 31 + 6 + 5 + 1 =
|
||||
* 43
|
||||
* step 5: add the month code (for jfmamjjasond: 144025036146): for october it is 1: 43 + 1 =
|
||||
* 44
|
||||
* step 6: add the previous five numbers: (44) and mod by 7: 44%7 = 2 (44/7 = 6 remainder 2)
|
||||
* step 7: sat sun mon tue wed thu fri is 0 1 2 3 4 5 6; our 2 means Oct 31 1977 was monday
|
||||
* Extra notes:
|
||||
* a) for January/February dates in leap years, add 6 at the start
|
||||
* b) for all dates in the 2000s, add 6 at the start
|
||||
* c) for all dates in the 1800s, add 2 at the star
|
||||
*
|
||||
* @return day of week
|
||||
*/
|
||||
public final String getDayOfWeek() {
|
||||
|
||||
final int year;
|
||||
final int century;
|
||||
final String monthCode;
|
||||
|
||||
final int step0;
|
||||
final int step1;
|
||||
final int step2;
|
||||
final int step3;
|
||||
final int step4;
|
||||
final int step5;
|
||||
|
||||
final int dayIndex;
|
||||
final String day;
|
||||
|
||||
monthCode = MONTH_CODES.charAt(this.month - GET_PREVIOUS) + "";
|
||||
|
||||
if (this.year < YEAR_NINETEEN_HUNDREDS) {
|
||||
century = YEAR_EIGHTEEN_HUNDREDS;
|
||||
} else if (this.year < YEAR_TWO_THOUSANDS) {
|
||||
century = YEAR_NINETEEN_HUNDREDS;
|
||||
} else {
|
||||
century = YEAR_TWO_THOUSANDS;
|
||||
}
|
||||
|
||||
year = this.year - century;
|
||||
|
||||
if (
|
||||
isLeapYear(this.year) &&
|
||||
(this.month == JANUARY || this.month == FEBRUARY) &&
|
||||
this.year >= YEAR_TWO_THOUSANDS
|
||||
) {
|
||||
|
||||
step0 = CENTURY_CODE_AND_MONTH_CODE;
|
||||
|
||||
} else if (
|
||||
isLeapYear(this.year) &&
|
||||
(this.month == JANUARY || this.month == FEBRUARY)
|
||||
) {
|
||||
step0 = CENTURY_CODE_YEAR_TWO_THOUSANDS;
|
||||
} else if (this.year >= YEAR_TWO_THOUSANDS) {
|
||||
step0 = CENTURY_CODE_YEAR_TWO_THOUSANDS;
|
||||
} else if (this.year < YEAR_NINETEEN_HUNDREDS) {
|
||||
step0 = CENTURY_CODE_YEAR_EIGHTEEN_HUNDREDS;
|
||||
} else {
|
||||
step0 = 0;
|
||||
}
|
||||
|
||||
step1 = year / DIVISION_BY_TWELVE;
|
||||
step2 = year - (DIVISION_BY_TWELVE * step1);
|
||||
step3 = step2 / DIVISION_BY_FOUR;
|
||||
step4 = this.day + step0 + step1 + step2 + step3;
|
||||
step5 = step4 + Integer.parseInt(monthCode);
|
||||
|
||||
dayIndex = step5 % DAYS_OF_WEEK;
|
||||
|
||||
if (dayIndex == SATURDAY) {
|
||||
day = "Saturday";
|
||||
} else if (dayIndex == SUNDAY) {
|
||||
day = "Sunday";
|
||||
} else if (dayIndex == MONDAY) {
|
||||
day = "Monday";
|
||||
} else if (dayIndex == TUESDAY) {
|
||||
day = "Tuesday";
|
||||
} else if (dayIndex == WEDNESDAY) {
|
||||
day = "Wednesday";
|
||||
} else if (dayIndex == THURSDAY) {
|
||||
day = "Thursday";
|
||||
} else if (dayIndex == FRIDAY) {
|
||||
day = "Friday";
|
||||
} else {
|
||||
throw new IllegalArgumentException("invalid day");
|
||||
}
|
||||
|
||||
return day;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package ca.bcit.comp2522.lab01;
|
||||
|
||||
/**
|
||||
* Main class driver
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @author Nico Agostini
|
||||
* @author Trishaan Shetty
|
||||
* @author Calvin Arifianto
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public final class Main {
|
||||
// Albert Einstein data
|
||||
private static final int ALBERT_BIRTH_YEAR = 1879;
|
||||
private static final int ALBERT_BIRTH_MONTH = 3;
|
||||
private static final int ALBERT_BIRTH_DAY = 14;
|
||||
private static final int ALBERT_DEATH_YEAR = 1955;
|
||||
private static final int ALBERT_DEATH_MONTH = 4;
|
||||
private static final int ALBERT_DEATH_DAY = 18;
|
||||
private static final int ALBERT_SIGNUP_YEAR = 1900;
|
||||
private static final int ALBERT_SIGNUP_MONTH = 1;
|
||||
private static final int ALBERT_SIGNUP_DAY = 1;
|
||||
private static final int ALBERT_CLOSE_YEAR = 1950;
|
||||
private static final int ALBERT_CLOSE_MONTH = 10;
|
||||
private static final int ALBERT_CLOSE_DAY = 14;
|
||||
private static final int ALBERT_STARTING_BALANCE_USD = 1000;
|
||||
private static final int ALBERT_WITHDRAW_USD = 100;
|
||||
private static final int ALBERT_PIN = 3141;
|
||||
|
||||
// Nelson Mandella data
|
||||
private static final int NELSON_BIRTH_YEAR = 1918;
|
||||
private static final int NELSON_BIRTH_MONTH = 7;
|
||||
private static final int NELSON_BIRTH_DAY = 18;
|
||||
private static final int NELSON_DEATH_YEAR = 2013;
|
||||
private static final int NELSON_DEATH_MONTH = 12;
|
||||
private static final int NELSON_DEATH_DAY = 5;
|
||||
private static final int NELSON_SIGNUP_YEAR = 1994;
|
||||
private static final int NELSON_SIGNUP_MONTH = 5;
|
||||
private static final int NELSON_SIGNUP_DAY = 10;
|
||||
private static final int NELSON_STARTING_BALANCE_USD = 2000;
|
||||
private static final int NELSON_WITHDRAW_USD = 200;
|
||||
private static final int NELSON_PIN = 4664;
|
||||
|
||||
// Frida data
|
||||
private static final int FRIDA_BIRTH_YEAR = 1907;
|
||||
private static final int FRIDA_BIRTH_MONTH = 7;
|
||||
private static final int FRIDA_BIRTH_DAY = 6;
|
||||
private static final int FRIDA_DEATH_YEAR = 1954;
|
||||
private static final int FRIDA_DEATH_MONTH = 7;
|
||||
private static final int FRIDA_DEATH_DAY = 13;
|
||||
private static final int FRIDA_SIGNUP_YEAR = 1940;
|
||||
private static final int FRIDA_SIGNUP_MONTH = 1;
|
||||
private static final int FRIDA_SIGNUP_DAY = 1;
|
||||
private static final int FRIDA_CLOSE_YEAR = 1954;
|
||||
private static final int FRIDA_CLOSE_MONTH = 7;
|
||||
private static final int FRIDA_CLOSE_DAY = 13;
|
||||
private static final int FRIDA_STARTING_BALANCE_USD = 500;
|
||||
private static final int FRIDA_WITHDRAW_USD = 50;
|
||||
private static final int FRIDA_PIN = 1907;
|
||||
|
||||
// Jackie Chan data
|
||||
private static final int JACKIE_BIRTH_YEAR = 1954;
|
||||
private static final int JACKIE_BIRTH_MONTH = 4;
|
||||
private static final int JACKIE_BIRTH_DAY = 7;
|
||||
private static final int JACKIE_SIGNUP_YEAR = 1980;
|
||||
private static final int JACKIE_SIGNUP_MONTH = 10;
|
||||
private static final int JACKIE_SIGNUP_DAY = 1;
|
||||
private static final int JACKIE_STARTING_BALANCE = 3000;
|
||||
private static final int JACKIE_WITHDRAW_USD = 500;
|
||||
private static final int JACKIE_PIN = 1954;
|
||||
|
||||
/**
|
||||
* main program entry
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
|
||||
/*
|
||||
create Albert Einsteins accounts
|
||||
and perform actions.
|
||||
*/
|
||||
final Name nameAlbert;
|
||||
final Date birthDateAlbert;
|
||||
final Date deathDateAlbert;
|
||||
final Date signupDateAlbert;
|
||||
final Date closeDateAlbert;
|
||||
final String clientIdAlbert;
|
||||
final BankClient clientAlbert;
|
||||
final BankAccount accountAlbert;
|
||||
|
||||
clientIdAlbert = "abc123";
|
||||
nameAlbert = new Name("Albert", "Einstein");
|
||||
birthDateAlbert = new Date(ALBERT_BIRTH_YEAR, ALBERT_BIRTH_MONTH, ALBERT_BIRTH_DAY);
|
||||
deathDateAlbert = new Date(ALBERT_DEATH_YEAR, ALBERT_DEATH_MONTH, ALBERT_DEATH_DAY);
|
||||
signupDateAlbert = new Date(ALBERT_SIGNUP_YEAR, ALBERT_SIGNUP_MONTH, ALBERT_SIGNUP_DAY);
|
||||
closeDateAlbert = new Date(ALBERT_CLOSE_YEAR, ALBERT_CLOSE_MONTH, ALBERT_CLOSE_DAY);
|
||||
|
||||
clientAlbert = new BankClient(
|
||||
nameAlbert,
|
||||
birthDateAlbert,
|
||||
deathDateAlbert,
|
||||
clientIdAlbert,
|
||||
signupDateAlbert
|
||||
);
|
||||
|
||||
accountAlbert = new BankAccount(
|
||||
clientAlbert,
|
||||
ALBERT_STARTING_BALANCE_USD,
|
||||
ALBERT_PIN,
|
||||
clientIdAlbert,
|
||||
signupDateAlbert,
|
||||
closeDateAlbert
|
||||
);
|
||||
|
||||
System.out.println(nameAlbert.getInitials() + " " + nameAlbert.getFullName() + " " + nameAlbert.getReverseName());
|
||||
System.out.println(clientAlbert.getDetails());
|
||||
System.out.println(accountAlbert.getDetails());
|
||||
System.out.println();
|
||||
accountAlbert.withdraw(ALBERT_WITHDRAW_USD, ALBERT_PIN);
|
||||
|
||||
|
||||
/*
|
||||
create Nelson Mandellas accounts
|
||||
and perform actions.
|
||||
*/
|
||||
final Name nameNelson;
|
||||
final Date birthDateNelson;
|
||||
final Date deathDateNelson;
|
||||
final Date signupDateNelson;
|
||||
final String clientIdNelson;
|
||||
final BankClient clientNelson;
|
||||
final BankAccount accountNelson;
|
||||
|
||||
clientIdNelson = "654321";
|
||||
nameNelson = new Name("Nelson", "Mandela");
|
||||
birthDateNelson = new Date(NELSON_BIRTH_YEAR, NELSON_BIRTH_MONTH, NELSON_BIRTH_DAY);
|
||||
deathDateNelson = new Date(NELSON_DEATH_YEAR, NELSON_DEATH_MONTH, NELSON_DEATH_DAY);
|
||||
signupDateNelson = new Date(NELSON_SIGNUP_YEAR, NELSON_SIGNUP_MONTH, NELSON_SIGNUP_DAY);
|
||||
|
||||
clientNelson = new BankClient(
|
||||
nameNelson,
|
||||
birthDateNelson,
|
||||
deathDateNelson,
|
||||
clientIdNelson,
|
||||
signupDateNelson
|
||||
);
|
||||
|
||||
accountNelson = new BankAccount(
|
||||
clientNelson,
|
||||
NELSON_STARTING_BALANCE_USD,
|
||||
NELSON_PIN,
|
||||
clientIdNelson,
|
||||
signupDateNelson,
|
||||
null
|
||||
);
|
||||
|
||||
System.out.println(nameNelson.getInitials() + " " + nameNelson.getFullName() + " " + nameNelson.getReverseName());
|
||||
System.out.println(clientNelson.getDetails());
|
||||
System.out.println(accountNelson.getDetails());
|
||||
System.out.println();
|
||||
accountNelson.withdraw(NELSON_WITHDRAW_USD, NELSON_PIN);
|
||||
|
||||
|
||||
/*
|
||||
create Frida Kahlo accounts
|
||||
and perform actions.
|
||||
*/
|
||||
final Name nameFrida;
|
||||
final Date birthDateFrida;
|
||||
final Date deathDateFrida;
|
||||
final Date signupDateFrida;
|
||||
final Date closeDateFrida;
|
||||
final String clientIdFrida;
|
||||
final BankClient clientFrida;
|
||||
final BankAccount accountFrida;
|
||||
|
||||
clientIdFrida = "frd1233";
|
||||
nameFrida = new Name("Frida", "Kahlo");
|
||||
birthDateFrida = new Date(FRIDA_BIRTH_YEAR, FRIDA_BIRTH_MONTH, FRIDA_BIRTH_DAY);
|
||||
deathDateFrida = new Date(FRIDA_DEATH_YEAR, FRIDA_DEATH_MONTH, FRIDA_DEATH_DAY);
|
||||
signupDateFrida = new Date(FRIDA_SIGNUP_YEAR, FRIDA_SIGNUP_MONTH, FRIDA_SIGNUP_DAY);
|
||||
closeDateFrida = new Date(FRIDA_CLOSE_YEAR, FRIDA_CLOSE_MONTH, FRIDA_CLOSE_DAY);
|
||||
|
||||
clientFrida = new BankClient(
|
||||
nameFrida,
|
||||
birthDateFrida,
|
||||
deathDateFrida,
|
||||
clientIdFrida,
|
||||
signupDateFrida
|
||||
);
|
||||
|
||||
accountFrida = new BankAccount(
|
||||
clientFrida,
|
||||
FRIDA_STARTING_BALANCE_USD,
|
||||
FRIDA_PIN,
|
||||
clientIdFrida,
|
||||
signupDateFrida,
|
||||
closeDateFrida
|
||||
);
|
||||
|
||||
System.out.println(nameFrida.getInitials() + " " + nameFrida.getFullName() + " " + nameFrida.getReverseName());
|
||||
System.out.println(clientFrida.getDetails());
|
||||
System.out.println(accountFrida.getDetails());
|
||||
System.out.println();
|
||||
accountFrida.withdraw(FRIDA_WITHDRAW_USD, FRIDA_PIN);
|
||||
|
||||
|
||||
/*
|
||||
create Jackie Chan's accounts
|
||||
and perform actions.
|
||||
*/
|
||||
final Name nameJackie;
|
||||
final Date birthDateJackie;
|
||||
final Date signupDateJackie;
|
||||
final String clientIdJackie;
|
||||
final BankClient clientJackie;
|
||||
final BankAccount accountJackie;
|
||||
|
||||
clientIdJackie = "chan789";
|
||||
nameJackie = new Name("Jackie", "Chan");
|
||||
birthDateJackie = new Date(JACKIE_BIRTH_YEAR, JACKIE_BIRTH_MONTH, JACKIE_BIRTH_DAY);
|
||||
signupDateJackie = new Date(JACKIE_SIGNUP_YEAR, JACKIE_SIGNUP_MONTH, JACKIE_SIGNUP_DAY);
|
||||
|
||||
clientJackie = new BankClient(
|
||||
nameJackie,
|
||||
birthDateJackie,
|
||||
null,
|
||||
clientIdJackie,
|
||||
signupDateJackie
|
||||
);
|
||||
|
||||
accountJackie = new BankAccount(
|
||||
clientJackie,
|
||||
JACKIE_STARTING_BALANCE,
|
||||
JACKIE_PIN,
|
||||
clientIdJackie,
|
||||
signupDateJackie,
|
||||
null
|
||||
);
|
||||
|
||||
System.out.println(nameJackie.getInitials() + " " + nameJackie.getFullName() + " " + nameJackie.getReverseName());
|
||||
System.out.println(clientJackie.getDetails());
|
||||
System.out.println(accountJackie.getDetails());
|
||||
accountJackie.withdraw(JACKIE_WITHDRAW_USD, JACKIE_PIN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package ca.bcit.comp2522.lab01;
|
||||
|
||||
/**
|
||||
* Name contains first and last names,
|
||||
* as well as useful functions for names.
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @author Nico Agostini
|
||||
* @author Trishaan Shetty
|
||||
* @author Calvin Arifianto
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public final class Name {
|
||||
private static final int MAX_CHARACTERS = 45;
|
||||
private static final int FIRST_CHARACTER = 0;
|
||||
private static final int SECOND_CHARACTER = 1;
|
||||
private static final int HALF = 2;
|
||||
private static final int INDEX_OFFSET = 1;
|
||||
|
||||
private final String first;
|
||||
private final String last;
|
||||
|
||||
/*
|
||||
validateString ensures that any input string is not blank,
|
||||
within the string length limit, and does not contain admin.
|
||||
*/
|
||||
private static void validateString(final String input)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
if (input.length() > MAX_CHARACTERS) {
|
||||
throw new IllegalArgumentException("input is too long");
|
||||
}
|
||||
|
||||
if (input.isBlank()) {
|
||||
throw new IllegalArgumentException("input cannot be empty");
|
||||
}
|
||||
|
||||
if (input.contains("admin")) {
|
||||
throw new IllegalArgumentException("input cannot contain \"admin\"");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
capitalize takes in a String, and ensures the first letter is
|
||||
uppercase while all other letters are lowercase
|
||||
*/
|
||||
private static String capitalize(final String input) {
|
||||
|
||||
final String capitalizedFirstCharacter;
|
||||
final String lowerCaseRemainingCharacters;
|
||||
final String result;
|
||||
|
||||
capitalizedFirstCharacter = input.substring(FIRST_CHARACTER, SECOND_CHARACTER).toUpperCase();
|
||||
lowerCaseRemainingCharacters = input.substring(SECOND_CHARACTER).toLowerCase();
|
||||
|
||||
result = capitalizedFirstCharacter + lowerCaseRemainingCharacters;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name constructor
|
||||
* @param first name as String
|
||||
* @param last name as String
|
||||
*/
|
||||
public Name(final String first, final String last) {
|
||||
|
||||
validateString(first);
|
||||
validateString(last);
|
||||
|
||||
this.first = capitalize(first);
|
||||
this.last = capitalize(last);
|
||||
}
|
||||
|
||||
/**
|
||||
* getInitials returns first letter of first and last name.
|
||||
* @return initials
|
||||
*/
|
||||
public final String getInitials() {
|
||||
|
||||
return this.first.charAt(FIRST_CHARACTER) + "." + this.last.charAt(FIRST_CHARACTER) + ".";
|
||||
}
|
||||
|
||||
/**
|
||||
* getFullName returns both the first and last name.
|
||||
* @return the first and last name
|
||||
*/
|
||||
public final String getFullName() {
|
||||
|
||||
return this.first + " " + this.last;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFirst name
|
||||
* @return first name
|
||||
*/
|
||||
public final String getFirst() {
|
||||
|
||||
return this.first;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLast name
|
||||
* @return last name
|
||||
*/
|
||||
public final String getLast() {
|
||||
|
||||
return this.last;
|
||||
}
|
||||
|
||||
/**
|
||||
* getReverseName returns the full name in reverse
|
||||
* @return full name reversed
|
||||
*/
|
||||
public final String getReverseName() {
|
||||
|
||||
final String fullName;
|
||||
final String fullNameReversed;
|
||||
fullName = this.first + " " + this.last;
|
||||
|
||||
char currentCharacter;
|
||||
final char[] nameCharacters;
|
||||
|
||||
nameCharacters = fullName.toCharArray();
|
||||
|
||||
for (int i = 0; i < nameCharacters.length / HALF; i++) {
|
||||
final int currentIndex;
|
||||
currentIndex = nameCharacters.length - INDEX_OFFSET - i;
|
||||
|
||||
currentCharacter = nameCharacters[currentIndex];
|
||||
nameCharacters[currentIndex] = nameCharacters[i];
|
||||
nameCharacters[i] = currentCharacter;
|
||||
}
|
||||
|
||||
fullNameReversed = new String(nameCharacters);
|
||||
|
||||
return fullNameReversed;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user