Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class P8_16
  4. {
  5. private static double totalPrice;
  6. private static int itemCount;
  7. private ArrayList<Double> items; //array for storing the values
  8.  
  9. public P8_16()//constructor
  10. {
  11. this.totalPrice=0;
  12. this.itemCount=0;
  13.  
  14. }
  15.  
  16. private static ArrayList<Double> readUserInput()
  17. {
  18. Scanner in = new Scanner(System.in);
  19. ArrayList<Double> inputs = new ArrayList<Double>();
  20. while(in.hasNextDouble())
  21. {
  22. inputs.add(in.nextDouble());
  23. }
  24.  
  25. return inputs;
  26.  
  27. }
  28.  
  29. public static int getCount(ArrayList<Double> prices)
  30. {
  31.  
  32. int i = 0;
  33. while(i< prices.size())
  34. {
  35. itemCount++;
  36. i++;
  37. }
  38. return itemCount;
  39. }
  40.  
  41.  
  42. public static double addItem(ArrayList<Double> prices)
  43. {
  44. Scanner in = new Scanner(System.in);
  45.  
  46. int i = 0;
  47. while(i< prices.size())
  48. {
  49. totalPrice = totalPrice + prices.get(i);
  50. i++;
  51.  
  52. }
  53. return totalPrice;
  54. }
  55.  
  56.  
  57. public void clear()
  58. {
  59. itemCount = 0;
  60. totalPrice = 0;
  61. }
  62.  
  63. public static void main (String [] args)
  64. {
  65. System.out.println("Please enter the values (price), enter 0 to quit");
  66. ArrayList<Double> prices = readUserInput();
  67. int count = getCount(prices);
  68. System.out.println("The count is: " + count);
  69. System.out.println("The total price is: " + addItem(prices));
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement