Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FundScreen has two methods that you are to modify: keep and exclude . Your mission is to complete these methods using only calls to ArrayLists

FundScreen has two methods that you are to modify: keep and exclude. Your mission is to complete these methods using only calls to ArrayLists removeIf method. Information on removeIf is in the ArrayList description. Google oracle java 8 api ArrayList to find Oracles official documentation for ArrayList. Locations for calls to removeIf are noted with TODO flags in the code.

keep

The keep method allows the user to specify which mutual funds should be kept in the portfolio. All other funds will be removed.

ALL CALLS TO removeIf IN THE keep METHOD ARE TO USE ANONYMOUS CLASSES AS THE Predicate.

Since we havent covered enum, the Domicile processing of the keep method is completed for you.

exclude

The exclude method is the opposite of keep. It allows the user to specify which funds are to be removed from the portfolio.

Sample code:

import java.util.Scanner; import java.util.function.Predicate;

public class FundScreen {

public static final String LIST = "List"; public static final String EXCLUDE = "Exclude"; public static final String KEEP = "Retain"; public static final String RESET_PORTFOLIO = "Reset Portfolio"; public static final String EXIT = "Exit";

public static final String TICKER = "Ticker"; public static final String AVG_HOLDING_SIZE = "Average Holding Size"; public static final String MIN_INVESTMENT = "Minimum Investment"; public static final String VALUE_MEASURE = "Value Measure"; public static final String DOMICILE = "Domicile";

public static final String[] BIG_MENU = { LIST, EXCLUDE, KEEP, RESET_PORTFOLIO, EXIT, };

public static final String[] FUND_FIELDS = { TICKER, AVG_HOLDING_SIZE, MIN_INVESTMENT, VALUE_MEASURE, DOMICILE, };

public static void main(String[] args) { Scanner kybd = new Scanner(System.in); Portfolio portfolio = new Portfolio(); String userChoice = ""; while (!EXIT.equals(userChoice)) { userChoice = Utils.userChoose(kybd, BIG_MENU); if (LIST.equals(userChoice)) { System.out.println("Current Portfolio"); Utils.userDisplay(kybd, portfolio); } else if (EXCLUDE.equals(userChoice)) { exclude(kybd, portfolio); } else if (KEEP.equals(userChoice)) { keep(kybd, portfolio); } else if (RESET_PORTFOLIO.equals(userChoice)) { portfolio.refresh(); } } }

private static void keep(Scanner kybd, Portfolio portfolio) { System.out.println("What field and values identify funds you wish to keep?"); String keeping = Utils.userChoose(kybd, FUND_FIELDS); if (TICKER.equals(keeping)) { System.out.print("Which ticker? "); String ticker = kybd.next().toUpperCase(); // TODO call portfolio.removeIf(...); } else if (AVG_HOLDING_SIZE.equals(keeping)) { System.out.print("Lower limit for holding size: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for holding size: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...);

} else if (MIN_INVESTMENT.equals(keeping)) { System.out.print("Lower limit for minimum investment: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for minimum investment: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...);

} else if (VALUE_MEASURE.equals(keeping)) { System.out.print("Lower limit for value: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for value: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...);

} else if (DOMICILE.equals(keeping)) { keeping = Utils.userChoose(kybd, MutualFund.MARKET.values()); MutualFund.MARKET mkt = MutualFund.MARKET.valueOf(keeping); // this one is supplied for you portfolio.removeIf(new Predicate() {

@Override public boolean test(MutualFund t) { System.out.println(t); return !(mkt == t.getDomicile()); } });

} }

private static void exclude(Scanner kybd, Portfolio portfolio) { System.out.println("What field and values identify funds you wish to remove?"); String removing = Utils.userChoose(kybd, FUND_FIELDS); if (TICKER.equals(removing)) { System.out.print("Which ticker? "); String ticker = kybd.next().toUpperCase(); // TODO call portfolio.removeIf(...);

} else if (AVG_HOLDING_SIZE.equals(removing)) { System.out.print("Lower limit for holding size: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for holding size: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...);

} else if (MIN_INVESTMENT.equals(removing)) { System.out.print("Lower limit for minimum investment: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for minimum investment: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...); } else if (VALUE_MEASURE.equals(removing)) { System.out.print("Lower limit for value: "); double floor = kybd.nextDouble(); System.out.print("Upper limit for value: "); double ceiling = kybd.nextDouble(); // TODO call portfolio.removeIf(...);

} else if (DOMICILE.equals(removing)) { removing = Utils.userChoose(kybd, MutualFund.MARKET.values()); MutualFund.MARKET mkt = MutualFund.MARKET.valueOf(removing); // TODO call portfolio.removeIf(...); } }

}

public class MutualFund { private String ticker; private double avgHoldingSize; private double minimumInvestment; private double valueMeasure; private MARKET domicile;

public enum MARKET {DOMESTIC, INTERNATIONAL, GLOBAL};

public MutualFund(String ticker, double averageHoldingSize, double minimumInvestmentRequirement, double valueMeasure, MARKET location) { this.ticker = ticker; avgHoldingSize = averageHoldingSize; minimumInvestment = minimumInvestmentRequirement; this.valueMeasure = valueMeasure; domicile = location; }

public double getAvgHoldingSize() { return avgHoldingSize; }

public void setAvgHoldingSize(double avgHoldingSize) { this.avgHoldingSize = avgHoldingSize; }

public double getMinimumInvestment() { return minimumInvestment; }

public void setMinimumInvestment(double minimumInvestment) { this.minimumInvestment = minimumInvestment; }

public double getValueMeasure() { return valueMeasure; }

public void setValueMeasure(double valueMeasure) { this.valueMeasure = valueMeasure; }

public String getTicker() { return ticker; }

public MARKET getDomicile() { return domicile; }

@Override public String toString() { return "MutualFund [ticker=" + ticker + ", avgHoldingSize=" + avgHoldingSize + ", minimumInvestment=" + minimumInvestment + ", valueMeasure=" + valueMeasure + ", domicile=" + domicile + "]"; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions