Skip to content

Instantly share code, notes, and snippets.

@zzz67240
Last active July 31, 2019 19:37
Show Gist options
  • Save zzz67240/d37ea60296b4709d5fa472e4a555285c to your computer and use it in GitHub Desktop.
Save zzz67240/d37ea60296b4709d5fa472e4a555285c to your computer and use it in GitHub Desktop.
Driverless car applet
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="driverlesscar" />
</profile>
</annotationProcessing>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xinyang.driverlesscar</groupId>
<artifactId>driverlesscar</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
package Exception;
public class OutOfRangeException extends Exception{
//TODO
public OutOfRangeException() {
super();
}
public OutOfRangeException(String message) {
super(message);
}
}
import place.CarPark;
import vehicle.DriverlessCar;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
CarPark carPark = new CarPark();
DriverlessCar driverlessCar = new DriverlessCar();
//Set dimension of the car park
System.out.println("Let's set the dimension of the car park first.");
while (true){
System.out.println("Please input the X axis dimension:");
String xInput = scanner.nextLine();
System.out.println("Please input the Y axis dimension:");
String yInput = scanner.nextLine();
if(isInt(xInput) && Integer.parseInt(xInput) > 0 &&
isInt(yInput) && Integer.parseInt(yInput) > 0 ){
carPark.setxAxisDimension(Integer.parseInt(xInput));
carPark.setyAxisDimension(Integer.parseInt(yInput));
System.out.println("Car park dimension is set to: " + carPark.getxAxisDimension() + ", " + carPark.getyAxisDimension());
break;
} else {
System.out.println("Wrong input format or input equals to or smaller than 0. Please input again.");
}
}
//Place the car
System.out.println("Let's place the car in the car park.");
while (true){
System.out.println("Please input the X coordinate:");
String xInput = scanner.nextLine();
System.out.println("Please input the Y coordinate:");
String yInput = scanner.nextLine();
System.out.println("Where is the car facing to? 1=North, 2=East, 3=South, 4=West. ");
String orientationInput = scanner.nextLine();
if(isInt(xInput) && isInt(yInput) && isInt(orientationInput)){
int x = Integer.parseInt(xInput);
int y = Integer.parseInt(yInput);
int o = Integer.parseInt(orientationInput);
if(x > 0 && x <= carPark.getxAxisDimension() &&
y > 0 && y <= carPark.getyAxisDimension()){
if(o >= 1 && o <= 4){
driverlessCar.setPositionX(x);
driverlessCar.setPositionY(y);
driverlessCar.setOrientation(o);
System.out.println("Car is place at: " + driverlessCar.getPositionX() + ", " + driverlessCar.getPositionY()
+ " and facing: " + driverlessCar.getOrientation());
break;
} else {
System.out.println("Incorrect orientation. Please input again.");
}
} else {
System.out.println("The position is too small or out of car park range. Please input again.");
}
} else {
System.out.println("Wrong input format. Please input again.");
}
}
//Move the car
while (true){
System.out.println("Would you like to turn the car clockwise? 0=No, 1=Yes.");
String turnOriInput = scanner.nextLine();
if(isInt(turnOriInput)){
switch (Integer.parseInt(turnOriInput)){
case 0:
System.out.println("Keep current orientation.");
break;
case 1:
driverlessCar.turnClockwise();
System.out.println("Turn the car clockwise, it's facing: " + driverlessCar.getOrientation());
break;
default:
System.out.println("Wrong input range. Please input again.");
}
} else {
System.out.println("Wrong format. Please input again.");
}
}
}
public static boolean isInt(String string){
try{
Integer.parseInt(string);
return true;
} catch (Exception e){
return false;
}
}
}
package place;
public class CarPark {
private int xAxisDimension;
private int yAxisDimension;
public int getxAxisDimension() {
return xAxisDimension;
}
public void setxAxisDimension(int xAxisDimension) {
this.xAxisDimension = xAxisDimension;
}
public int getyAxisDimension() {
return yAxisDimension;
}
public void setyAxisDimension(int yAxisDimension) {
this.yAxisDimension = yAxisDimension;
}
}
package vehicle;
public interface Car {
void move(String command);
int getPositionX();
int getPositionY();
String getOrientation();
}
package vehicle;
public class DriverlessCar implements Car{
private int orientation;
private int positionX;
private int positionY;
public void move(String command) {
}
public void turnClockwise(){
orientation++;
if(orientation > 4){
orientation = 1;
}
}
public String getOrientation() {
switch (orientation){
case 1:
return "North";
case 2:
return "East";
case 3:
return "South";
case 4:
return "West";
}
return null;
}
public void setOrientation(int orientation) {
this.orientation = orientation;
}
public int getPositionX() {
return positionX;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionY() {
return positionY;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment