Helping a redditor


SUBMITTED BY: Guest

DATE: Oct. 4, 2014, 8:24 p.m.

FORMAT: Text only

SIZE: 2.3 kB

HITS: 1390

  1. import java.util.Scanner;
  2. import java.util.*;
  3. public class Home
  4. {
  5. public static void main(String[] args)
  6. {
  7. int ticketCounter = 0; // Number of tickets sold
  8. double moneyCollected = 0; // Sum of money collected
  9. int price = 42; // You didn't write what the price is, so I decided.;
  10. Scanner input = new Scanner(System.in); // This is for input, probably replaceable with JOptionPane
  11. int age; // The age of the person buying a ticket
  12. System.out.println("What is your age?"); //JOptionPane again, just presenting the user with some text
  13. age = input.nextInt(); // Getting the input and putting it in the age, JOptionPane again
  14. while (age != -1) // This is the loop for multiple inputs. If a person writes their age as -1, it stops getting inputs
  15. {
  16. if (age <=5)
  17. {
  18. ticketCounter++; // You sold a ticket, add one to the counter
  19. System.out.println("That ticket was free");//again, output with JOptionPane. Since it was free there is nothing to add to the amount collected
  20. }
  21. else if ((6<=age) && (age<=12))
  22. {
  23. ticketCounter++; //Sold a ticket
  24. moneyCollected+= price*0.5;//Adding half the price of a ticket because of the discount to the sum
  25. System.out.println("That ticket was 50% off");//output
  26. }
  27. else if ((age>=13)&&(age<=54))
  28. {
  29. ticketCounter++;//Sold a ticket
  30. moneyCollected+=price;//full ticket price
  31. System.out.println("That ticket wasn't at a discount");//output
  32. }
  33. else if(age>=55)
  34. {
  35. ticketCounter++;//Sold a ticket
  36. moneyCollected+=price*0.25;//75% discount
  37. System.out.println("That ticket was at a 75% discount");//output
  38. }
  39. System.out.println("What is your age? (type -1 to stop getting age)");//output
  40. age = input.nextInt(); // Getting the new age, which could be -1 which will stop the loop
  41. }
  42. //Now if someone inputs the age of -1, it stops the loop so you can see how many tickets you sold and what's the sum of money you got
  43. System.out.println("You sold "+ticketCounter+" tickets");
  44. System.out.println("You collected "+moneyCollected+" Dollars");
  45. }
  46. }

comments powered by Disqus