Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package freeWork;
- import java.util.*;
- class ticketInfo {
- private String userName = "";
- private String ticketID = "";
- @Override
- public String toString() {
- return ("Username: " + this.getUserName() + " TicketID: " + this.getTicketID());
- }
- public ticketInfo(String userName, String ticketID) {
- this.userName = userName;
- this.ticketID = ticketID;
- }
- /*
- * public void setUserName(String userName) { this.userName = userName; }
- *
- * public void setTicketID(String ticketID) { this.ticketID = ticketID; }
- */
- public String getUserName() {
- return userName;
- }
- public String getTicketID() {
- return ticketID;
- }
- public void printTicketInfo() {
- System.out.println("Username: " + userName + " TicketID: " + ticketID);
- }
- }
- public class testCode {
- // checks to see if userName matches registered tickets in arraylist
- private static ticketInfo findTicketByName(ArrayList<ticketInfo> ticketList, String userName) {
- for (int i = 0; i < ticketList.size(); i++) {
- if (ticketList.get(i).getUserName().equals(userName)) {
- return ticketList.get(i);
- }
- }
- return null;
- }
- private static final Scanner sc = new Scanner(System.in);
- private static final Scanner sc2 = new Scanner(System.in);
- public static void main(String[] args) {
- // variables
- double balance = 800;
- int ticketPrice = 8;
- String ticketID = UUID.randomUUID().toString();
- String userName = "";
- int recieptNo;
- int travelPass;
- int zones;
- double charge = 0;
- double oldcharge = 0;
- int reload;
- double spend = 0;
- String input;
- String fundsinput;
- char selection = '\0';
- // List<String> tickets = new ArrayList<String>();
- int ticketCount = 0;
- ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
- do {
- // display menu options
- System.out.println("*** Cinco Ticketing System ***");
- System.out.println();
- System.out.println("A - Purchase Ticket");
- System.out.println("B - Display Ticket Info");
- System.out.println("C - Reload PepPep Ticket");
- System.out.println("D - Purchase Travel Pass");
- System.out.println("X - Quit");
- System.out.println("Enter your selection: ");
- input = sc.nextLine();
- if (input.length() != 1) {
- System.out.println("Error - Selection must be a single character!");
- } else {
- selection = Character.toUpperCase(input.charAt(0));
- // process the user's selection
- switch (selection) {
- case 'A':
- // purchase ticket option
- /*
- * if (balance < ticketPrice) {
- * System.out.println("Not enough funds to purchase ticket! You need $8."); }
- * else { System.out.println("Enter a username"); name = sc.nextLine(); balance
- * = balance - ticketPrice; System.out.println("Purchasing...");
- * //tickets.add(ticketID); //tickets.add(userName);
- * System.out.println("Ticket purchased!");
- * System.out.println("Your balance is: $" + balance);
- * System.out.println("Returning you to the main menu..."); }
- */
- System.out.println("Enter Username: ");
- userName = sc.nextLine();
- balance = balance - ticketPrice;
- System.out.println("Purchasing...");
- System.out.println("Ticket purchased!");
- System.out.println("Your balance is: $" + balance);
- System.out.println("Returning you to the main menu...");
- // ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
- ticketList.add(new ticketInfo(userName, ticketID));
- break;
- case 'B':
- System.out.println("Display Ticket Information: ");
- for (int i = 0; i < ticketList.size(); i++) {
- // System.out.println(ticketList.get(i));
- ticketList.get(i).printTicketInfo();
- }
- // System.out.println(ticketList);
- break;
- case 'C':
- System.out.println("Reload Ticket Selected!");
- System.out.println("Enter username: ");
- userName = sc.nextLine();
- if (userName == ticketInfo.findTicketByName)
- System.out.println("How much would you like to reload on your card?");
- reload = sc2.nextInt();
- oldcharge = charge;
- // get the reload amount the user wish's to enter
- while (!(reload > 0 && reload <= 100 && charge < 100 && reload % 5 == 0)) {
- System.out.println(
- "ERROR: Ticket reloads are capped at $100, and must be divisible by 5. Please try again.");
- // offer option to exit loop
- System.out.println("if you wish to cancel reload please enter 0");
- reload = sc2.nextInt();
- if (reload == 0) {
- break;
- }
- }
- if (balance >= reload) {
- // update the charge on the card
- charge = oldcharge + reload;
- // deduct the charge on the card
- balance = balance - reload;
- // let user know the card is reloaded
- System.out.println("Reloaded!");
- // advise the customer of the new figure
- System.out.println(
- "You have been charged: $" + charge + "and your current balance is: $" + balance);
- System.out.println("Returning you to the main menu...");
- System.out.println();
- }
- // if user cannot afford the recharge
- else {
- // advise user insufficient funds
- System.out.println("Sorry you have insufficent funds for the recharge...");
- System.out.println("Returning you to the main menu...");
- }
- break;
- case 'D':
- System.out.println("Enter username: ");
- userName = sc.nextLine();
- for (int i = 0; i < ticketList.size(); i++) {
- if (!ticketList.get(i).getUserName().equals(userName)) {
- System.out.println("Error! Name not in database.");
- }
- if (ticketList.get(i).getUserName().equals(userName)) {
- System.out.println(
- "Are you buying a (1) all day or (2) 2 hour ticket? (Please enter 1 or 2)");
- travelPass = sc2.nextInt();
- System.out.println("Are you travelling 1 or 2 zones today? (Please enter 1 or 2)");
- zones = sc2.nextInt();
- if (travelPass == 1) // if it's an all day ticket
- {
- // First if statement
- if (zones == 1) {
- // check if there is sufficent charge for an all day Zone 1 ticket
- if (charge >= 7) {
- // if succesful deduct the charge off the card
- charge = charge - 7;
- // spend is code for the purpose of the message at end telling user that funds
- // was deducted
- spend = 7;
- // advise cust of their purchase
- System.out.println(travelPass + " ticket purchased, " + zones
- + " zones travelled, $" + spend + " charged");
- }
- // if not enough funds warn user of this and advise to topup PepPep
- else {
- System.out
- .println("Sorry you do not have enough credit to purchase this ticket");
- System.out.println("Please top up your PepPepTicket before trying again");
- }
- }
- }
- // if Zone 1 + 2 all day
- if (zones == 2) {
- // check if suffucient funds
- if (charge >= 12) {
- charge = charge - 12; // customer charged $12 for two zones on all day ticket
- // spend is code for the purpose of the message at end telling user that funds
- // was deducted
- spend = 12;
- // advise cust of their purchase
- System.out.println(travelPass + " ticket purchased, " + zones
- + " zones travelled, $" + spend + " charged");
- }
- // if not enough funds warn user of this and advise to topup PepPep
- else {
- System.out.println("Sorry you do not have enough credit to purchase this ticket");
- System.out.println("Please top up your PepPepTicket before trying again");
- }
- }
- // if for a 2 hourly pass
- if (travelPass == 2) {
- // if it's a 2 hour day ticket
- if (zones == 1) {
- // check user has sufficent charge to pay for ticket
- if (charge >= 3.5) {
- // customer charged $3.50 for one zone on two hour ticket
- charge = charge - 3.5;
- // spend is code for the purpose of the message at end telling user that funds
- // was deducted
- spend = 3.5;
- // advise cust of their purchase
- System.out.println(travelPass + " ticket purchased, " + zones
- + " zones travelled, $" + spend + " charged");
- } else {
- System.out
- .println("Sorry you do not have enough credit to purchase this ticket");
- System.out.println("Please top up your PepPepTicket before trying again");
- }
- }
- if (zones == 2) {
- // check if user has enough funds for charge
- if (charge >= 6) {
- // customer charged $6 for one zone on two hour ticket
- charge = charge - 6;
- // spend is code for the purpose of the message at end telling user that funds
- // was deducted
- spend = 6;
- // advise cust of their purchase
- System.out.println(travelPass + " ticket purchased, " + zones
- + " zones travelled, $" + spend + " charged");
- } else {
- System.out
- .println("Sorry you do not have enough credit to purchase this ticket");
- System.out.println("Please top up your PepPepTicket before trying again");
- }
- }
- }
- System.out.println("Returning you to the main menu...");
- System.out.println();
- }
- }
- break;
- }
- }
- } while (selection != 'X');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement