Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package View;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- import Model.Bike;
- import Model.CreditCard;
- import Model.Customer;
- import Model.ElectricBike;
- import Model.Manager;
- import Model.ManualBike;
- import data.ReadAndWrite;
- import View.ManagerView;
- public class CustomerView {
- private ArrayList<Bike> bikeList;
- public CustomerView() {
- bikeList = ReadAndWrite.getAllBikes(); // collect bike data from textfile
- }
- /** create object customer and ask for inputs */
- public static Customer getCustomerDetails() {
- System.out.println("\n###### CREATE NEW ACCOUNT ######");
- Scanner input = new Scanner(System.in);
- String details;
- Customer customer = new Customer();
- System.out.print("First name:\t"); // first name
- details = input.nextLine();
- details = onlyLetters(details);
- customer.setFirstName(details);
- System.out.print("Last name:\t"); // last name
- details = input.nextLine();
- details = onlyLetters(details);
- customer.setLastName(details);
- System.out.print("E-mail:\t\t"); // mail
- details = input.nextLine();
- details = mailOk(details);
- customer.setMail(details);
- System.out.print("Street:\t\t"); //street
- details = input.nextLine();
- customer.setStreet(details);
- System.out.print("Zip code:\t"); //zip
- details = input.nextLine();
- details = zipOk(details);
- customer.setZip(details);
- System.out.print("Date of birth:\t"); //dob
- details = input.nextLine();
- details = dobOk(details);
- customer.setDob(details);
- System.out.print("Phone no.:\t"); // phone
- details = input.nextLine();
- details = phoneOk(details);
- customer.setPhone(details);
- System.out.print("CPR no.:\t"); // cpr
- details = input.nextLine();
- details = cprOk(details);
- customer.setCpr(details);
- /**System.out.print("Credit Card no.:\t"); // ccNumber
- details = input.nextLine();
- details = ccOk(details);
- customer.setCcNumber(details);
- System.out.print("Expiry Date:\t"); // expiryDate
- details = input.nextLine();
- System.out.print("ccv:\t"); // ccv
- details = input.nextLine(); */
- System.out.println("\n***************************************************");
- System.out.println("*\t\t Hello " + customer.getFirstName() + "!\t\t *");
- System.out.println("* Thanks for using JAVABIKES! *");
- System.out.println("* *");
- System.out.println("* Remember your user name and password! *");
- System.out.println("* ==> User:\t" + customer.getUserName() + "\t\t\t\t *");
- System.out.println("* ==> Password:\t" + customer.getPassword() + "\t\t\t\t *");
- System.out.println("* *");
- System.out.println("* You'll get 20% off your first ride! *");
- System.out.println("* ENJOY *");
- System.out.println("***************************************************\n");
- return customer;
- }
- /** validation methods */
- public static String onlyLetters(String n) { // first and last name
- Scanner input = new Scanner(System.in);
- while(!(n.matches("^[a-zA-Z]{3,}$"))) { // min 3 letters, otherwise username and password cannot be generated
- System.out.print("\nEnter letters only. Please re-enter:\t");
- n = input.nextLine();
- }
- return n;
- }
- public static String mailOk(String mail) { // email
- Scanner input = new Scanner(System.in);
- while(!(mail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))) {
- System.out.println("\nEnter a valid e-mail address.\t");
- System.out.print("E-mail:\t\t");
- mail = input.nextLine();
- }
- return mail;
- }
- public static String zipOk(String zip) { // zip
- Scanner input = new Scanner(System.in);
- while(!(zip.matches("^(1[2-9][0-9][0-9])|(2[0-4][0-9][0-9]|(2500))"))) {
- System.out.println("\nEnter Copenhagen zip code only.");
- System.out.print("Zip code:\t");
- zip = input.nextLine();
- }
- return zip;
- }
- public static String dobOk(String dob) { // date of birth
- Scanner input = new Scanner(System.in);
- while (!(dob.matches("^[0-3]?[0-9].[0-3]?[0-9].(?:[1-2]?[9,2])(?:[0-9]{2})$"))) {
- System.out.println("\nUse the format DD/MM/YYYY and a valid year only.");
- System.out.print("Date of birth:\t");
- dob = input.nextLine();
- }
- return dob;
- }
- public static String phoneOk(String phone) { // phone
- Scanner input = new Scanner(System.in);
- while (!(phone.matches("^[\\d]{8}$"))) { // start and end with 8 digit pattern
- System.out.println("\nEnter exactly 8 digits only.");
- System.out.print("Phone no.:\t");
- phone = input.nextLine();
- }
- return phone;
- }
- public static String cprOk(String cpr) { // cpr
- Scanner input = new Scanner(System.in);
- while(!(cpr.matches("^[\\d]{6}-[\\d]{4}$"))) { // start with 6 digit pattern, dash, end with 4 digit pattern
- System.out.println("\nEnter digits in the format xxxxxx-xxxx only.");
- System.out.print("CPR no.:\t");
- cpr = input.nextLine();
- }
- return cpr;
- }
- /** validations for location and bike type */
- public static String locationOk() { // location
- Scanner input = new Scanner(System.in);
- System.out.print("LOCATION:\t");
- String location = input.nextLine().toUpperCase();
- while(!(location.toUpperCase().matches("VESTERBRO|NORREPORT|AMAGERBRO"))) {
- System.out.println("\nPlease enter either Vesterbro, Norreport or Amagerbro.");
- System.out.print("LOCATION:\t");
- location = input.nextLine().toUpperCase();
- }
- return location;
- }
- public static String bikeOk() { // bike type
- Scanner input = new Scanner(System.in);
- System.out.print("TYPE:\t\t");
- String type = input.nextLine().toUpperCase();
- while (!(type.toUpperCase().matches("ELECTRIC|MANUAL"))) { // validation of type - only electric and manual
- System.out.println("\nPlease enter either Manual or Electric.");
- System.out.print("TYPE:\t\t");
- type = input.nextLine().toUpperCase();
- }
- return type;
- }
- /**validate Credit Card */
- public static String ccOk(String ccNumber) { // credit card number
- Scanner input = new Scanner(System.in);
- do {
- CreditCard.checkCC(ccNumber);
- System.out.print("Please enter a valid credit card");
- System.out.print("Credit Card no.:\t");
- ccNumber = input.nextLine();
- CreditCard.checkCC(ccNumber);
- } while (!(CreditCard.checkCC(ccNumber)));
- return ccNumber;
- }
- // BOOK A BIKE
- public void printAvailableBikes() { // overview of available bikes as table
- System.out.println("AVAILABLE BIKES");
- System.out.println("**********************************");
- System.out.println("* CITY | ELECTRIC | MANUAL *");
- System.out.println("* ========== | ======== | ====== *");
- System.out.println("* VESTERBRO |\t " + bikeCount("VESTERBRO", "ELECTRIC") + " | " + bikeCount("VESTERBRO", "MANUAL") + " *");
- System.out.println("* NORREPORT |\t " + bikeCount("NORREPORT", "ELECTRIC") + " | " + bikeCount("NORREPORT", "MANUAL") + " *");
- System.out.println("* AMAGERBRO |\t " + bikeCount("AMAGERBRO", "ELECTRIC") + " | " + bikeCount("AMAGERBRO", "MANUAL") + " *");
- System.out.println("* ---------- | -------- | ------ *");
- System.out.println("* PRICE | 20,00 | 10,00 *");
- System.out.println("**********************************");
- }
- public int bikeCount(String city, String type) { // count available bikes
- int count = 0;
- for (int i = 0; i < bikeList.size(); i++) {
- if (type == "ELECTRIC") { // count bikes for TABLE depending on type + location
- if (bikeList.get(i).getType().toUpperCase().matches(type)
- && ((ElectricBike)bikeList.get(i)).getLocation().toUpperCase().matches(city)
- && ((ElectricBike)bikeList.get(i)).isAvailable() == true)
- count++;
- }
- else {
- if (bikeList.get(i).getType().toUpperCase().matches(type)
- && ((ManualBike)bikeList.get(i)).getLocation().toUpperCase().matches(city)
- && ((ManualBike)bikeList.get(i)).isAvailable() == true)
- count++;
- }
- }
- return count;
- }
- public Bike getRentBike() { // return bike object matching type + location
- ElectricBike ebike = new ElectricBike();
- ManualBike mbike = new ManualBike();
- boolean match = false; // stop for-loop at first match
- System.out.println("\nWhere would you like to rent?");
- String location = locationOk(); // get location input
- System.out.println("\nWhat type of bike would you like to rent?");
- String type = bikeOk(); // get type input
- switch (type) { // match location and type input depending on type
- case "ELECTRIC":
- for (int i = 0; i < bikeList.size() && !match; i++) { // search IDs until type + location match
- if (bikeList.get(i).getType().matches(type)
- && ((ElectricBike)bikeList.get(i)).getLocation().matches(location)
- && ((ElectricBike)bikeList.get(i)).isAvailable() == true) {
- match = true;
- ebike = (ElectricBike)bikeList.get(i);
- }
- }
- return ebike;
- default:
- for (int i = 0; i < bikeList.size() && !match; i++) {
- if (bikeList.get(i).getType().matches(type)
- && ((ManualBike)bikeList.get(i)).getLocation().matches(location)
- && ((ManualBike)bikeList.get(i)).isAvailable() == true) {
- match = true;
- mbike = (ManualBike)bikeList.get(i);
- }
- }
- return mbike;
- } // switch
- }
- public int getBikeConfirmation() { // ask customer for confirmation of preferences or discard bike selection
- ManagerView manager = new ManagerView();
- int choice = 0;
- String location;
- int index;
- Bike bike = getRentBike(); // return bike with id, type and location
- try {
- index = manager.getObjectIndex("bike", bike.getId());
- if (bike.getType().equalsIgnoreCase("ELECTRIC"))
- location = ((ElectricBike)bikeList.get(index)).getLocation(); // get location for print statement
- else
- location = ((ManualBike)bikeList.get(index)).getLocation();
- System.out.println("\nWould you like to rent " + bike.getType() + " BIKE in " + location + "?");
- switch(UtilityView.menuChoice("customerconfirm")) {
- case 1: // 1 = confirm choice
- if (bike.getType().equalsIgnoreCase("ELECTRIC"))
- ((ElectricBike)bikeList.get(index)).setAvailable(false);
- else
- ((ManualBike)bikeList.get(index)).setAvailable(false);
- printConfirmation(bike, location);
- UtilityView.closeApp(); break;
- case 2: // 2 = change preferences
- System.out.println("");
- choice = 2; break;
- case 3: // 3 = log out of system
- System.out.println("\nLog out without any further changes.");
- choice = 4; break;
- default: System.out.println("Invalid choice."); break;
- }
- UtilityView.updateDatabaseBike(bikeList);
- }
- catch (NullPointerException e) { // when bike type + location combination not found
- System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println("Selected type not available at this location.");
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- choice = 2;
- }
- return choice;
- }
- public void printConfirmation(Bike bike, String location) {
- System.out.println("\n*************************************************************");
- System.out.println("* RECEIPT *");
- System.out.println("* *");
- System.out.println("* Bike ID:\t\t" + bike.getId() + "\t\t\t\t *");
- System.out.println("* Rental location:\t" + location + "\t\t\t *");
- System.out.println("* Rental type:\t\t" + bike.getType() + "\t\t\t *");
- System.out.println("* *");
- System.out.println("* *");
- System.out.println("* Your trip costs " + bike.getPrice() + "€ for the next 24h.\t\t *");
- System.out.println("* The bike can be returned at any of our locations. *");
- System.out.println("* *");
- System.out.println("* Thank you for renting with *");
- System.out.println("* JAVABIKES *");
- System.out.println("* *");
- System.out.println("*************************************************************");
- }
- /** return the bike by ID */
- public void returnBike() {
- CustomerView customer = new CustomerView();
- ManagerView manager = new ManagerView();
- Scanner input = new Scanner (System.in);
- int id;
- int index;
- System.out.println("\nPlease enter the ID of the Bike you want to return:");
- System.out.print("ID:\t\t");
- id = input.nextInt();
- index = manager.getObjectIndex("bike", id); // return arraylist index of to returned bike
- System.out.println("Please enter the returning location:");
- String location = locationOk(); // location input + validation
- if (bikeList.get(index).getType().matches("ELECTRIC")) { // setters depending on bike type
- ((ElectricBike)bikeList.get(index)).setLocation(location);
- System.out.println("Please enter the battery status:");
- System.out.print("BATTERY (in %):\t");
- int battery = input.nextInt();
- ((ElectricBike)bikeList.get(index)).setBattery(battery);
- ((ElectricBike)bikeList.get(index)).setAvailable(true);
- }
- else {
- ((ManualBike)bikeList.get(index)).setLocation(location);
- ((ManualBike)bikeList.get(index)).setAvailable(true);
- }
- UtilityView.updateDatabaseBike(bikeList);
- customer.collectFeedback();
- }
- /** collect Feedback f */
- public void collectFeedback() {
- Scanner input = new Scanner (System.in);
- String comment = "";
- System.out.print("\nPlease rate our service: ");
- System.out.println("[1]* [2]** [3]*** [4]**** [5]*****");
- System.out.print("RATING:\t");
- String rating = input.nextLine(); // get rating input
- System.out.print("COMMENT:\t");
- comment = input.nextLine(); // get comment
- ReadAndWrite.writeDetails("feedback.txt", (rating + ";" + comment + ";"));
- System.out.println("\nThanks for your feedback!");
- }
- } //class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement