Advertisement
Bene2468

Untitled

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