Advertisement
Guest User

Untitled

a guest
May 4th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. package freeWork;
  2.  
  3. import java.util.*;
  4.  
  5. class ticketInfo {
  6.  
  7. private String userName = "";
  8. private String ticketID = "";
  9.  
  10. @Override
  11. public String toString() {
  12. return ("Username: " + this.getUserName() + " TicketID: " + this.getTicketID());
  13. }
  14.  
  15. public ticketInfo(String userName, String ticketID) {
  16. this.userName = userName;
  17. this.ticketID = ticketID;
  18. }
  19.  
  20. /*
  21. * public void setUserName(String userName) { this.userName = userName; }
  22. *
  23. * public void setTicketID(String ticketID) { this.ticketID = ticketID; }
  24. */
  25. public String getUserName() {
  26. return userName;
  27. }
  28.  
  29. public String getTicketID() {
  30. return ticketID;
  31. }
  32.  
  33. public void printTicketInfo() {
  34.  
  35. System.out.println("Username: " + userName + " TicketID: " + ticketID);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. public class testCode {
  42.  
  43. // checks to see if userName matches registered tickets in arraylist
  44. private static ticketInfo findTicketByName(ArrayList<ticketInfo> ticketList, String userName) {
  45. for (int i = 0; i < ticketList.size(); i++) {
  46. if (ticketList.get(i).getUserName().equals(userName)) {
  47. return ticketList.get(i);
  48. }
  49. }
  50. return null;
  51. }
  52.  
  53. private static final Scanner sc = new Scanner(System.in);
  54. private static final Scanner sc2 = new Scanner(System.in);
  55.  
  56. public static void main(String[] args) {
  57.  
  58. // variables
  59.  
  60. double balance = 800;
  61. int ticketPrice = 8;
  62. String ticketID = UUID.randomUUID().toString();
  63. String userName = "";
  64. int recieptNo;
  65. int travelPass;
  66. int zones;
  67. double charge = 0;
  68. double oldcharge = 0;
  69. int reload;
  70. double spend = 0;
  71.  
  72. String input;
  73. String fundsinput;
  74. char selection = '\0';
  75.  
  76. // List<String> tickets = new ArrayList<String>();
  77. int ticketCount = 0;
  78. ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
  79.  
  80. do {
  81. // display menu options
  82. System.out.println("*** Cinco Ticketing System ***");
  83. System.out.println();
  84. System.out.println("A - Purchase Ticket");
  85. System.out.println("B - Display Ticket Info");
  86. System.out.println("C - Reload PepPep Ticket");
  87. System.out.println("D - Purchase Travel Pass");
  88. System.out.println("X - Quit");
  89.  
  90. System.out.println("Enter your selection: ");
  91. input = sc.nextLine();
  92.  
  93. if (input.length() != 1) {
  94. System.out.println("Error - Selection must be a single character!");
  95. } else {
  96. selection = Character.toUpperCase(input.charAt(0));
  97.  
  98. // process the user's selection
  99. switch (selection) {
  100. case 'A':
  101.  
  102. // purchase ticket option
  103.  
  104. /*
  105. * if (balance < ticketPrice) {
  106. * System.out.println("Not enough funds to purchase ticket! You need $8."); }
  107. * else { System.out.println("Enter a username"); name = sc.nextLine(); balance
  108. * = balance - ticketPrice; System.out.println("Purchasing...");
  109. * //tickets.add(ticketID); //tickets.add(userName);
  110. * System.out.println("Ticket purchased!");
  111. * System.out.println("Your balance is: $" + balance);
  112. * System.out.println("Returning you to the main menu..."); }
  113. */
  114.  
  115. System.out.println("Enter Username: ");
  116. userName = sc.nextLine();
  117. balance = balance - ticketPrice;
  118. System.out.println("Purchasing...");
  119. System.out.println("Ticket purchased!");
  120. System.out.println("Your balance is: $" + balance);
  121. System.out.println("Returning you to the main menu...");
  122.  
  123. // ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
  124. ticketList.add(new ticketInfo(userName, ticketID));
  125.  
  126. break;
  127.  
  128. case 'B':
  129.  
  130. System.out.println("Display Ticket Information: ");
  131. for (int i = 0; i < ticketList.size(); i++) {
  132. // System.out.println(ticketList.get(i));
  133. ticketList.get(i).printTicketInfo();
  134. }
  135. // System.out.println(ticketList);
  136. break;
  137.  
  138. case 'C':
  139. System.out.println("Reload Ticket Selected!");
  140. System.out.println("Enter username: ");
  141. userName = sc.nextLine();
  142.  
  143. if (userName == ticketInfo.findTicketByName)
  144. System.out.println("How much would you like to reload on your card?");
  145. reload = sc2.nextInt();
  146. oldcharge = charge;
  147. // get the reload amount the user wish's to enter
  148. while (!(reload > 0 && reload <= 100 && charge < 100 && reload % 5 == 0)) {
  149. System.out.println(
  150. "ERROR: Ticket reloads are capped at $100, and must be divisible by 5. Please try again.");
  151. // offer option to exit loop
  152. System.out.println("if you wish to cancel reload please enter 0");
  153. reload = sc2.nextInt();
  154. if (reload == 0) {
  155. break;
  156. }
  157. }
  158. if (balance >= reload) {
  159. // update the charge on the card
  160. charge = oldcharge + reload;
  161. // deduct the charge on the card
  162. balance = balance - reload;
  163. // let user know the card is reloaded
  164. System.out.println("Reloaded!");
  165. // advise the customer of the new figure
  166. System.out.println(
  167. "You have been charged: $" + charge + "and your current balance is: $" + balance);
  168. System.out.println("Returning you to the main menu...");
  169. System.out.println();
  170. }
  171.  
  172. // if user cannot afford the recharge
  173. else {
  174. // advise user insufficient funds
  175. System.out.println("Sorry you have insufficent funds for the recharge...");
  176. System.out.println("Returning you to the main menu...");
  177.  
  178. }
  179. break;
  180.  
  181. case 'D':
  182.  
  183. System.out.println("Enter username: ");
  184. userName = sc.nextLine();
  185.  
  186. for (int i = 0; i < ticketList.size(); i++) {
  187. if (!ticketList.get(i).getUserName().equals(userName)) {
  188. System.out.println("Error! Name not in database.");
  189. }
  190.  
  191. if (ticketList.get(i).getUserName().equals(userName)) {
  192. System.out.println(
  193. "Are you buying a (1) all day or (2) 2 hour ticket? (Please enter 1 or 2)");
  194. travelPass = sc2.nextInt();
  195. System.out.println("Are you travelling 1 or 2 zones today? (Please enter 1 or 2)");
  196. zones = sc2.nextInt();
  197.  
  198. if (travelPass == 1) // if it's an all day ticket
  199. {
  200. // First if statement
  201. if (zones == 1) {
  202. // check if there is sufficent charge for an all day Zone 1 ticket
  203. if (charge >= 7) {
  204. // if succesful deduct the charge off the card
  205. charge = charge - 7;
  206. // spend is code for the purpose of the message at end telling user that funds
  207. // was deducted
  208. spend = 7;
  209. // advise cust of their purchase
  210. System.out.println(travelPass + " ticket purchased, " + zones
  211. + " zones travelled, $" + spend + " charged");
  212.  
  213. }
  214. // if not enough funds warn user of this and advise to topup PepPep
  215. else {
  216. System.out
  217. .println("Sorry you do not have enough credit to purchase this ticket");
  218. System.out.println("Please top up your PepPepTicket before trying again");
  219. }
  220.  
  221. }
  222. }
  223.  
  224. // if Zone 1 + 2 all day
  225. if (zones == 2) {
  226. // check if suffucient funds
  227. if (charge >= 12) {
  228. charge = charge - 12; // customer charged $12 for two zones on all day ticket
  229. // spend is code for the purpose of the message at end telling user that funds
  230. // was deducted
  231. spend = 12;
  232. // advise cust of their purchase
  233. System.out.println(travelPass + " ticket purchased, " + zones
  234. + " zones travelled, $" + spend + " charged");
  235.  
  236. }
  237. // if not enough funds warn user of this and advise to topup PepPep
  238. else {
  239. System.out.println("Sorry you do not have enough credit to purchase this ticket");
  240. System.out.println("Please top up your PepPepTicket before trying again");
  241. }
  242. }
  243.  
  244. // if for a 2 hourly pass
  245. if (travelPass == 2) {
  246. // if it's a 2 hour day ticket
  247. if (zones == 1) {
  248. // check user has sufficent charge to pay for ticket
  249. if (charge >= 3.5) {
  250. // customer charged $3.50 for one zone on two hour ticket
  251. charge = charge - 3.5;
  252. // spend is code for the purpose of the message at end telling user that funds
  253. // was deducted
  254. spend = 3.5;
  255. // advise cust of their purchase
  256. System.out.println(travelPass + " ticket purchased, " + zones
  257. + " zones travelled, $" + spend + " charged");
  258. } else {
  259. System.out
  260. .println("Sorry you do not have enough credit to purchase this ticket");
  261. System.out.println("Please top up your PepPepTicket before trying again");
  262. }
  263. }
  264. if (zones == 2) {
  265. // check if user has enough funds for charge
  266. if (charge >= 6) {
  267. // customer charged $6 for one zone on two hour ticket
  268. charge = charge - 6;
  269. // spend is code for the purpose of the message at end telling user that funds
  270. // was deducted
  271. spend = 6;
  272. // advise cust of their purchase
  273. System.out.println(travelPass + " ticket purchased, " + zones
  274. + " zones travelled, $" + spend + " charged");
  275.  
  276. } else {
  277. System.out
  278. .println("Sorry you do not have enough credit to purchase this ticket");
  279. System.out.println("Please top up your PepPepTicket before trying again");
  280. }
  281. }
  282. }
  283.  
  284. System.out.println("Returning you to the main menu...");
  285. System.out.println();
  286. }
  287. }
  288.  
  289. break;
  290.  
  291. }
  292.  
  293. }
  294. } while (selection != 'X');
  295.  
  296. }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement