Illustrating Object Types, Primitive Types, and Boolean Expressions
Car.java
import java.util.Scanner;
/**
* Class representing Car type of vehicles
*/
public class Car extends Vehicle {
/**
* Sedan car type literal constant
*/
private static final String SedanType = "Sedan";
/**
* Coupe car type literal constant
*/
private static final String CoupeType = "Coupe";
/**
* Wagon car type literal constant
*/
private static final String WagonType = "Wagon";
/**
* Car type
*/
private String type;
/**
* Presence of car towing package
*/
private boolean towingPackage;
/**
* Default no-argument constructor. Reads object attributes from the console
*/
public Car() {
super();
Scanner scanner = Main.getScanner();
type = "";
while (type.isEmpty()) {
System.out.println("What type of Car is this?");
System.out.println("\t\t1. " + SedanType);
System.out.println("\t\t2. " + CoupeType);
System.out.println("\t\t3. " + WagonType);
System.out.print("\t\tChoice: ");
switch (scanner.nextLine().trim()) {
case "1":
type = SedanType;
break;
case "2":
type = CoupeType;
break;
case "3":
type = WagonType;
break;
default:
System.out.println("Invalid input. Please, try again");
}
}
boolean chosen = false;
while (!chosen) {
System.out.println("Does this car has a towing package?");
System.out.println("\t\t1. Yes");
System.out.println("\t\t2. No");
System.out.print("\t\tChoice: ");
switch (scanner.nextLine().trim()) {
case "1":
towingPackage = true;
chosen = true;
break;
case "2":
towingPackage = false;
chosen = true;
break;
default:
System.out.println("Invalid input. Please, try again");
}
}
}
/**
* Type field getter
* @return car type
*/
public String getType() {
return type;
}
/**
* Type field setter
* @param type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* Towing package field getter
* @return car towing package presence
*/
public boolean isTowingPackage() {
return towingPackage;
}
/**
* Towing package field setter
* @param towingPackage to set
*/
public void setTowingPackage(boolean towingPackage) {
this.towingPackage = towingPackage;
}
/**
* Overriden toString method
* @return string summary of car
*/
@Override
public String toString() {
return "Car:" + System.lineSeparator() +
super.toString() +
"\t\t" + String.format("%-10s%s", "Type:", getType()) + System.lineSeparator() +
"\t\t" + String.format("%-10s%s", "Towing:", isTowingPackage() ? "included" : "not included") + System.lineSeparator();
}
}
Main.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
/**
* Main driver class of the application
*/
public class Main {
/**
* Scanner instance for parsing console input
*/
public static Scanner scanner = null;
/**
* Main method of the application
* @param args application command line arguments
*/
public static void main(String[] args) {
Collection vehicles = new ArrayList<>();
System.out.println("Vehicle Ordering System" + System.lineSeparator());
boolean otherVehicle = true;
scanner = new Scanner(System.in);
while (otherVehicle) {
boolean choice = false;
while (!choice) {
String choiceStr = "";
System.out.print("Do you want to order a Truck (T/t) or Car (C/c)? ");
choiceStr = scanner.nextLine().trim().toLowerCase();
if (choiceStr.equals("c")) {
vehicles.add(new Car());
choice = true;
}
else if (choiceStr.equals("t")) {
vehicles.add(new Truck());
choice = true;
}
else {
System.out.println("Invalid input. Please, try again");
}
}
String choiceStr = "";
System.out.println();
System.out.print("Do you want to order another vehicle? ");
choiceStr = scanner.nextLine().trim().toLowerCase();
if (!"y".equals(choiceStr)) {
otherVehicle = false;
}
}
List trucks = new ArrayList<>();
List cars = new ArrayList<>();
for (Vehicle v : vehicles) {
if (v instanceof Truck) {
trucks.add((Truck) v);
}
if (v instanceof Car) {
cars.add((Car) v);
}
}
System.out.print("Trucks:\n");
for (Truck t : trucks) {
System.out.println(t.toString() + System.lineSeparator());
}
System.out.println();
System.out.print("Cars:\n");
for (Car c : cars) {
System.out.println(c.toString() + System.lineSeparator());
}
System.out.println();
System.out.println("Thank you for using Vehicle Ordering System.");
}
/**
* Static getter for scanner object
* @return
*/
public static Scanner getScanner() {
return scanner;
}
}
Truck.java
import java.util.Scanner;
/**
* Class representing Truck type of vehicles
*/
public class Truck extends Vehicle {
/**
* Halfton truck size literal constant
*/
private static final String HalftonSize = "Half-ton";
/**
* Fullton truck size literal constant
*/
private static final String FulltonSize = "Full ton";
/**
* Truck size
*/
private String size;
/**
* Truck engine size
*/
private boolean engineSizeBig;
/**
* Default no-argument constructor. Reads object attributes from the console
*/
public Truck() {
super();
Scanner scanner = Main.getScanner();
size = "";
while (size.isEmpty()) {
System.out.println("What size truck is this?");
System.out.println("\t\t1. " + HalftonSize);
System.out.println("\t\t2. " + FulltonSize);
System.out.print("\t\tChoice: ");
switch (scanner.nextLine().trim()) {
case "1":
size = HalftonSize;
break;
case "2":
size = FulltonSize;
break;
default:
System.out.println("Invalid input. Please, try again");
}
}
boolean chosen = false;
while (!chosen) {
System.out.println("What size truck is this?");
System.out.println("\t\t1. Really big");
System.out.println("\t\t2. Not so big");
System.out.print("\t\tChoice: ");
switch (scanner.nextLine().trim()) {
case "1":
engineSizeBig = true;
chosen = true;
break;
case "2":
engineSizeBig = false;
chosen = true;
break;
default:
System.out.println("Invalid input. Please, try again");
}
}
}
/**
* Size field getter
* @return truck type
*/
public String getSize() {
return size;
}
/**
* Size package field setter
* @param size to set
*/
public void setSize(String size) {
this.size = size;
}
/**
* EngineSizeBig field getter
* @return truck engine size
*/
public boolean isEngineSizeBig() {
return engineSizeBig;
}
/**
* EngineSizeBig field setter
* @param engineSizeBig to set
*/
public void setEngineSizeBig(boolean engineSizeBig) {
this.engineSizeBig = engineSizeBig;
}
/**
* Overriden toString method
* @return string summary of truck
*/
@Override
public String toString() {
return "Car:" + System.lineSeparator() +
super.toString() +
"\t\t" + String.format("%-10s%s", "Load:", getSize()) + System.lineSeparator() +
"\t\t" + String.format("%-10s%s", "Engine:", isEngineSizeBig() ? "Really big" : "Not so big") + System.lineSeparator();
}
}
Vehicle.java
import java.util.Scanner;
/**
* Abstract class, representing common entity of vehicles: cars and trucks
*/
public abstract class Vehicle {
private static int vehicleCounter = 1;
/**
* Vehicle id
*/
private final int id;
/**
* Vehicle model
*/
private String model;
/**
* Vehicle model
*/
private String color;
/**
* Vehicle cost
*/
private double cost;
/**
* Vehicle discount for ordering
*/
private double discount;
/**
* Default no-argument constructor. Reads object attributes from the console
*/
public Vehicle() {
if (vehicleCounter == Integer.MAX_VALUE) {
throw new RuntimeException("Can not create more vehicles");
}