Advertisement
Guest User

JavaBikes

a guest
Apr 18th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.09 KB | None | 0 0
  1. -------------- MODEL --------------
  2.  
  3. package model;
  4.  
  5. import model.Product;
  6.  
  7. public class Bike extends Product {
  8.  
  9. private String colour;
  10. private int gear;
  11. private static int stockAvailable;
  12.  
  13.  
  14. public Bike(){
  15. }
  16.  
  17.  
  18. public Bike(int productID, String productName, String colour, int gear, double productPrice) {
  19. super(productID, productName, productPrice);
  20. this.colour = colour;
  21. this.gear = gear;
  22. stockAvailable++;
  23. }
  24.  
  25.  
  26. public String getColour() {
  27. return colour;
  28. }
  29.  
  30.  
  31. public void setColour(String colour) {
  32. this.colour = colour;
  33. }
  34.  
  35.  
  36. public int getGear() {
  37. return gear;
  38. }
  39.  
  40.  
  41. public void setGear(int gear) {
  42. this.gear = gear;
  43. }
  44.  
  45.  
  46. public void rentBike() {
  47. stockAvailable--;
  48. }
  49.  
  50.  
  51. public static int getStockAvailable() {
  52. return stockAvailable;
  53. }
  54.  
  55.  
  56. @Override
  57. public String toString() {
  58. return "Bike [colour=" + colour + ", gear=" + gear + ", id=" +getProductID()
  59. + ", productPrice=" + getProductPrice() + ", stock=" + getStockAvailable()+ "]";
  60. }
  61.  
  62. }
  63.  
  64. package model;
  65.  
  66. public class CreditCard {
  67.  
  68. private String cardNumber, experationDate, cvcNumber;
  69.  
  70. public CreditCard(){
  71.  
  72. }
  73.  
  74. public CreditCard(String cardNum, String exDate, String cvc){
  75. cardNumber = cardNum;
  76. experationDate = exDate;
  77. cvcNumber = cvc;
  78. }
  79.  
  80.  
  81. public String getCardNumber() {
  82. return cardNumber;
  83. }
  84.  
  85.  
  86. public void setCardNumber(String cardNumber) {
  87. this.cardNumber = cardNumber;
  88. }
  89.  
  90.  
  91. public String getExperationDate() {
  92. return experationDate;
  93. }
  94.  
  95.  
  96. public void setExperationDate(String experationDate) {
  97. this.experationDate = experationDate;
  98. }
  99.  
  100.  
  101. public String getCvcNumber() {
  102. return cvcNumber;
  103. }
  104.  
  105.  
  106. public void setCvcNumber(String cvcNumber) {
  107. this.cvcNumber = cvcNumber;
  108. }
  109.  
  110. }
  111. package model;
  112.  
  113. import data.ReadAndWrite;
  114.  
  115. public class Customer {
  116.  
  117. private String firstName, lastName, phoneNumber, email, username, password;
  118.  
  119. public Customer(){
  120.  
  121. }
  122.  
  123.  
  124. public Customer(String fName, String lName, String phoneNum, String creditCa, String eMa){
  125. firstName = fName;
  126. lastName = lName;
  127. phoneNumber = phoneNum;
  128. email = eMa;
  129.  
  130.  
  131. }
  132.  
  133.  
  134. public String getFirstName() {
  135. return firstName;
  136. }
  137.  
  138. public void setFirstName(String firstName) {
  139. this.firstName = firstName;
  140. }
  141.  
  142.  
  143. public String getLastName() {
  144. return lastName;
  145. }
  146.  
  147. public void setLastName(String lastName) {
  148. this.lastName = lastName;
  149. }
  150.  
  151.  
  152. public String getPhonenumber() {
  153. return phoneNumber;
  154. }
  155.  
  156. public void setPhoneNumber(String phoneNumber) {
  157. this.phoneNumber = phoneNumber;
  158. }
  159.  
  160.  
  161. public String getEmail() {
  162. return email;
  163. }
  164.  
  165. public void setEmail(String email) {
  166. this.email = email;
  167. }
  168.  
  169.  
  170. public String getUsername() {
  171. return username;
  172. }
  173.  
  174. public void setUsername(String username) {
  175. this.username = username;
  176. }
  177.  
  178.  
  179. public String getPassword() {
  180. return password;
  181. }
  182.  
  183.  
  184. public void setPassword(String password) {
  185. this.password = password;
  186. }
  187.  
  188.  
  189. //public void setUsername() {
  190. //this.username = firstName.substring(0, 3)+surname.substring(0, 3);
  191. // }
  192.  
  193.  
  194. //public void setPassword() {
  195. //this.password = surname.substring(0, 3) + phoneNumber.substring(2, 5);
  196. //}
  197.  
  198.  
  199. public void generateUsername() {
  200. this.username = firstName.substring(0, 3)+lastName.substring(0, 3);
  201. this.password = lastName.substring(0, 3) + phoneNumber.substring(2, 5);
  202.  
  203. }
  204.  
  205.  
  206. public String toString(){
  207. return (firstName+ " " + lastName + " " + phoneNumber +" " + " "+ email);
  208. }
  209.  
  210.  
  211. public void writetoFile(){
  212. String details = lastName+ ";" + firstName + ";" + getUsername() + ";" + getPassword() + ";" + phoneNumber +";"+ email+ ";";
  213. ReadAndWrite.WriteDetails("customer.txt", details);
  214.  
  215. }
  216.  
  217.  
  218. }
  219. package model;
  220.  
  221. import model.Product;
  222.  
  223. public class ElectricBike extends Product {
  224.  
  225. private String colour;
  226. private int watt;
  227. private static int stockAvailable;
  228.  
  229.  
  230. public ElectricBike(){
  231. }
  232.  
  233.  
  234. public ElectricBike(int productID, String productName, String colour, int watt, double productPrice) {
  235. super(productID, productName, productPrice);
  236. this.colour = colour;
  237. this.watt = watt;
  238. stockAvailable++;
  239. }
  240.  
  241.  
  242. public String getColour() {
  243. return colour;
  244. }
  245.  
  246.  
  247. public void setColour(String colour) {
  248. this.colour = colour;
  249. }
  250.  
  251.  
  252. public int getWatt() {
  253. return watt;
  254. }
  255.  
  256.  
  257. public void setWatt(int watt) {
  258. this.watt = watt;
  259. }
  260.  
  261.  
  262. public void rentBike() {
  263. stockAvailable--;
  264. }
  265.  
  266.  
  267. public static int getStockAvailable() {
  268. return stockAvailable;
  269. }
  270.  
  271.  
  272. @Override
  273. public String toString() {
  274. return "Bike [colour=" + colour + ", watt=" + watt + ", id=" +getProductID()
  275. + ", productPrice=" + getProductPrice() + ", stock=" + getStockAvailable()+ "]";
  276. }
  277.  
  278. }
  279. package model;
  280.  
  281. public class Product {
  282.  
  283. private String productName;
  284. private static int id;
  285. private int productID;
  286. private double productPrice;
  287.  
  288.  
  289. public Product(int productID, String productName, double productPrice){
  290. productID = id;
  291. this.productName = productName;
  292. this.productPrice = productPrice;
  293. id++;
  294. }
  295.  
  296.  
  297. public int getProductID() {
  298. return productID;
  299. }
  300.  
  301.  
  302. public void setProductID(int productID) {
  303. this.productID = productID;
  304. }
  305.  
  306.  
  307. public Product(){
  308. id++;
  309. }
  310.  
  311.  
  312. public String getProductName() {
  313. return productName;
  314. }
  315.  
  316.  
  317. public void setProductName(String productName) {
  318. this.productName = productName;
  319. }
  320.  
  321.  
  322. public int getId() {
  323. return id;
  324. }
  325.  
  326.  
  327. public void setId(int id) {
  328. this.id = id;
  329. }
  330.  
  331.  
  332. public double getProductPrice() {
  333. return productPrice;
  334. }
  335.  
  336. public void setProductPrice(double productPrice) {
  337. this.productPrice = productPrice;
  338. }
  339.  
  340. }
  341. package model;
  342.  
  343. import java.util.ArrayList;
  344.  
  345. import data.ReadAndWrite;
  346.  
  347. public class ProductDatabase {
  348.  
  349. ArrayList<Product> database;
  350.  
  351. public ProductDatabase(){
  352. database = ReadAndWrite.getAllProductDetails();
  353. }
  354.  
  355. }
  356.  
  357. ------------ VIEW -------------
  358.  
  359. package view;
  360.  
  361. import java.util.Scanner;
  362.  
  363. import model.CreditCard;
  364.  
  365. import model.Customer;
  366.  
  367. public class CustomerView {
  368.  
  369. private Scanner input;
  370.  
  371.  
  372. public CustomerView(){
  373.  
  374. }
  375.  
  376.  
  377. public Customer getCustomerDetails(){
  378. input = new Scanner(System.in);
  379. String details;
  380. Customer RC = new Customer();
  381. CreditCard CC = new CreditCard();
  382. LoginView LV = new LoginView();
  383.  
  384.  
  385. boolean correct = false;
  386. while (!correct) {
  387. System.out.print("Enter your first name: ");
  388. details = input.nextLine();
  389. RC.setFirstName(details);
  390. correct = true;
  391. }
  392.  
  393.  
  394. correct = false;
  395. while (!correct) {
  396. System.out.print("Enter your last name: ");
  397. details = input.nextLine();
  398. RC.setLastName(details);
  399. correct = true;
  400. }
  401.  
  402.  
  403. correct = false;
  404. while (!correct) {
  405. System.out.print("Enter your Phone Number (8 digits): ");
  406. details = input.nextLine();
  407. if (details.matches("[0-9]+") && details.length()==8) {
  408. RC.setPhoneNumber(details);
  409. correct = true;
  410. }
  411. else
  412. System.out.println("You have entered an invalid number");
  413. }
  414.  
  415.  
  416. correct = false;
  417. while (!correct) {
  418. System.out.print("Enter your card number (16 digits): ");
  419. details = input.nextLine();
  420. if (details.matches("^(\\d{4}?\\d{4}?\\d{4}?\\d{4})")) {
  421. CC.setCardNumber(details);
  422. correct = true;
  423. }
  424. else
  425. System.out.println("You have entered an invalid credit card number");
  426. }
  427.  
  428.  
  429. correct = false;
  430. while (!correct) {
  431. System.out.print("Enter your expiry date (xx/xx): ");
  432. details = input.nextLine();
  433. if (details.matches("^(\\d{2}+/+\\d{2})$")) {
  434. CC.setExperationDate(details);
  435. correct = true;
  436. }
  437. else
  438. System.out.println("You have entered an invalid expiry date");
  439. }
  440.  
  441.  
  442. correct = false;
  443. while (!correct) {
  444. System.out.print("Enter your CVC number (3 digits: xxx): ");
  445. details = input.nextLine();
  446. if (details.matches("[0-9]+") && details.length()==3) {
  447. CC.setCvcNumber(details);
  448. correct = true;
  449. }
  450. else
  451. System.out.println("You have entered an invalid CVC number ");
  452. }
  453.  
  454. correct = false;
  455. while (!correct) {
  456. System.out.println("Enter your Email:");
  457. details = input.nextLine();
  458. if (details.matches("[a-zA-Z_0-9]+@+[a-zA-Z_0-9]+.+[a-zA-Z_0-9]")) {
  459. RC.setEmail(details);
  460. correct = true;
  461. }
  462. else
  463. System.out.println("\nYou have enteren an invalid Email ");
  464. }
  465.  
  466. //RC.setPassword();
  467. RC.generateUsername();
  468. RC.writetoFile();
  469.  
  470. System.out.println("\nCONGRATULATIONS");
  471. System.out.println("You have now succesfully created a account in CPH Java Bikes");
  472. System.out.println("\nYour username is: " + RC.getUsername());
  473. System.out.println("Your password is: " + RC.getPassword());
  474. //RC.setUsername();
  475. LV.Reload();
  476. LV.login();
  477. return RC;
  478.  
  479. }
  480.  
  481.  
  482. public void printCustomerDetails(Customer RC) {
  483. // TODO Auto-generated method stub
  484. System.out.println(RC.toString());
  485. }
  486.  
  487.  
  488. public void getCustomerViewDetails() {
  489. // TODO Auto-generated method stub
  490.  
  491. }
  492.  
  493. }
  494.  
  495. package view;
  496.  
  497. import java.util.ArrayList;
  498. import java.util.Scanner;
  499.  
  500. import model.Customer;
  501. import data.ReadAndWrite;
  502.  
  503. public class LoginView {
  504.  
  505. private ArrayList<Customer>customerDetails;
  506.  
  507.  
  508. public LoginView(){
  509. customerDetails = ReadAndWrite.getAllCustomerDetails();
  510. }
  511.  
  512. public void Reload()
  513. {
  514. customerDetails = ReadAndWrite.getAllCustomerDetails();
  515. }
  516.  
  517.  
  518. public boolean login(){
  519.  
  520.  
  521. String password = "";
  522. String username = "";
  523.  
  524.  
  525. Scanner input = new Scanner(System.in);
  526.  
  527.  
  528. int numbersOfTries = 3; //3 = number of tries to log-in.
  529. int counterTries = 0; //The counter for the loop
  530. boolean loggedIn = false; //Sentinel value - breaks the loop
  531.  
  532.  
  533. do{
  534. counterTries++;
  535.  
  536. System.out.println("\nPlease enter your username: ");
  537. username = input.nextLine();
  538.  
  539.  
  540. System.out.println("Please enter your password: ");
  541. password = input.nextLine();
  542.  
  543.  
  544. boolean userFound = false;
  545.  
  546.  
  547. for(int i = 0; i < customerDetails.size() && !loggedIn; i++){
  548. //
  549. if(username.equalsIgnoreCase(customerDetails.get(i).getUsername()) &&
  550. password.equalsIgnoreCase(customerDetails.get(i).getPassword())){
  551. System.out.println("\nGreat! You are now logged in ");
  552. loggedIn = true;
  553. }
  554. }
  555.  
  556. if(!loggedIn){
  557. System.out.println("You have entered the wrong credentials ");
  558. System.out.println("Please try agian ");
  559. }
  560.  
  561. }while(counterTries < numbersOfTries && !loggedIn);
  562.  
  563. return loggedIn;
  564. }
  565.  
  566. }
  567.  
  568. package view;
  569.  
  570. import java.util.Scanner;
  571.  
  572. public class WelcomeView {
  573.  
  574. public WelcomeView(){
  575.  
  576. }
  577.  
  578. public int menuChoice(){
  579. Scanner input = new Scanner(System.in);
  580.  
  581. System.out.println("------------------------------------------");
  582. System.out.println(" WELCOME TO CPH JAVA BIKES SHARING! ");
  583. System.out.println("------------------------------------------");
  584. System.out.println("");
  585. System.out.println("What do you want to do? Press the respective number.");
  586. System.out.println("");
  587. System.out.println("-----------------------");
  588. System.out.println("[1] I'am a customer ");
  589. System.out.println("[2] I'am an existing customer ");
  590. System.out.println("[3] I would like to browse the catalog ");
  591. System.out.println("[4] I would like to rent a Java bike ");
  592. System.out.println("[5] Exit ");
  593. System.out.println("-----------------------");
  594.  
  595. int answer = input.nextInt();
  596.  
  597. return answer;
  598. }//method menuChoice
  599. }//class WelcomeView
  600.  
  601. --------------- CONTROL -----------------
  602.  
  603. package control;
  604.  
  605. import java.util.Scanner;
  606. import model.Customer;
  607. import view.CustomerView;
  608. import view.LoginView;
  609. import view.WelcomeView;
  610.  
  611. public class Booking {
  612.  
  613. public static void main(String[] args) {
  614. // TODO Auto-generated method stub
  615.  
  616. Customer model = new Customer();
  617. CustomerView view = new CustomerView();
  618. CustomerController controller = new CustomerController(model,view);
  619. controller.createCustomer();
  620. controller.print();
  621. }
  622.  
  623. }
  624.  
  625. package control;
  626.  
  627. import model.Customer;
  628. import view.CustomerView;
  629. import view.WelcomeView;
  630.  
  631. public class CustomerController {
  632.  
  633. private Customer myCustomer = new Customer();
  634. private WelcomeView welcome = new WelcomeView();
  635. private CustomerView myView = new CustomerView();
  636.  
  637. public CustomerController(Customer myCustomer, CustomerView myView){
  638. this.myCustomer = myCustomer;
  639. this.myView = myView;
  640. }
  641.  
  642. public void createCustomer(){
  643. myCustomer = myView.getCustomerDetails();
  644. }
  645.  
  646. public void print(){
  647. myView.printCustomerDetails(myCustomer);
  648. }
  649.  
  650. }
  651.  
  652. package control;
  653.  
  654. import model.Customer;
  655. import model.ProductDatabase;
  656. import view.CustomerView;
  657. import view.LoginView;
  658. import view.WelcomeView;
  659.  
  660. public class WebShopping {
  661.  
  662. private static Customer myCustomer = new Customer();
  663. private static WelcomeView welcome = new WelcomeView();
  664. private static CustomerView myView = new CustomerView();
  665. private static LoginView logIn = new LoginView();
  666.  
  667.  
  668. public static void main(String[] args) {
  669. System.out.println("**");
  670. boolean shopping = true;
  671. while (shopping) {
  672. int choice = welcome.menuChoice();
  673.  
  674. switch (choice){
  675. case 1: myCustomer = myView.getCustomerDetails();
  676. break;
  677.  
  678. case 2: if (logIn.login())
  679. System.out.println(" Existing customer - Login ");
  680. break;
  681. case 3: if (logIn.login())
  682. System.out.println(" I would like to browse the JAVA BIKE catalog ");
  683. break;
  684. case 4: if (logIn.login())
  685. System.out.println(" I would like to place an order ");
  686. break;
  687. case 5: shopping = false;
  688. break;
  689. default : System.out.println("You're choice is invalid ");
  690.  
  691.  
  692. }//switch
  693. }//while
  694.  
  695. }//main method
  696.  
  697. }//class
  698.  
  699. ----------- DATA ----------
  700.  
  701. package data;
  702.  
  703. import java.util.ArrayList;
  704. import java.util.Scanner;
  705.  
  706. import model.Bike;
  707. import model.ElectricBike;
  708. import model.Customer;
  709. import model.Product;
  710.  
  711. import java.io.*;
  712.  
  713. public class ReadAndWrite {
  714. public static Scanner readDetails( String file){
  715. Scanner input = new Scanner(System.in);
  716.  
  717. try {
  718. File ReadFile = new java.io.File(file);
  719.  
  720. input = new Scanner(ReadFile);
  721.  
  722. // Close the file
  723.  
  724. }
  725. catch (FileNotFoundException ex){
  726. System.out.println("Error reading the file'" + file + "'");
  727. }
  728. return input;
  729. }//readDetails
  730.  
  731. public static Customer getCustomer(String line){
  732. Customer customerFromFile = new Customer();
  733. // Look for every ";" and turns the values into strings
  734. String[] values = line.split(";");
  735.  
  736. // Change the String type into the correct format
  737. customerFromFile.setLastName(values[0]);
  738. customerFromFile.setFirstName(values[1]);
  739. customerFromFile.setUsername(values[2]);
  740. customerFromFile.setPassword(values[3]);
  741. customerFromFile.setPhoneNumber(values[4]);
  742. customerFromFile.setEmail(values[5]);
  743.  
  744. return customerFromFile;
  745.  
  746. }
  747.  
  748.  
  749. public static Product getProduct(String line){
  750.  
  751. String[] values = line.split(";");
  752. if (values[1].equals("Bike")){
  753. int productID = Integer.parseInt(values[0]);
  754. int gear = Integer.parseInt(values[3]);
  755. double price = Double.parseDouble(values[4]);
  756. Bike BikeFromFile = new Bike(productID,values[1],values[2],gear,price);
  757.  
  758. return BikeFromFile;
  759.  
  760. }
  761.  
  762.  
  763.  
  764. else {
  765. ElectricBike ElectricBikeFromFile = new ElectricBike(Integer.parseInt(values[0]),values[1],values[2],
  766. Integer.parseInt(values[3]),Double.parseDouble(values[4]));
  767.  
  768. return ElectricBikeFromFile; }
  769.  
  770. }
  771.  
  772.  
  773. public static ArrayList<Customer> getAllCustomerDetails(){
  774. ArrayList<Customer> customerList = new ArrayList<Customer>();
  775. Scanner input = readDetails("customer.txt");
  776.  
  777.  
  778. // checking each line
  779. while (input.hasNextLine()) {
  780. customerList.add(getCustomer(input.nextLine()));
  781. //passing each line to the method getCustomer which returns a customer
  782. } //then added to a ArrayList
  783.  
  784. return customerList;
  785. }
  786.  
  787. public static ArrayList<Product> getAllProductDetails(){
  788. ArrayList<Product> productList = new ArrayList<Product>();
  789. Scanner input = readDetails("product.txt");
  790.  
  791. // checking each line
  792. while (input.hasNextLine()) {
  793.  
  794. productList.add(getProduct(input.nextLine()));//passing each line to the method getProduct which returns a product
  795. } //then added to a ArrayList
  796.  
  797. return productList;
  798. }
  799.  
  800.  
  801. public static void WriteDetails(String file, String input){
  802. try{
  803. FileWriter fwriter = new FileWriter(file,true);
  804. PrintWriter output = new java.io.PrintWriter(fwriter);
  805. // Write formatted output to the file
  806. output.println(input);
  807. output.close();
  808. }
  809. catch (IOException ex) {
  810. // if the file is not accessible, print this message
  811. System.out.println("Error writing to file '" + file + "'");
  812. }
  813. }//WriteDetails
  814.  
  815.  
  816. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement