Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class AssetCollection {
- static ArrayList<Asset> assetList = new ArrayList<Asset>();
- static int numOfAssets;
- static Asset Asset = new Asset();
- /*
- * Method requests input for information to be stored into the ArrayList.
- */
- public static void addAsset() {
- String assetName;
- String assetType;
- int count = 0;
- double assetNum = -1;
- Scanner Scan = new Scanner(System.in);
- do {
- System.out.println("How many assets would you like to add?");
- while (!Scan.hasNextInt()) {
- System.out.println("You must enter a number.");
- Scan.next(); // this is important!
- }
- count = Scan.nextInt();
- } while(count <= 0);
- Scan.nextLine();
- /*
- * Populates an arraylist with Asset objects. Fields are populated, then the object is added to the assetList.
- */
- for(int i = 0; i < count; i++) {
- System.out.println("Enter asset name.");
- assetName = Scan.nextLine();
- System.out.println("Enter asset type.");
- assetType = Scan.nextLine();
- System.out.println("Enter a number between 0 and 1 for the asset's value.");
- assetNum = Scan.nextDouble();
- Scan.nextLine();
- assetList.add(new Asset(assetName, assetType, assetNum));
- }
- }
- /*
- * Method that outputs the contents of the assetList arraylist.
- */
- public static void listAssets() {
- System.out.println(assetList);
- }
- /*
- * Method searches based upon the Asset's name and removes it from the arrayList based on its determined Index.
- */
- public static void deleteAsset() {
- Scanner Scan = new Scanner(System.in);
- System.out.println("Enter the name of the asset you wish to remove.");
- String assetToRemove = Scan.nextLine();
- boolean found = false;
- /*
- * Loop cycles through indexes of assetList in order to
- * check if the provided input matches any Asset.
- * If the object with input name exists, it is removed,
- * else the loop stops.
- */
- for(int i = 0; i < assetList.size(); i++) {
- /*
- * Current index of assetList is retrieved in order to gain access to field of object in respective index.
- */
- Asset = assetList.get(i);
- /*
- * Comparison between the input and current index's asset name.
- * If found, it is removed, else the loop stops.
- */
- if(assetToRemove.equals(Asset.getName())) {
- assetList.remove(i);
- System.out.println("Asset " + assetToRemove + " has been removed. \n");
- found = true;
- }
- }
- if(!found) {
- System.out.println("No assets with the provided name were found. \n");
- }
- }
- /*
- * This method uses a loop to compare each "AssetNum" field of each object.
- * It does so to the size of the arraylist, and once a value is determined,
- * its corresponding "name" is formatted to the output.
- */
- public static void mostValuedAsset() {
- double highest = 0;
- String mostValuable = "";
- for(int i = 0; i < assetList.size(); i++){
- Asset x = (Asset)assetList.get(i);
- if(x.getAssetNum() > highest)
- highest = x.getAssetNum();
- mostValuable = x.getName();
- System.out.println(mostValuable);
- }
- System.out.println("The most valuable asset is: " + mostValuable);
- }
- /*
- * Simple use of the size() method to determine the arraylist's size.
- */
- public static void numAssets() {
- int sizeOfList = assetList.size();
- System.out.println("Total number of assets: " + sizeOfList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement