Advertisement
Guest User

Untitled

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