Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pepPepPackage;
- import java.util.Scanner;
- import java.util.UUID;
- import java.util.Date;
- import java.util.ArrayList;
- import java.util.List;
- class ticketInfo {
- private String name;
- private String ticketID;
- private double charge;
- private Date date;
- // constructor with customer detail parameters
- public ticketInfo(Date date, String name, String ticketID, double charge) {
- this.date = date;
- this.name = name;
- this.ticketID = ticketID;
- this.charge = charge;
- }
- // method to ensure string input is display - not object listing
- @Override
- public String toString() {
- return ("Username: " + this.getName() + " TicketID: " + this.getTicketID() + "Charge: " + this.getCharge() + "Date: " + this.getDate());
- }
- // setters
- public void setCharge(double charge) {
- this.charge = charge;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setTicketID(String ticketID) {
- this.ticketID = ticketID;
- }
- public void setTicketID(Date date) {
- this.date = date;
- }
- // getters
- public double getCharge() {
- return charge;
- }
- public String getName() {
- return name;
- }
- public String getTicketID() {
- return ticketID;
- }
- public Date getDate() {
- return date;
- }
- public void printTicketInfo() {
- System.out.println("Date: " + date + ", Customer Name: " + name + ", TicketID: " + ticketID + ", Charge: " + charge);
- }
- }
- public class PepPepClass {
- // method to check if userName matches registered tickets in arrayList
- private static ticketInfo findTicketByName(ArrayList<ticketInfo> ticketList, String name) {
- for (int i = 0; i < ticketList.size(); i++) {
- if (ticketList.get(i).getName().equalsIgnoreCase(name)) {
- 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) {
- // declaring variables
- // balance is the customers bank balance
- double balance = 200;
- // setting the value for ticketPrice;
- int ticketPrice = 8;
- // setting customer name
- String name = "";
- // setting tickets ID
- String ticketID = UUID.randomUUID().toString();
- // variable for receiptNo
- int receiptNo;
- // travelPass is a variable telling us if its daily or 2 hourly
- int travelPass;
- // zones is a variable for either zone 1 or zone 1+2
- int zones;
- // charge is balance of PepPepticket
- double charge = 0;
- // reload is the value the customer enters when trying to recharge their
- // PepPepticket
- int reload;
- // oldcharge is to help create a variable to replace the charge with the new
- // charge figure after toping up
- double oldcharge;
- // spend is for the purpose of how much charge customers try to use when buying
- // a Zone 1 or Zone 1 + 2 ticket
- double spend = 0;
- // date variable
- Date date = new Date();
- String input;
- String fundsinput;
- char selection = '\0';
- ArrayList<ticketInfo> ticketList = new ArrayList<ticketInfo>();
- do {
- // display menu options
- System.out.println();
- System.out.println("****** Cinco Ticketing System ******");
- System.out.println();
- System.out.println("A - Purchase Ticket");
- System.out.println("B - Recent Transactions");
- System.out.println("C - Display account details and current balance");
- System.out.println("D - Reload Ticket");
- System.out.println("E - Buy Ticket Pass");
- System.out.println("X - Quit");
- System.out.println();
- // prompt the user to enter an option
- System.out.println("Enter your selection: ");
- input = sc.nextLine();
- System.out.println();
- // check to see if the user failed to enter exactly one character
- // for their menu selection
- 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
- ticketInfo validateUsername3 = findTicketByName(ticketList, name);
- System.out.println("Enter your name: ");
- name = sc.nextLine();
- if(validateUsername3 == null) {
- if (balance < ticketPrice) {
- System.out.println("Not enough funds to purchase a ticket you need $8");
- } else {
- 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...");
- System.out.println();
- ticketList.add(new ticketInfo(date, name, ticketID, charge));
- }
- } else {
- System.out.println("The username already exists!");
- }
- break;
- // if (balance < ticketPrice)
- // {
- // System.out.println("Not enough funds to purchase a ticket you need $8");
- // }
- // else
- // {
- // balance = balance - ticketPrice;
- // System.out.println("Purchasing...");
- // System.out.println("Your current funds is " + balance);
- // }
- // break;
- case 'B':
- System.out.println("Still in Progress!");
- break;
- case 'C':
- System.out.println("Display Ticket Information: ");
- for (int i = 0; i < ticketList.size(); i++) {
- ticketList.get(i).printTicketInfo();
- }
- break;
- case 'D':
- System.out.println("Enter name: ");
- name = sc.nextLine();
- ticketInfo validateUsername = findTicketByName(ticketList, name);
- if (validateUsername == null) {
- System.out.println("The username does not exist!");
- } else {
- System.out.println("How much would you like to reload on your card?");
- reload = sc2.nextInt();
- oldcharge = validateUsername.getCharge();
- // 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);
- validateUsername.setCharge(charge);
- System.out.println("Charge balance: " + charge);
- //ticketList.get(charge).setCharge(charge);
- System.out.println("Returning you to the main menu...");
- System.out.println();
- }
- // if user cannot afford the recharge
- else {
- // advise user insufficent funds
- System.out.println("Sorry you have insufficent funds for the recharge...");
- System.out.println("Charge balance: " + charge);
- System.out.println("Returning you to the main menu...");
- System.out.println();
- }
- }
- break;
- case 'E':
- // TRAVEL PASS
- System.out.println("Enter name: ");
- name = sc.nextLine();
- ticketInfo validateUsername1 = findTicketByName(ticketList, name);
- if (validateUsername1 == null) {
- System.out.println("The username does not exist!");
- } else {
- 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;
- case 'X':
- System.out.println("Thanks for using Cinco Ticketing System!");
- break;
- default:
- // default case - handles invalid selections
- System.out.println("Error - invalid selection!");
- }
- }
- } while (selection != 'X');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement