Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------- MODEL --------------
- package model;
- import model.Product;
- public class Bike extends Product {
- private String colour;
- private int gear;
- private static int stockAvailable;
- public Bike(){
- }
- public Bike(int productID, String productName, String colour, int gear, double productPrice) {
- super(productID, productName, productPrice);
- this.colour = colour;
- this.gear = gear;
- stockAvailable++;
- }
- public String getColour() {
- return colour;
- }
- public void setColour(String colour) {
- this.colour = colour;
- }
- public int getGear() {
- return gear;
- }
- public void setGear(int gear) {
- this.gear = gear;
- }
- public void rentBike() {
- stockAvailable--;
- }
- public static int getStockAvailable() {
- return stockAvailable;
- }
- @Override
- public String toString() {
- return "Bike [colour=" + colour + ", gear=" + gear + ", id=" +getProductID()
- + ", productPrice=" + getProductPrice() + ", stock=" + getStockAvailable()+ "]";
- }
- }
- package model;
- public class CreditCard {
- private String cardNumber, experationDate, cvcNumber;
- public CreditCard(){
- }
- public CreditCard(String cardNum, String exDate, String cvc){
- cardNumber = cardNum;
- experationDate = exDate;
- cvcNumber = cvc;
- }
- public String getCardNumber() {
- return cardNumber;
- }
- public void setCardNumber(String cardNumber) {
- this.cardNumber = cardNumber;
- }
- public String getExperationDate() {
- return experationDate;
- }
- public void setExperationDate(String experationDate) {
- this.experationDate = experationDate;
- }
- public String getCvcNumber() {
- return cvcNumber;
- }
- public void setCvcNumber(String cvcNumber) {
- this.cvcNumber = cvcNumber;
- }
- }
- package model;
- import data.ReadAndWrite;
- public class Customer {
- private String firstName, lastName, phoneNumber, email, username, password;
- public Customer(){
- }
- public Customer(String fName, String lName, String phoneNum, String creditCa, String eMa){
- firstName = fName;
- lastName = lName;
- phoneNumber = phoneNum;
- email = eMa;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getPhonenumber() {
- return phoneNumber;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- //public void setUsername() {
- //this.username = firstName.substring(0, 3)+surname.substring(0, 3);
- // }
- //public void setPassword() {
- //this.password = surname.substring(0, 3) + phoneNumber.substring(2, 5);
- //}
- public void generateUsername() {
- this.username = firstName.substring(0, 3)+lastName.substring(0, 3);
- this.password = lastName.substring(0, 3) + phoneNumber.substring(2, 5);
- }
- public String toString(){
- return (firstName+ " " + lastName + " " + phoneNumber +" " + " "+ email);
- }
- public void writetoFile(){
- String details = lastName+ ";" + firstName + ";" + getUsername() + ";" + getPassword() + ";" + phoneNumber +";"+ email+ ";";
- ReadAndWrite.WriteDetails("customer.txt", details);
- }
- }
- package model;
- import model.Product;
- public class ElectricBike extends Product {
- private String colour;
- private int watt;
- private static int stockAvailable;
- public ElectricBike(){
- }
- public ElectricBike(int productID, String productName, String colour, int watt, double productPrice) {
- super(productID, productName, productPrice);
- this.colour = colour;
- this.watt = watt;
- stockAvailable++;
- }
- public String getColour() {
- return colour;
- }
- public void setColour(String colour) {
- this.colour = colour;
- }
- public int getWatt() {
- return watt;
- }
- public void setWatt(int watt) {
- this.watt = watt;
- }
- public void rentBike() {
- stockAvailable--;
- }
- public static int getStockAvailable() {
- return stockAvailable;
- }
- @Override
- public String toString() {
- return "Bike [colour=" + colour + ", watt=" + watt + ", id=" +getProductID()
- + ", productPrice=" + getProductPrice() + ", stock=" + getStockAvailable()+ "]";
- }
- }
- package model;
- public class Product {
- private String productName;
- private static int id;
- private int productID;
- private double productPrice;
- public Product(int productID, String productName, double productPrice){
- productID = id;
- this.productName = productName;
- this.productPrice = productPrice;
- id++;
- }
- public int getProductID() {
- return productID;
- }
- public void setProductID(int productID) {
- this.productID = productID;
- }
- public Product(){
- id++;
- }
- public String getProductName() {
- return productName;
- }
- public void setProductName(String productName) {
- this.productName = productName;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public double getProductPrice() {
- return productPrice;
- }
- public void setProductPrice(double productPrice) {
- this.productPrice = productPrice;
- }
- }
- package model;
- import java.util.ArrayList;
- import data.ReadAndWrite;
- public class ProductDatabase {
- ArrayList<Product> database;
- public ProductDatabase(){
- database = ReadAndWrite.getAllProductDetails();
- }
- }
- ------------ VIEW -------------
- package view;
- import java.util.Scanner;
- import model.CreditCard;
- import model.Customer;
- public class CustomerView {
- private Scanner input;
- public CustomerView(){
- }
- public Customer getCustomerDetails(){
- input = new Scanner(System.in);
- String details;
- Customer RC = new Customer();
- CreditCard CC = new CreditCard();
- LoginView LV = new LoginView();
- boolean correct = false;
- while (!correct) {
- System.out.print("Enter your first name: ");
- details = input.nextLine();
- RC.setFirstName(details);
- correct = true;
- }
- correct = false;
- while (!correct) {
- System.out.print("Enter your last name: ");
- details = input.nextLine();
- RC.setLastName(details);
- correct = true;
- }
- correct = false;
- while (!correct) {
- System.out.print("Enter your Phone Number (8 digits): ");
- details = input.nextLine();
- if (details.matches("[0-9]+") && details.length()==8) {
- RC.setPhoneNumber(details);
- correct = true;
- }
- else
- System.out.println("You have entered an invalid number");
- }
- correct = false;
- while (!correct) {
- System.out.print("Enter your card number (16 digits): ");
- details = input.nextLine();
- if (details.matches("^(\\d{4}?\\d{4}?\\d{4}?\\d{4})")) {
- CC.setCardNumber(details);
- correct = true;
- }
- else
- System.out.println("You have entered an invalid credit card number");
- }
- correct = false;
- while (!correct) {
- System.out.print("Enter your expiry date (xx/xx): ");
- details = input.nextLine();
- if (details.matches("^(\\d{2}+/+\\d{2})$")) {
- CC.setExperationDate(details);
- correct = true;
- }
- else
- System.out.println("You have entered an invalid expiry date");
- }
- correct = false;
- while (!correct) {
- System.out.print("Enter your CVC number (3 digits: xxx): ");
- details = input.nextLine();
- if (details.matches("[0-9]+") && details.length()==3) {
- CC.setCvcNumber(details);
- correct = true;
- }
- else
- System.out.println("You have entered an invalid CVC number ");
- }
- correct = false;
- while (!correct) {
- System.out.println("Enter your Email:");
- details = input.nextLine();
- if (details.matches("[a-zA-Z_0-9]+@+[a-zA-Z_0-9]+.+[a-zA-Z_0-9]")) {
- RC.setEmail(details);
- correct = true;
- }
- else
- System.out.println("\nYou have enteren an invalid Email ");
- }
- //RC.setPassword();
- RC.generateUsername();
- RC.writetoFile();
- System.out.println("\nCONGRATULATIONS");
- System.out.println("You have now succesfully created a account in CPH Java Bikes");
- System.out.println("\nYour username is: " + RC.getUsername());
- System.out.println("Your password is: " + RC.getPassword());
- //RC.setUsername();
- LV.Reload();
- LV.login();
- return RC;
- }
- public void printCustomerDetails(Customer RC) {
- // TODO Auto-generated method stub
- System.out.println(RC.toString());
- }
- public void getCustomerViewDetails() {
- // TODO Auto-generated method stub
- }
- }
- package view;
- import java.util.ArrayList;
- import java.util.Scanner;
- import model.Customer;
- import data.ReadAndWrite;
- public class LoginView {
- private ArrayList<Customer>customerDetails;
- public LoginView(){
- customerDetails = ReadAndWrite.getAllCustomerDetails();
- }
- public void Reload()
- {
- customerDetails = ReadAndWrite.getAllCustomerDetails();
- }
- public boolean login(){
- String password = "";
- String username = "";
- Scanner input = new Scanner(System.in);
- int numbersOfTries = 3; //3 = number of tries to log-in.
- int counterTries = 0; //The counter for the loop
- boolean loggedIn = false; //Sentinel value - breaks the loop
- do{
- counterTries++;
- System.out.println("\nPlease enter your username: ");
- username = input.nextLine();
- System.out.println("Please enter your password: ");
- password = input.nextLine();
- boolean userFound = false;
- for(int i = 0; i < customerDetails.size() && !loggedIn; i++){
- //
- if(username.equalsIgnoreCase(customerDetails.get(i).getUsername()) &&
- password.equalsIgnoreCase(customerDetails.get(i).getPassword())){
- System.out.println("\nGreat! You are now logged in ");
- loggedIn = true;
- }
- }
- if(!loggedIn){
- System.out.println("You have entered the wrong credentials ");
- System.out.println("Please try agian ");
- }
- }while(counterTries < numbersOfTries && !loggedIn);
- return loggedIn;
- }
- }
- package view;
- import java.util.Scanner;
- public class WelcomeView {
- public WelcomeView(){
- }
- public int menuChoice(){
- Scanner input = new Scanner(System.in);
- System.out.println("------------------------------------------");
- System.out.println(" WELCOME TO CPH JAVA BIKES SHARING! ");
- System.out.println("------------------------------------------");
- System.out.println("");
- System.out.println("What do you want to do? Press the respective number.");
- System.out.println("");
- System.out.println("-----------------------");
- System.out.println("[1] I'am a customer ");
- System.out.println("[2] I'am an existing customer ");
- System.out.println("[3] I would like to browse the catalog ");
- System.out.println("[4] I would like to rent a Java bike ");
- System.out.println("[5] Exit ");
- System.out.println("-----------------------");
- int answer = input.nextInt();
- return answer;
- }//method menuChoice
- }//class WelcomeView
- --------------- CONTROL -----------------
- package control;
- import java.util.Scanner;
- import model.Customer;
- import view.CustomerView;
- import view.LoginView;
- import view.WelcomeView;
- public class Booking {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Customer model = new Customer();
- CustomerView view = new CustomerView();
- CustomerController controller = new CustomerController(model,view);
- controller.createCustomer();
- controller.print();
- }
- }
- package control;
- import model.Customer;
- import view.CustomerView;
- import view.WelcomeView;
- public class CustomerController {
- private Customer myCustomer = new Customer();
- private WelcomeView welcome = new WelcomeView();
- private CustomerView myView = new CustomerView();
- public CustomerController(Customer myCustomer, CustomerView myView){
- this.myCustomer = myCustomer;
- this.myView = myView;
- }
- public void createCustomer(){
- myCustomer = myView.getCustomerDetails();
- }
- public void print(){
- myView.printCustomerDetails(myCustomer);
- }
- }
- package control;
- import model.Customer;
- import model.ProductDatabase;
- import view.CustomerView;
- import view.LoginView;
- import view.WelcomeView;
- public class WebShopping {
- private static Customer myCustomer = new Customer();
- private static WelcomeView welcome = new WelcomeView();
- private static CustomerView myView = new CustomerView();
- private static LoginView logIn = new LoginView();
- public static void main(String[] args) {
- System.out.println("**");
- boolean shopping = true;
- while (shopping) {
- int choice = welcome.menuChoice();
- switch (choice){
- case 1: myCustomer = myView.getCustomerDetails();
- break;
- case 2: if (logIn.login())
- System.out.println(" Existing customer - Login ");
- break;
- case 3: if (logIn.login())
- System.out.println(" I would like to browse the JAVA BIKE catalog ");
- break;
- case 4: if (logIn.login())
- System.out.println(" I would like to place an order ");
- break;
- case 5: shopping = false;
- break;
- default : System.out.println("You're choice is invalid ");
- }//switch
- }//while
- }//main method
- }//class
- ----------- DATA ----------
- package data;
- import java.util.ArrayList;
- import java.util.Scanner;
- import model.Bike;
- import model.ElectricBike;
- import model.Customer;
- import model.Product;
- import java.io.*;
- public class ReadAndWrite {
- public static Scanner readDetails( String file){
- Scanner input = new Scanner(System.in);
- try {
- File ReadFile = new java.io.File(file);
- input = new Scanner(ReadFile);
- // Close the file
- }
- catch (FileNotFoundException ex){
- System.out.println("Error reading the file'" + file + "'");
- }
- return input;
- }//readDetails
- public static Customer getCustomer(String line){
- Customer customerFromFile = new Customer();
- // Look for every ";" and turns the values into strings
- String[] values = line.split(";");
- // Change the String type into the correct format
- customerFromFile.setLastName(values[0]);
- customerFromFile.setFirstName(values[1]);
- customerFromFile.setUsername(values[2]);
- customerFromFile.setPassword(values[3]);
- customerFromFile.setPhoneNumber(values[4]);
- customerFromFile.setEmail(values[5]);
- return customerFromFile;
- }
- public static Product getProduct(String line){
- String[] values = line.split(";");
- if (values[1].equals("Bike")){
- int productID = Integer.parseInt(values[0]);
- int gear = Integer.parseInt(values[3]);
- double price = Double.parseDouble(values[4]);
- Bike BikeFromFile = new Bike(productID,values[1],values[2],gear,price);
- return BikeFromFile;
- }
- else {
- ElectricBike ElectricBikeFromFile = new ElectricBike(Integer.parseInt(values[0]),values[1],values[2],
- Integer.parseInt(values[3]),Double.parseDouble(values[4]));
- return ElectricBikeFromFile; }
- }
- public static ArrayList<Customer> getAllCustomerDetails(){
- ArrayList<Customer> customerList = new ArrayList<Customer>();
- Scanner input = readDetails("customer.txt");
- // checking each line
- while (input.hasNextLine()) {
- customerList.add(getCustomer(input.nextLine()));
- //passing each line to the method getCustomer which returns a customer
- } //then added to a ArrayList
- return customerList;
- }
- public static ArrayList<Product> getAllProductDetails(){
- ArrayList<Product> productList = new ArrayList<Product>();
- Scanner input = readDetails("product.txt");
- // checking each line
- while (input.hasNextLine()) {
- productList.add(getProduct(input.nextLine()));//passing each line to the method getProduct which returns a product
- } //then added to a ArrayList
- return productList;
- }
- public static void WriteDetails(String file, String input){
- try{
- FileWriter fwriter = new FileWriter(file,true);
- PrintWriter output = new java.io.PrintWriter(fwriter);
- // Write formatted output to the file
- output.println(input);
- output.close();
- }
- catch (IOException ex) {
- // if the file is not accessible, print this message
- System.out.println("Error writing to file '" + file + "'");
- }
- }//WriteDetails
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement