Advertisement
Guest User

Untitled

a guest
May 10th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. package pepPepPackage;
  2.  
  3. import java.util.Scanner;
  4. import java.util.UUID;
  5.  
  6. import java.util.Date;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. class ticketInfo {
  11.  
  12. private String name;
  13. private String ticketID;
  14. private double charge;
  15. private Date date;
  16.  
  17. // constructor with customer detail parameters
  18. public ticketInfo(Date date, String name, String ticketID, double charge) {
  19. this.date = date;
  20. this.name = name;
  21. this.ticketID = ticketID;
  22. this.charge = charge;
  23.  
  24. }
  25.  
  26. // method to ensure string input is display - not object listing
  27. @Override
  28. public String toString() {
  29. return ("Username: " + this.getName() + " TicketID: " + this.getTicketID() + "Charge: " + this.getCharge() + "Date: " + this.getDate());
  30. }
  31.  
  32. // setters
  33. public void setCharge(double charge) {
  34. this.charge = charge;
  35. }
  36.  
  37. public void setName(String name) {
  38. this.name = name;
  39. }
  40.  
  41. public void setTicketID(String ticketID) {
  42. this.ticketID = ticketID;
  43. }
  44.  
  45. public void setTicketID(Date date) {
  46. this.date = date;
  47. }
  48.  
  49. // getters
  50. public double getCharge() {
  51. return charge;
  52. }
  53.  
  54. public String getName() {
  55. return name;
  56. }
  57.  
  58. public String getTicketID() {
  59. return ticketID;
  60. }
  61.  
  62. public Date getDate() {
  63. return date;
  64. }
  65.  
  66. public void printTicketInfo() {
  67. System.out.println("Date: " + date + ", Customer Name: " + name + ", TicketID: " + ticketID + ", Charge: " + charge);
  68. }
  69. }
  70.  
  71. public class PepPepClass {
  72.  
  73. // method to check if userName matches registered tickets in arrayList
  74. private static ticketInfo findTicketByName(ArrayList<ticketInfo> ticketList, String name) {
  75. for (int i = 0; i < ticketList.size(); i++) {
  76. if (ticketList.get(i).getName().equalsIgnoreCase(name)) {
  77. return ticketList.get(i);
  78. }
  79. }
  80. return null;
  81. }
  82.  
  83. private static final Scanner sc = new Scanner(System.in);
  84. private static final Scanner sc2 = new Scanner(System.in);
  85.  
  86. public static void main(String[] args) {
  87.  
  88. // declaring variables
  89. // balance is the customers bank balance
  90. double balance = 200;
  91. // setting the value for ticketPrice;
  92. int ticketPrice = 8;
  93. // setting customer name
  94. String name = "";
  95. // setting tickets ID
  96. String ticketID = UUID.randomUUID().toString();
  97. // variable for receiptNo
  98. int receiptNo;
  99. // travelPass is a variable telling us if its daily or 2 hourly
  100. int travelPass;
  101. // zones is a variable for either zone 1 or zone 1+2
  102. int zones;
  103. // charge is balance of PepPepticket
  104. double charge = 0;
  105. // reload is the value the customer enters when trying to recharge their
  106. // PepPepticket
  107. int reload;
  108. // oldcharge is to help create a variable to replace the charge with the new
  109. // charge figure after toping up
  110. double oldcharge;
  111. // spend is for the purpose of how much charge customers try to use when buying
  112. // a Zone 1 or Zone 1 + 2 ticket
  113. double spend = 0;
  114. // date variable
  115. Date date = new Date();
  116.  
  117. String input;
  118. String fundsinput;
  119. char selection = '\0';
  120.  
  121. ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
  122.  
  123. do {
  124. // display menu options
  125. System.out.println();
  126. System.out.println("****** Cinco Ticketing System ******");
  127. System.out.println();
  128. System.out.println("A - Purchase Ticket");
  129. System.out.println("B - Recent Transactions");
  130. System.out.println("C - Display account details and current balance");
  131. System.out.println("D - Reload Ticket");
  132. System.out.println("E - Buy Ticket Pass");
  133. System.out.println("X - Quit");
  134. System.out.println();
  135.  
  136. // prompt the user to enter an option
  137. System.out.println("Enter your selection: ");
  138. input = sc.nextLine();
  139.  
  140. System.out.println();
  141.  
  142. // check to see if the user failed to enter exactly one character
  143. // for their menu selection
  144.  
  145. if (input.length() != 1) {
  146. System.out.println("Error - selection must be a single character!");
  147. } else {
  148. selection = Character.toUpperCase(input.charAt(0));
  149.  
  150. // process the user's selection
  151. switch (selection) {
  152. case 'A':
  153.  
  154. // Purchase Ticket Option
  155. ticketInfo validateUsername3 = findTicketByName(ticketList, name);
  156. System.out.println("Enter your name: ");
  157. name = sc.nextLine();
  158.  
  159. if(validateUsername3 == null) {
  160. if (balance < ticketPrice) {
  161. System.out.println("Not enough funds to purchase a ticket you need $8");
  162. } else {
  163.  
  164. balance = balance - ticketPrice;
  165. System.out.println("Purchasing...");
  166. System.out.println("Ticket purchased!");
  167. System.out.println("Your balance is: $" + balance);
  168. System.out.println("Returning you to the main menu...");
  169. System.out.println();
  170.  
  171. ticketList.add(new ticketInfo(date, name, ticketID, charge));
  172. }
  173. } else {
  174. System.out.println("The username already exists!");
  175. }
  176. break;
  177.  
  178. // if (balance < ticketPrice)
  179. // {
  180. // System.out.println("Not enough funds to purchase a ticket you need $8");
  181. // }
  182. // else
  183. // {
  184. // balance = balance - ticketPrice;
  185. // System.out.println("Purchasing...");
  186. // System.out.println("Your current funds is " + balance);
  187. // }
  188. // break;
  189.  
  190. case 'B':
  191.  
  192. System.out.println("Still in Progress!");
  193. break;
  194.  
  195. case 'C':
  196.  
  197. System.out.println("Display Ticket Information: ");
  198. for (int i = 0; i < ticketList.size(); i++) {
  199. ticketList.get(i).printTicketInfo();
  200. }
  201.  
  202. break;
  203.  
  204. case 'D':
  205.  
  206. System.out.println("Enter name: ");
  207. name = sc.nextLine();
  208. ticketInfo validateUsername = findTicketByName(ticketList, name);
  209.  
  210. if (validateUsername == null) {
  211. System.out.println("The username does not exist!");
  212. } else {
  213. System.out.println("How much would you like to reload on your card?");
  214. reload = sc2.nextInt();
  215. oldcharge = validateUsername.getCharge();
  216. // get the reload amount the user wish's to enter
  217. while (!(reload > 0 && reload <= 100 && charge < 100 && reload % 5 == 0)) {
  218. System.out.println(
  219. "ERROR: Ticket reloads are capped at $100, and must be divisible by 5. Please try again.");
  220. // offer option to exit loop
  221. System.out.println("if you wish to cancel reload please enter 0");
  222. reload = sc2.nextInt();
  223. if (reload == 0) {
  224. break;
  225. }
  226. }
  227. if (balance >= reload) {
  228. // update the charge on the card
  229. charge = oldcharge + reload;
  230. // deduct the charge on the card
  231. balance = balance - reload;
  232. // let user know the card is reloaded
  233. System.out.println("Reloaded!");
  234. // advise the customer of the new figure
  235. System.out.println(
  236. "You have been charged: $" + charge + "and your current balance is: $" + balance);
  237. validateUsername.setCharge(charge);
  238. System.out.println("Charge balance: " + charge);
  239. //ticketList.get(charge).setCharge(charge);
  240. System.out.println("Returning you to the main menu...");
  241. System.out.println();
  242. }
  243.  
  244. // if user cannot afford the recharge
  245. else {
  246. // advise user insufficent funds
  247. System.out.println("Sorry you have insufficent funds for the recharge...");
  248. System.out.println("Charge balance: " + charge);
  249. System.out.println("Returning you to the main menu...");
  250. System.out.println();
  251. }
  252. }
  253.  
  254. break;
  255.  
  256. case 'E':
  257. // TRAVEL PASS
  258. System.out.println("Enter name: ");
  259. name = sc.nextLine();
  260. ticketInfo validateUsername1 = findTicketByName(ticketList, name);
  261.  
  262. if (validateUsername1 == null) {
  263. System.out.println("The username does not exist!");
  264. } else {
  265. System.out.println("Are you buying a (1) all day or (2) 2 hour ticket? (Please enter 1 or 2)");
  266. travelPass = sc2.nextInt();
  267. System.out.println("Are you travelling 1 or 2 zones today? (Please enter 1 or 2)");
  268. zones = sc2.nextInt();
  269.  
  270. if (travelPass == 1) // if it's an all day ticket
  271. {
  272. // First if statement
  273. if (zones == 1) {
  274. // check if there is sufficent charge for an all day Zone 1 ticket
  275. if (charge >= 7) {
  276. // if succesful deduct the charge off the card
  277. charge = charge - 7;
  278. // spend is code for the purpose of the message at end telling user that funds
  279. // was deducted
  280. spend = 7;
  281. // advise cust of their purchase
  282. System.out.println(travelPass + " ticket purchased, " + zones
  283. + " zones travelled, $" + spend + " charged");
  284.  
  285. }
  286. // if not enough funds warn user of this and advise to topup PepPep
  287. else {
  288. System.out.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.  
  295. // if Zone 1 + 2 all day
  296. if (zones == 2) {
  297. // check if suffucient funds
  298. if (charge >= 12) {
  299. charge = charge - 12; // customer charged $12 for two zones on all day ticket
  300. // spend is code for the purpose of the message at end telling user that funds
  301. // was deducted
  302. spend = 12;
  303. // advise cust of their purchase
  304. System.out.println(travelPass + " ticket purchased, " + zones + " zones travelled, $"
  305. + spend + " charged");
  306.  
  307. }
  308. // if not enough funds warn user of this and advise to topup PepPep
  309. else {
  310. System.out.println("Sorry you do not have enough credit to purchase this ticket");
  311. System.out.println("Please top up your PepPepTicket before trying again");
  312. }
  313. }
  314.  
  315. // if for a 2 hourly pass
  316. if (travelPass == 2) {
  317. // if it's a 2 hour day ticket
  318. if (zones == 1) {
  319. // check user has sufficent charge to pay for ticket
  320. if (charge >= 3.5) {
  321. // customer charged $3.50 for one zone on two hour ticket
  322. charge = charge - 3.5;
  323. // spend is code for the purpose of the message at end telling user that funds
  324. // was deducted
  325. spend = 3.5;
  326. // advise cust of their purchase
  327. System.out.println(travelPass + " ticket purchased, " + zones
  328. + " zones travelled, $" + spend + " charged");
  329. } else {
  330. System.out.println("Sorry you do not have enough credit to purchase this ticket");
  331. System.out.println("Please top up your PepPepTicket before trying again");
  332. }
  333. }
  334. if (zones == 2) {
  335. // check if user has enough funds for charge
  336. if (charge >= 6) {
  337. // customer charged $6 for one zone on two hour ticket
  338. charge = charge - 6;
  339. // spend is code for the purpose of the message at end telling user that funds
  340. // was deducted
  341. spend = 6;
  342. // advise cust of their purchase
  343. System.out.println(travelPass + " ticket purchased, " + zones
  344. + " zones travelled, $" + spend + " charged");
  345.  
  346. } else {
  347. System.out.println("Sorry you do not have enough credit to purchase this ticket");
  348. System.out.println("Please top up your PepPepTicket before trying again");
  349. }
  350. }
  351. }
  352.  
  353. System.out.println("Returning you to the main menu...");
  354. System.out.println();
  355. }
  356.  
  357. break;
  358.  
  359. case 'X':
  360.  
  361. System.out.println("Thanks for using Cinco Ticketing System!");
  362. break;
  363.  
  364. default:
  365.  
  366. // default case - handles invalid selections
  367. System.out.println("Error - invalid selection!");
  368. }
  369. }
  370. } while (selection != 'X');
  371. }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement