Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/main/java/com/dtcc/exams/arrays/IntegerArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.dtcc.exams.arrays;

public class IntegerArrayUtils {


/**
* @param integerArray - array to have value added to it
* @param valueToBeAdded - value to be added to the end of the array
* @return - identical array with one additional element of `valueToBeAdded` at the end of the array
*/
public static Integer[] add(Integer[] integerArray, Integer valueToBeAdded) {
return null;
Integer[] newArray = new Integer[integerArray.length + 1];
for (int i = 0; i < integerArray.length; i++){
newArray[i] = integerArray[i];
}
newArray[newArray.length-1] = valueToBeAdded;

return newArray;
}

/**
Expand All @@ -17,7 +25,8 @@ public static Integer[] add(Integer[] integerArray, Integer valueToBeAdded) {
* @return `integerArray` with `valueToBeInserted` at index number `indexToInsertAt`
*/
public static Integer[] replace(Integer[] integerArray, int indexToInsertAt, Integer valueToBeInserted) {
return null;
integerArray[indexToInsertAt] = valueToBeInserted;
return integerArray;
}

/**
Expand All @@ -26,30 +35,49 @@ public static Integer[] replace(Integer[] integerArray, int indexToInsertAt, Int
* @return element located at `indexToFetch`
*/
public static Integer get(Integer[] integerArray, Integer indexToFetch) {
return null;
Integer value = integerArray[indexToFetch];
return value;
}

/**
* @param integerArray - array to be evaluated
* @return identical array with even-values incremented by 1 and odd-values decremented by 1
*/
public static Integer[] incrementEvenDecrementOdd(Integer[] integerArray) {
return null;
for (int i = 0; i < integerArray.length; i++){
if (integerArray[i] % 2 == 0){
integerArray[i]+=1;
}
else{
integerArray[i]-=1;
}
}
return integerArray;
}

/**
* @param integerArray - array to be evaluated
* @return identical array with even-values incremented by 1
*/
public static Integer[] incrementEven(Integer[] integerArray) {
return null;
for (int i = 0; i < integerArray.length; i++){
if (integerArray[i] % 2 == 0){
integerArray[i]+=1;
}
}
return integerArray;
}

/**
* @param input - array to be evaluated
* @return identical array with odd-values decremented by 1
*/
public static Integer[] decrementOdd(Integer[] input) {
return null;
for (int i = 0; i < input.length; i++){
if (input[i] % 2 != 0){
input[i]-=1;
}
}
return input;
}
}
34 changes: 32 additions & 2 deletions src/main/java/com/dtcc/exams/arrays/StringArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ public class StringArrayUtils {
* @return an array with all elements between `startingIndex` and `endingIndex`
*/
public static String[] getSubArray(String[] arrayToBeSpliced, int startingIndex, int endingIndex) {
return null;
int index = 0;
String[] splicedArray;
if(startingIndex >= 0 && endingIndex >= 0 && startingIndex < arrayToBeSpliced.length && endingIndex < arrayToBeSpliced.length){
splicedArray = new String[endingIndex-startingIndex];
for (int i = startingIndex; i < endingIndex; i++) {
splicedArray[index] = arrayToBeSpliced[i];
index++;
}
}
else if(startingIndex > arrayToBeSpliced.length && endingIndex > arrayToBeSpliced.length){
throw new IndexOutOfBoundsException();
}
else{
throw new IllegalArgumentException();
}
return splicedArray;
}


Expand All @@ -18,6 +33,21 @@ public static String[] getSubArray(String[] arrayToBeSpliced, int startingIndex,
* @return an array all elements between after `startingIndex`
*/
public static String[] getEndingArray(String[] arrayToBeSpliced, int startingIndex) {
return null;
int index = 0;
String[] splicedArray;
if(startingIndex >= 0 && startingIndex < arrayToBeSpliced.length){
splicedArray = new String[arrayToBeSpliced.length-startingIndex];
for (int i = startingIndex; i < arrayToBeSpliced.length; i++) {
splicedArray[index] = arrayToBeSpliced[i];
index++;
}
}
else if(startingIndex > arrayToBeSpliced.length){
throw new IllegalArgumentException();
}
else{
throw new IndexOutOfBoundsException();
}
return splicedArray;
}
}
22 changes: 19 additions & 3 deletions src/main/java/com/dtcc/exams/atm/Account.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package com.dtcc.exams.atm;

public class Account {

private double balance;

public Account(double v) {
this.balance = v;
}

public double balance() {
return 0.0;
return this.balance;
}

public boolean closeAccount() {
return false;
boolean canClose = false;
if(balance == 0){
canClose = true;
}
return canClose;
}

public void deposit(double v) {
this.balance += v;

}

public Double withdraw(double v) {
return 0.0;
if(this.balance >= v){
this.balance = this.balance - v;
}
return this.balance;
}

public void transfer(Account b, double v) {
if(this.balance >= v){
b.balance = b.balance + v;
this.balance = this.balance - v;
}
}
}
31 changes: 27 additions & 4 deletions src/main/java/com/dtcc/exams/collections/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package com.dtcc.exams.collections;

import java.util.HashMap;
import java.util.List;

/**
* Use a map to keep track of inventory in a store
*/
public class Inventory {
HashMap<String, Integer> inventory= new HashMap<>();
/**
* @param strings list of strings to add / remove / fetch from
*/
public Inventory(List<String> strings) {

for(int i = 0; i< strings.size(); i++){
if(this.inventory.containsKey(strings.get(i))){
int j = this.inventory.get(strings.get(i)) + 1;
this.inventory.put(strings.get(i), j);
}
else{
this.inventory.put(strings.get(i), 1);
}
}
this.inventory = inventory;
}

/**
Expand All @@ -24,21 +35,33 @@ public Inventory() {
* @param item - increment the number of this item in stock by 1
*/
public void addItemToInventory(String item) {
return;
if(this.inventory.containsKey(item)){
int quantity = this.inventory.get(item);
this.inventory.put(item, quantity +1);
}
else {
this.inventory.put(item, 1);
}
}

/**
* @param item - decrement the number of this item in stock by 1
*/
public void removeItemFromInventory(String item) {
return;
if(this.inventory.containsKey(item)){
this.inventory.put(item, this.inventory.get(item) -1);
}
}

/**
* @param item - Search for this item in stock
* @return - return the number of items
*/
public Integer getItemQuantity(String item) {
return null;
int quantity = 0;
if(this.inventory.containsKey(item)){
quantity = this.inventory.get(item);
}
return quantity;
}
}
60 changes: 50 additions & 10 deletions src/main/java/com/dtcc/exams/collections/MonthConversion.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,101 @@
package com.dtcc.exams.collections;

import java.util.HashMap;
import java.util.Set;

/**
* Use a map to solve
*/
public class MonthConversion {

HashMap<Integer, String> conversionMap = new HashMap<>();
/**
* @param monthNumber - ordinal of month in the year; i.e. January = 1, February = 2
* @param monthName - name of month
// * @param monthNumber - ordinal of month in the year; i.e. January = 1, February = 2
// * @param monthName - name of month
*/
public void add(Integer monthNumber, String monthName) {

public MonthConversion(){
this.conversionMap = conversionMap;
}

public void add(Integer monthNumber, String monthName) {
this.conversionMap.put(monthNumber, monthName);
}



/**
* @param monthNumber - ordinal of month in the year
* @return the name of the respective month
*/
public String getName(Integer monthNumber) {
throw new NullPointerException();
String monthName = null;
if(this.conversionMap.containsKey(monthNumber)){
monthName = this.conversionMap.get(monthNumber);
}
// else{
// throw new NullPointerException();
// }
return monthName;
}

/**
* @param monthName - name of month
* @return - the ordinal of the month in the year
*/
public int getNumber(String monthName) {
return (Integer)null;
public Integer getNumber(String monthName) {
Set<Integer> keys = this.conversionMap.keySet();
Integer numberResult = null;
for(Integer number : keys){
if(this.conversionMap.get(number).equals(monthName)){
numberResult = number;
}
}
return numberResult;
}

/**
* @param monthNumber
* @return true if the monthNumber is in the keySet
*/
public Boolean isValidNumber(Integer monthNumber) {
return null;
boolean flag = false;
if(this.conversionMap.containsKey(monthNumber)){
flag = true;
}
return flag;
}

/**
* @param monthName
* @return true if the monthName is in the valueSet
*/
public Boolean isValidMonth(String monthName) {
return null;
boolean flag = false;
Set<Integer> keys = this.conversionMap.keySet();
for(Integer number : keys){
if(this.conversionMap.get(number).equals(monthName)){
flag = true;
}
}
return flag;
}

/**
* @return number of entries in this mapping
*/
public Integer size() {
return -1;
int size = 0;
Set keys = this.conversionMap.keySet();
size = keys.size();
return size;
}

/**
* @param monthNumber - number of month in year
* @param monthName - name of month
*/
public void update(Integer monthNumber, String monthName) {

this.conversionMap.put(monthNumber, monthName);
}
}
Loading