Advertisement
Guest User

Untitled

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