Advertisement
Guest User

Untitled

a guest
May 7th, 2017
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.35 KB | None | 0 0
  1. package View;
  2.  
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6.  
  7. import Model.Bike;
  8. import Model.CreditCard;
  9. import Model.Customer;
  10. import Model.ElectricBike;
  11. import Model.Manager;
  12. import Model.ManualBike;
  13. import data.ReadAndWrite;
  14. import View.ManagerView;
  15.  
  16. public class CustomerView {
  17.  
  18.     private ArrayList<Bike> bikeList;
  19.  
  20.     public CustomerView() {
  21.         bikeList = ReadAndWrite.getAllBikes(); // collect bike data from textfile
  22.     }
  23.    
  24.     /** create object customer and ask for inputs */
  25.     public static Customer getCustomerDetails() {
  26.         System.out.println("\n###### CREATE NEW ACCOUNT ######");
  27.         Scanner input = new Scanner(System.in);
  28.             String details;
  29.         Customer customer = new Customer();
  30.         System.out.print("First name:\t"); // first name
  31.             details = input.nextLine();
  32.             details = onlyLetters(details);
  33.             customer.setFirstName(details);
  34.         System.out.print("Last name:\t"); // last name
  35.             details = input.nextLine();
  36.             details = onlyLetters(details);
  37.             customer.setLastName(details);
  38.         System.out.print("E-mail:\t\t"); // mail
  39.             details = input.nextLine();
  40.             details = mailOk(details);
  41.             customer.setMail(details);
  42.         System.out.print("Street:\t\t"); //street
  43.             details = input.nextLine();
  44.             customer.setStreet(details);
  45.         System.out.print("Zip code:\t"); //zip
  46.             details = input.nextLine();
  47.             details = zipOk(details);
  48.             customer.setZip(details);
  49.         System.out.print("Date of birth:\t"); //dob
  50.             details = input.nextLine();
  51.             details = dobOk(details);
  52.             customer.setDob(details);
  53.         System.out.print("Phone no.:\t"); // phone
  54.             details = input.nextLine();
  55.             details = phoneOk(details);
  56.             customer.setPhone(details);
  57.         System.out.print("CPR no.:\t"); // cpr
  58.             details = input.nextLine();
  59.             details = cprOk(details);
  60.             customer.setCpr(details);
  61.         /**System.out.print("Credit Card no.:\t"); // ccNumber
  62.             details = input.nextLine();
  63.             details = ccOk(details);
  64.             customer.setCcNumber(details);
  65.         System.out.print("Expiry Date:\t"); // expiryDate
  66.             details = input.nextLine();
  67.         System.out.print("ccv:\t"); // ccv
  68.             details = input.nextLine();  */
  69.            
  70.         System.out.println("\n***************************************************");
  71.         System.out.println("*\t\t     Hello " + customer.getFirstName() + "!\t\t  *");
  72.         System.out.println("*           Thanks for using JAVABIKES!           *");
  73.         System.out.println("*                                                 *");
  74.         System.out.println("* Remember your user name and password!           *");
  75.         System.out.println("* ==> User:\t" + customer.getUserName() + "\t\t\t\t  *");
  76.         System.out.println("* ==> Password:\t" + customer.getPassword() + "\t\t\t\t  *");
  77.         System.out.println("*                                                 *");
  78.         System.out.println("*      You'll get 20% off your first ride!        *");
  79.         System.out.println("*                      ENJOY                      *");
  80.         System.out.println("***************************************************\n");
  81.        
  82.         return customer;
  83.     }
  84.  
  85.     /** validation methods */
  86.     public static String onlyLetters(String n) { // first and last name
  87.         Scanner input = new Scanner(System.in);
  88.         while(!(n.matches("^[a-zA-Z]{3,}$"))) { // min 3 letters, otherwise username and password cannot be generated
  89.             System.out.print("\nEnter letters only. Please re-enter:\t");
  90.             n = input.nextLine();
  91.         }
  92.         return n;
  93.     }
  94.     public static String mailOk(String mail) { // email
  95.         Scanner input = new Scanner(System.in);
  96.         while(!(mail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))) {
  97.             System.out.println("\nEnter a valid e-mail address.\t");
  98.             System.out.print("E-mail:\t\t");
  99.             mail = input.nextLine();
  100.         }
  101.         return mail;
  102.     }
  103.     public static String zipOk(String zip) { // zip
  104.         Scanner input = new Scanner(System.in);
  105.         while(!(zip.matches("^(1[2-9][0-9][0-9])|(2[0-4][0-9][0-9]|(2500))"))) {
  106.             System.out.println("\nEnter Copenhagen zip code only.");
  107.             System.out.print("Zip code:\t");
  108.             zip = input.nextLine();
  109.         }
  110.         return zip;
  111.     }
  112.     public static String dobOk(String dob) { // date of birth
  113.         Scanner input = new Scanner(System.in);
  114.         while (!(dob.matches("^[0-3]?[0-9].[0-3]?[0-9].(?:[1-2]?[9,2])(?:[0-9]{2})$"))) {
  115.             System.out.println("\nUse the format DD/MM/YYYY and a valid year only.");
  116.             System.out.print("Date of birth:\t");
  117.             dob = input.nextLine();
  118.         }
  119.         return dob;
  120.     }
  121.     public static String phoneOk(String phone) { // phone
  122.         Scanner input = new Scanner(System.in);
  123.         while (!(phone.matches("^[\\d]{8}$"))) { // start and end with 8 digit pattern
  124.             System.out.println("\nEnter exactly 8 digits only.");
  125.             System.out.print("Phone no.:\t");
  126.             phone = input.nextLine();
  127.         }  
  128.         return phone;
  129.     }
  130.     public static String cprOk(String cpr) { // cpr
  131.         Scanner input = new Scanner(System.in);
  132.         while(!(cpr.matches("^[\\d]{6}-[\\d]{4}$"))) { // start with 6 digit pattern, dash, end with 4 digit pattern
  133.             System.out.println("\nEnter digits in the format xxxxxx-xxxx only.");
  134.             System.out.print("CPR no.:\t");
  135.             cpr = input.nextLine();
  136.         }
  137.         return cpr;
  138.     }
  139.    
  140.     /** validations for location and bike type */
  141.     public static String locationOk() { // location
  142.         Scanner input = new Scanner(System.in);
  143.         System.out.print("LOCATION:\t");
  144.         String location = input.nextLine().toUpperCase();
  145.         while(!(location.toUpperCase().matches("VESTERBRO|NORREPORT|AMAGERBRO"))) {
  146.             System.out.println("\nPlease enter either Vesterbro, Norreport or Amagerbro.");
  147.             System.out.print("LOCATION:\t");
  148.             location = input.nextLine().toUpperCase();
  149.         }
  150.         return location;
  151.     }
  152.     public static String bikeOk() { // bike type
  153.         Scanner input = new Scanner(System.in);
  154.         System.out.print("TYPE:\t\t");
  155.         String type = input.nextLine().toUpperCase();
  156.             while (!(type.toUpperCase().matches("ELECTRIC|MANUAL"))) { // validation of type - only electric and manual
  157.                 System.out.println("\nPlease enter either Manual or Electric.");
  158.                 System.out.print("TYPE:\t\t");
  159.                 type = input.nextLine().toUpperCase();
  160.             }
  161.         return type;
  162.     }
  163.    
  164.     /**validate Credit Card */
  165.     public static String ccOk(String ccNumber) { // credit card number
  166.         Scanner input = new Scanner(System.in);
  167.             do {
  168.                 CreditCard.checkCC(ccNumber);
  169.                 System.out.print("Please enter a valid credit card");
  170.                 System.out.print("Credit Card no.:\t");
  171.                 ccNumber = input.nextLine();
  172.                 CreditCard.checkCC(ccNumber);
  173.             } while (!(CreditCard.checkCC(ccNumber)));
  174.                 return ccNumber;
  175.             }
  176.  
  177.    
  178.     // BOOK A BIKE
  179.    
  180.     public void printAvailableBikes() { // overview of available bikes as table
  181.         System.out.println("AVAILABLE BIKES");
  182.         System.out.println("**********************************");
  183.         System.out.println("* CITY       | ELECTRIC | MANUAL *");
  184.         System.out.println("* ========== | ======== | ====== *");
  185.         System.out.println("* VESTERBRO  |\t  " + bikeCount("VESTERBRO", "ELECTRIC") + "     |   " + bikeCount("VESTERBRO", "MANUAL") + "    *");
  186.         System.out.println("* NORREPORT  |\t  " + bikeCount("NORREPORT", "ELECTRIC") + "     |   " + bikeCount("NORREPORT", "MANUAL") + "    *");
  187.         System.out.println("* AMAGERBRO  |\t  " + bikeCount("AMAGERBRO", "ELECTRIC") + "     |   " + bikeCount("AMAGERBRO", "MANUAL") + "    *");
  188.         System.out.println("* ---------- | -------- | ------ *");
  189.         System.out.println("* PRICE      |  20,00   | 10,00  *");
  190.         System.out.println("**********************************");
  191.     }
  192.        
  193.     public int bikeCount(String city, String type) { // count available bikes
  194.         int count = 0;
  195.         for (int i = 0; i < bikeList.size(); i++) {
  196.             if (type == "ELECTRIC") { // count bikes for TABLE depending on type + location
  197.                 if (bikeList.get(i).getType().toUpperCase().matches(type)
  198.                         && ((ElectricBike)bikeList.get(i)).getLocation().toUpperCase().matches(city)
  199.                         && ((ElectricBike)bikeList.get(i)).isAvailable() == true)
  200.                 count++;
  201.              }
  202.              else {
  203.                  if (bikeList.get(i).getType().toUpperCase().matches(type)
  204.                          && ((ManualBike)bikeList.get(i)).getLocation().toUpperCase().matches(city)
  205.                          && ((ManualBike)bikeList.get(i)).isAvailable() == true)
  206.                  count++;  
  207.             }
  208.         }
  209.  
  210.         return count;
  211.     }
  212.    
  213.     public Bike getRentBike() { // return bike object matching type + location
  214.         ElectricBike ebike = new ElectricBike();
  215.         ManualBike mbike = new ManualBike();
  216.         boolean match = false; // stop for-loop at first match
  217.        
  218.         System.out.println("\nWhere would you like to rent?");
  219.         String location = locationOk(); // get location input
  220.         System.out.println("\nWhat type of bike would you like to rent?");
  221.         String type = bikeOk(); // get type input
  222.  
  223.         switch (type) { // match location and type input depending on type
  224.             case "ELECTRIC":
  225.                 for (int i = 0; i < bikeList.size() && !match; i++) { // search IDs until type + location match
  226.                     if (bikeList.get(i).getType().matches(type)
  227.                             && ((ElectricBike)bikeList.get(i)).getLocation().matches(location)
  228.                             && ((ElectricBike)bikeList.get(i)).isAvailable() == true) {
  229.                         match = true;
  230.                         ebike = (ElectricBike)bikeList.get(i);
  231.                     }
  232.                 }
  233.                
  234.         return ebike;
  235.             default:
  236.                 for (int i = 0; i < bikeList.size() && !match; i++) {
  237.                     if (bikeList.get(i).getType().matches(type)
  238.                             && ((ManualBike)bikeList.get(i)).getLocation().matches(location)
  239.                             && ((ManualBike)bikeList.get(i)).isAvailable() == true) {
  240.                         match = true;
  241.                         mbike = (ManualBike)bikeList.get(i);
  242.                     }  
  243.                 }
  244.        
  245.         return mbike;
  246.         } // switch
  247.     }
  248.    
  249.     public int getBikeConfirmation() { // ask customer for confirmation of preferences or discard bike selection
  250.         ManagerView manager = new ManagerView();
  251.         int choice = 0;
  252.         String location;
  253.         int index;
  254.        
  255.         Bike bike = getRentBike(); // return bike with id, type and location
  256.        
  257.         try {
  258.             index = manager.getObjectIndex("bike", bike.getId());
  259.                 if (bike.getType().equalsIgnoreCase("ELECTRIC"))
  260.                     location = ((ElectricBike)bikeList.get(index)).getLocation(); // get location for print statement
  261.                 else
  262.                     location = ((ManualBike)bikeList.get(index)).getLocation();
  263.                
  264.             System.out.println("\nWould you like to rent " + bike.getType() + " BIKE in " + location + "?");
  265.            
  266.                 switch(UtilityView.menuChoice("customerconfirm")) {
  267.                     case 1:                                         // 1 = confirm choice
  268.                         if (bike.getType().equalsIgnoreCase("ELECTRIC"))
  269.                             ((ElectricBike)bikeList.get(index)).setAvailable(false);
  270.                         else
  271.                             ((ManualBike)bikeList.get(index)).setAvailable(false);
  272.                         printConfirmation(bike, location);
  273.                         UtilityView.closeApp(); break;
  274.                     case 2:                                         // 2 = change preferences
  275.                         System.out.println("");
  276.                         choice = 2; break;
  277.                     case 3:                                         // 3 = log out of system
  278.                         System.out.println("\nLog out without any further changes.");
  279.                         choice = 4; break;
  280.                     default: System.out.println("Invalid choice."); break;
  281.                 }
  282.            
  283.             UtilityView.updateDatabaseBike(bikeList);
  284.            
  285.         }
  286.         catch (NullPointerException e) { // when bike type + location combination not found
  287.             System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  288.             System.out.println("Selected type not available at this location.");
  289.             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  290.             choice = 2;
  291.         }
  292.        
  293.     return choice; 
  294.     }
  295.    
  296.     public void printConfirmation(Bike bike, String location) {
  297.         System.out.println("\n*************************************************************");
  298.         System.out.println("*                          RECEIPT                          *");
  299.         System.out.println("*                                                           *");
  300.         System.out.println("* Bike ID:\t\t" + bike.getId() + "\t\t\t\t    *");
  301.         System.out.println("* Rental location:\t" + location + "\t\t\t    *");
  302.         System.out.println("* Rental type:\t\t" + bike.getType() + "\t\t\t    *");
  303.         System.out.println("*                                                           *");
  304.         System.out.println("*                                                           *");
  305.         System.out.println("*    Your trip costs  " + bike.getPrice() + "€  for the next 24h.\t\t  *");
  306.         System.out.println("*    The bike can be returned at any of our locations.      *");
  307.         System.out.println("*                                                           *");
  308.         System.out.println("*                 Thank you for renting with                *");
  309.         System.out.println("*                         JAVABIKES                         *");
  310.         System.out.println("*                                                           *");
  311.         System.out.println("*************************************************************");   
  312.     }
  313.    
  314.  
  315.     /** return the bike by ID */
  316.     public void returnBike() {
  317.         CustomerView customer = new CustomerView();
  318.         ManagerView manager = new ManagerView();
  319.         Scanner input = new Scanner (System.in);
  320.         int id;
  321.         int index;
  322.        
  323.             System.out.println("\nPlease enter the ID of the Bike you want to return:");
  324.             System.out.print("ID:\t\t");
  325.             id = input.nextInt();
  326.             index = manager.getObjectIndex("bike", id); // return arraylist index of to returned bike
  327.             System.out.println("Please enter the returning location:");
  328.             String location = locationOk(); // location input + validation
  329.            
  330.                 if (bikeList.get(index).getType().matches("ELECTRIC")) { // setters depending on bike type
  331.                     ((ElectricBike)bikeList.get(index)).setLocation(location);
  332.                     System.out.println("Please enter the battery status:");
  333.                     System.out.print("BATTERY (in %):\t");
  334.                     int battery = input.nextInt();
  335.                     ((ElectricBike)bikeList.get(index)).setBattery(battery);
  336.                     ((ElectricBike)bikeList.get(index)).setAvailable(true);
  337.                 }
  338.                 else {
  339.                     ((ManualBike)bikeList.get(index)).setLocation(location);
  340.                     ((ManualBike)bikeList.get(index)).setAvailable(true);  
  341.                 }
  342.         UtilityView.updateDatabaseBike(bikeList);
  343.         customer.collectFeedback();
  344.     }
  345.        
  346.     /** collect Feedback f */
  347.     public void collectFeedback() {
  348.         Scanner input = new Scanner (System.in);
  349.         String comment = "";
  350.        
  351.         System.out.print("\nPlease rate our service: ");
  352.         System.out.println("[1]*  [2]** [3]*** [4]**** [5]*****");
  353.         System.out.print("RATING:\t");
  354.         String rating = input.nextLine(); // get rating input
  355.         System.out.print("COMMENT:\t");
  356.         comment = input.nextLine(); // get comment
  357.        
  358.         ReadAndWrite.writeDetails("feedback.txt", (rating + ";" + comment + ";"));
  359.         System.out.println("\nThanks for your feedback!");     
  360.     }
  361.    
  362. } //class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement