Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class AssetCollection {
  5.  
  6. static ArrayList<Asset> assetList = new ArrayList<Asset>();
  7. static int numOfAssets;
  8. static Asset Asset = new Asset();
  9.  
  10. /*
  11. * Method requests input for information to be stored into the ArrayList.
  12. */
  13. public static void addAsset() {
  14.  
  15. String assetName;
  16. String assetType;
  17. int count = 0;
  18. double assetNum = -1;
  19.  
  20. Scanner Scan = new Scanner(System.in);
  21.  
  22. do {
  23. System.out.println("How many assets would you like to add?");
  24. while (!Scan.hasNextInt()) {
  25. System.out.println("You must enter a number.");
  26. Scan.next(); // this is important!
  27. }
  28. count = Scan.nextInt();
  29. } while(count <= 0);
  30.  
  31. Scan.nextLine();
  32.  
  33. /*
  34. * Populates an arraylist with Asset objects. Fields are populated, then the object is added to the assetList.
  35. */
  36. for(int i = 0; i < count; i++) {
  37. System.out.println("Enter asset name.");
  38. assetName = Scan.nextLine();
  39. System.out.println("Enter asset type.");
  40. assetType = Scan.nextLine();
  41. System.out.println("Enter a number between 0 and 1 for the asset's value.");
  42. assetNum = Scan.nextDouble();
  43. Scan.nextLine();
  44. assetList.add(new Asset(assetName, assetType, assetNum));
  45.  
  46. }
  47. }
  48.  
  49. /*
  50. * Method that outputs the contents of the assetList arraylist.
  51. */
  52. public static void listAssets() {
  53. System.out.println(assetList);
  54.  
  55. }
  56.  
  57. /*
  58. * Method searches based upon the Asset's name and removes it from the arrayList based on its determined Index.
  59. */
  60. public static void deleteAsset() {
  61. Scanner Scan = new Scanner(System.in);
  62. System.out.println("Enter the name of the asset you wish to remove.");
  63. String assetToRemove = Scan.nextLine();
  64. boolean found = false;
  65.  
  66. /*
  67. * Loop cycles through indexes of assetList in order to
  68. * check if the provided input matches any Asset.
  69. * If the object with input name exists, it is removed,
  70. * else the loop stops.
  71. */
  72. for(int i = 0; i < assetList.size(); i++) {
  73. /*
  74. * Current index of assetList is retrieved in order to gain access to field of object in respective index.
  75. */
  76. Asset = assetList.get(i);
  77. /*
  78. * Comparison between the input and current index's asset name.
  79. * If found, it is removed, else the loop stops.
  80. */
  81. if(assetToRemove.equals(Asset.getName())) {
  82. assetList.remove(i);
  83. System.out.println("Asset " + assetToRemove + " has been removed. \n");
  84. found = true;
  85. }
  86. }
  87.  
  88. if(!found) {
  89. System.out.println("No assets with the provided name were found. \n");
  90. }
  91.  
  92. }
  93.  
  94. /*
  95. * This method uses a loop to compare each "AssetNum" field of each object.
  96. * It does so to the size of the arraylist, and once a value is determined,
  97. * its corresponding "name" is formatted to the output.
  98. */
  99. public static void mostValuedAsset() {
  100. double highest = 0;
  101. String mostValuable = "";
  102. for(int i = 0; i < assetList.size(); i++){
  103. Asset x = (Asset)assetList.get(i);
  104. if(x.getAssetNum() > highest)
  105. highest = x.getAssetNum();
  106. mostValuable = x.getName();
  107. System.out.println(mostValuable);
  108. }
  109. System.out.println("The most valuable asset is: " + mostValuable);
  110. }
  111.  
  112.  
  113. /*
  114. * Simple use of the size() method to determine the arraylist's size.
  115. */
  116. public static void numAssets() {
  117. int sizeOfList = assetList.size();
  118. System.out.println("Total number of assets: " + sizeOfList);
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement