Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Furniture
  5. {
  6. public static void main(String[] args)
  7. {
  8. Scanner scan = new Scanner(System.in);
  9. int count = 0;
  10. char input = ']';
  11. String[] nameArray = new String[1];
  12. double[] costArray = new double[1];
  13.  
  14. do
  15. {
  16. System.out.print("What action would you like to perform?\n\n");
  17. printMenu();
  18. input = scan.next().charAt(0);
  19. String uppercase = Character.toString(input);
  20. uppercase = uppercase.toUpperCase();
  21. input = uppercase.charAt(0);
  22. String furnitureName;
  23. if (input == 'A' || input == 'S' || input == 'L' || input == 'C' || input == 'T' || input == 'R' || input == 'Q' || input == '?')
  24. {
  25. switch(input)
  26. {
  27. case 'A':
  28. do
  29. {
  30. System.out.print("Please enter a furniture to add:\n");
  31. furnitureName = scan.next();
  32. nameArray[count] = furnitureName.substring(0, 1).toUpperCase() + furnitureName.substring(1);
  33. if(!furnitureName.equalsIgnoreCase("done"))
  34. {
  35. System.out.print("Please enter a price for the " + furnitureName + ":\n");
  36. double furniturePrice = scan.nextDouble();
  37. costArray[count] = furniturePrice;
  38.  
  39. count++;
  40. String[] nameArray2 = new String[count + 1];
  41. double[] costArray2 = new double[count + 1];
  42.  
  43. for(int i = 0; i < count; i++)
  44. {
  45. nameArray2[i] = nameArray[i];
  46. costArray2[i] = costArray[i];
  47. }
  48. nameArray = nameArray2;
  49. costArray = costArray2;
  50. }
  51. }
  52. while(!furnitureName.equalsIgnoreCase("done"));
  53. break;
  54. case 'S':
  55. System.out.print("Please enter a furniture name to search for:\n");
  56. String searchedFurniture = scan.next();
  57. boolean found = false;
  58. for(int i = 0; i < count; i++)
  59. {
  60. if(nameArray[i].equalsIgnoreCase(searchedFurniture))
  61. {
  62. System.out.print("Furniture Name: " + nameArray[i] + " | Cost: $" + costArray[i]);
  63. found = true;
  64. }
  65. if(found = false)
  66. {
  67. System.out.print("No furniture by the name '" + searchedFurniture + "' was found.\n");
  68. }
  69. }
  70. break;
  71. case 'L':
  72. System.out.println("\n");
  73. System.out.println("Furniture List:\n----------------------------------------\n");
  74. for(int i = 0; i < count; i++)
  75. {
  76. System.out.print("Furniture Name: " + nameArray[i] + "\t\t| Cost: " + costArray[i] + "\n");
  77. }
  78. System.out.println();
  79. break;
  80. case 'C':
  81. int finalCount = count;
  82. System.out.println("There are currently " + finalCount + " furniture in the list.");
  83. System.out.println();
  84. break;
  85. case 'T':
  86. double price = 0;
  87. for(int i = 0; i < count; i++)
  88. {
  89. price += costArray[i];
  90. }
  91. System.out.println("Total Cost: $" + price);
  92. break;
  93. case 'R':
  94. System.out.print("Please enter the name of a furniture you would like to remove:\n");
  95. String removedFurniture = scan.next();
  96. int counting = count - 1;
  97. boolean find = false;
  98. boolean furniFound = false;
  99.  
  100. String[] nameArray3 = new String[counting];
  101. double[] costArray3 = new double[counting];
  102.  
  103. int c = 0;
  104. while(furniFound == false)
  105. {
  106. if(nameArray[c].equalsIgnoreCase(removedFurniture))
  107. {
  108. find = true;
  109. furniFound = true;
  110. }
  111. else
  112. {
  113. find = false;
  114. }
  115. c++;
  116. }
  117. if(find == false)
  118. {
  119. System.out.print("No furniture with that name was found.\n");
  120. }
  121. if(find == true)
  122. {
  123. int counter = 0;
  124. for(int i = 0; i < count; i++)
  125. {
  126. if(nameArray[i].equalsIgnoreCase(removedFurniture))
  127. {
  128. counter--;
  129. }
  130. else
  131. {
  132. nameArray3[counter] = nameArray[i];
  133. costArray3[counter] = costArray[i];
  134. counter++;
  135. }
  136. }
  137. nameArray = nameArray3;
  138. costArray = costArray3;
  139. System.out.print("Furniture removed.\n");
  140. }
  141. break;
  142. case 'Q':
  143. break;
  144. case '?':
  145. printMenu();
  146. break;
  147.  
  148. }
  149. }
  150. }
  151. while(input != 'Q');
  152. }
  153. public static void printMenu()
  154. {
  155. System.out.print("Choice\t\tAction\n" +
  156. "------\t\t------\n" +
  157. "A\t\tAdd Furniture\n" +
  158. "S\t\tSearch for Furniture\n" +
  159. "L\t\tList Current Furniture\n" +
  160. "C\t\tCount Furniture\n" +
  161. "T\t\tTotal Cost\n" +
  162. "R\t\tRemove Furniture\n" +
  163. "Q\t\tQuit\n" +
  164. "?\t\tDisplay Help\n\n");
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement