Advertisement
Guest User

Untitled

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