Advertisement
Guest User

Untitled

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