Quillewd
11/14/2019 - 12:55 AM

Code Challenge #3

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        //create a scanner for input
        Scanner userInput = new Scanner(System.in);

        //run loop into infinity and beyond
        while(true) {
            System.out.println("\n#---------------------------------------------#");
            System.out.println("# Welcome to the Vehicle Registration System  #");
            System.out.println("# Select one of our wonderful options please. #");
            System.out.println("#---------------------------------------------#");
            System.out.println("1) Register a new Vehicle");
            System.out.println("2) Edit a Vehicle");
            System.out.println("3) List Vehicles");
            System.out.println("4) Exit");


            String inputString = userInput.next();
            if (checkInt(inputString)) {
                userInput.nextLine();

                switch (Integer.parseInt(inputString)) {

                    case 1:
                        //have the user enter info for the vehicle
                        System.out.println("Enter Vehicle Name");

                        //save vehicle name
                        String vName = userInput.nextLine();

                        //have the user enter info for the vehicle
                        System.out.println("Enter Vehicle Color");

                        //save vehicle name
                        String vColor = userInput.nextLine();

                        //have the user enter info for the vehicle
                        System.out.println("Enter Vehicle Wheel Count");

                        //save vehicle name
                        String vWheels = userInput.next();
                        userInput.nextLine(); //skip crap
                        if(!checkInt(vWheels)){
                            System.out.println("Invalid number of wheels.");
                            break;
                        }
                        int wheelCount = Integer.parseInt(vWheels);

                        System.out.println(vName + " " + vColor + " " + vWheels);
                        //check if it's a bike or a car and register it
                        if (wheelCount == 2) {
                            Vehicle.registerVehicle(new Bike(vName, wheelCount, vColor));
                        } else {
                            //register a car if wheels are more than 2
                            Car car = new Car(vName, wheelCount, vColor);
                            Vehicle.registerVehicle(car);
                        }
                        break;


                    case 2:
                        //prompt for them to enter the VIN number
                        System.out.println("Enter the Vehicle Registration Value");
                        long vin = userInput.nextLong();

                        Vehicle.editVehicle(vin);
                        break;

                    case 3:
                        Vehicle.listRegisteredVehicles();
                        break;

                    case 4:
                        System.out.println("Exiting");
                        System.exit(0);
                        break;

                    default:
                        System.out.println("Invalid Input.");

                }
            } else { System.out.println("ERROR: INVALID INPUT");}
        }
    }

    /**
     *
     * @param input string
     * @return whether it is acceptable int or not
     */
    public static boolean checkInt(String input){
        return (input.length() > 0 && input.matches("[0-9]+")) ? true : false;
    }
}
import java.util.Random;

public class Car extends Vehicle {
    //local vars
    private String name;
    private String color;
    private int wheels;
    private long registration_number;

    //new random number gen
    Random randomRegister = new Random();

    //constructor for bike

    /**
     *
     * @param name name of the vehicle
     * @param wheels wheels the car has
     * @param color color of the vehicle
     */
    public Car(String name, int wheels, String color){
        this.color = color;
        this.wheels = wheels;
        this.name = name;
        this.registration_number = randomRegister.nextInt(100000);
    }

    @Override
    void setName(String name) {
        this.name = name;
    }

    @Override
    void setColor(String color) {
        this.color = color;
    }

    @Override
    void setWheels(int wheels) {
        this.wheels = wheels;
    }

    @Override
    String getColor() {
        return this.color;
    }

    @Override
    String getName() {
        return this.name;
    }

    @Override
    int getWheels() {
        return this.wheels;
    }

    @Override
    long getRegistrationNumber() {
        return this.registration_number;
    }
}
import java.util.Random;

public class Bike extends Vehicle {
    //local vars
    private String name;
    private String color;
    private int wheels;
    private long registration_number;

    //new random number gen
    Random randomRegister = new Random();

    //constructor for bike

    /**
     *
     * @param name name of the bike
     * @param wheels amount of wheels the vehicle has (it better be two or I'm brekaing kneecaps)
     * @param color color of the vehicle
     */
    public Bike(String name, int wheels, String color){
        this.color = color;
        this.wheels = wheels;
        this.name = name;
        this.registration_number = randomRegister.nextInt(100000);
    }

    @Override
    void setName(String name) {
        this.name = name;
    }

    @Override
    void setColor(String color) {
        this.color = color;
    }

    @Override
    void setWheels(int wheels) {
        this.wheels = wheels;
    }

    @Override
    String getColor() {
        return this.color;
    }

    @Override
    String getName() {
        return this.name;
    }

    @Override
    int getWheels() {
        return this.wheels;
    }

    @Override
    long getRegistrationNumber() {
        return this.registration_number;
    }
}
import java.util.ArrayList;
import java.util.Scanner;

public abstract class Vehicle {

    //arraylist for vehicles
    static ArrayList<Vehicle> vehicleList = new ArrayList<>();

    //static methods
    static void registerVehicle(Vehicle vehicle){
        vehicleList.add(vehicle);
        System.out.println("Registered Vehicle. Vin is" + vehicle.getRegistrationNumber());
    }

    static void listRegisteredVehicles(){
        if(vehicleList.size() > 0) {
            for (int i = 0; i < vehicleList.size(); i++) {
                //fanciness
                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                //get vehicle at current index
                Vehicle curRide = vehicleList.get(i);
                System.out.println("Name: " + curRide.getName());
                System.out.println("Color: " + curRide.getColor());
                System.out.println("Wheels: " + curRide.getWheels());
                System.out.println("Registration Number: " + curRide.getRegistrationNumber());

                System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            }
        } else { System.out.println("No Vehicles Registered");}
    }

    static void editVehicle(long vin){
        for(int i = 0; i < vehicleList.size(); i++){
            //save vehicle to vehicle
            Vehicle vehicle = vehicleList.get(i);

            if(vehicle.getRegistrationNumber() == vin){
                //Create a new scanner for input
                Scanner editInput = new Scanner(System.in);

                //prompt for new name
                System.out.println("Enter new Name");
                String newName = editInput.nextLine();

                //prompt for new color
                System.out.println("Enter new Color");
                String newColor = editInput.nextLine();

                System.out.println("Enter new Wheel Count");
                int newWheels = editInput.nextInt();

                if(newName.toLowerCase() != "keep"){
                    vehicle.setName(newName); //set new color
                }

                //if the user typed anything but keep
                if(newColor.toLowerCase() != "keep"){
                    vehicle.setColor(newColor); //set new color
                }

                //if wheels are not negative
                if(newWheels > 0) {
                    vehicle.setWheels(newWheels); //set new wheel count
                }
                System.out.println("Vehicle Successfully Edited");

            } else {
                System.out.println("Invalid Registration Number");
            }
        }
    }

    //setters
    abstract void setName(String name);
    abstract void setColor(String color);
    abstract void setWheels(int wheels);

    //getters
    abstract String getColor();
    abstract String getName();
    abstract int getWheels();
    abstract long getRegistrationNumber();
}