Skip to content

Instantly share code, notes, and snippets.

@Cubik65536
Created February 25, 2024 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cubik65536/9d2d6871debd13f12bc83a5e8527b195 to your computer and use it in GitHub Desktop.
Save Cubik65536/9d2d6871debd13f12bc83a5e8527b195 to your computer and use it in GitHub Desktop.
An Interesting Problem #1
package org.qianq;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* An enum containing several brands of planes.
*/
enum PlaneBrands {
AIRBUS, BOEING, COMAC
}
/**
* An interface containing several critical methods for a vehicle.
*/
interface Vehicle {
/**
* Get the ID of the current vehicle.
* @return the ID of the vehicle
*/
String getId();
/**
* Get the name of the current vehicle.
* @return the name of the vehicle
*/
String getName();
/**
* Get the capacity (the maximum number of people onboard) of the current vehicle.
* @return the capacity of the vehicle
*/
int capacity();
/**
* Make the vehicle transport a certain number of people from the departure to the destination.
* @param people the number of people to transport
* @param departure the departure location
* @param destination the destination location
*/
void transport(int people, String departure, String destination);
/**
* Get the information of the current vehicle.
* @return the information of the vehicle, in form of "Vehicle [ID]: [Name]"
*/
default String getInfo() {
return String.format("Vehicle %s: %s", getId(), getName());
}
}
class Plane implements Vehicle {
/**
* The current ID number.
*/
private static int currentId = 0;
/**
* The ID of the plane.
*/
private String id;
/**
* The name of the name.
*/
private String name;
/**
* The maximum number of people that can be onboard.
*/
private int capacity;
/**
* The brand of the plane.
*/
private PlaneBrands planeBrand;
/**
* The cost to fly the plane once.
*/
private int cost;
/**
* The price of a ticket.
*/
private int ticket;
public Plane(String name, int capacity, PlaneBrands planeBrand, int cost, int ticket) {
this.id = switch (planeBrand) {
case AIRBUS -> String.format("P%03d-A", ++currentId);
case BOEING -> String.format("P%03d-B", ++currentId);
case COMAC -> String.format("P%03d-C", ++currentId);
};
this.name = name;
this.capacity = capacity;
this.planeBrand = planeBrand;
this.cost = cost;
this.ticket = ticket;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int capacity() {
return capacity;
}
public void transport(int people, String departure, String destination) {
System.out.printf("fly %d people from %s to %s\n", people, departure, destination);
}
public PlaneBrands getPlaneBrand() {
return planeBrand;
}
public int getCost() {
return cost;
}
public int getTicket() {
return ticket;
}
}
/**
* A company in operation.
*/
class Company {
/**
* The name of the company.
*/
private String name;
/**
* How much money the company has.
*/
private int balance = 10000;
public Company(String name) {
this.name = name;
}
/**
* Gain money.
* @param amount amount of money the company gained
*/
void gain(int amount) {
balance += amount;
}
/**
* Spend money.
* @param amount amount of money the company spent
*/
void spend(int amount) {
balance -= amount;
}
public int getBalance() {
return balance;
}
}
/**
* A company in air transport.
*/
class TransportCompany extends Company {
/**
* The planes that the company has.
*/
public ArrayList<Plane> planes = new ArrayList<>();
public TransportCompany(String name) {
super(name);
}
/**
* Show all the planes that the company has, in ascending order by capacity.
*/
public void showVehicles() {
boolean swapped;
do {
swapped = false;
for (int i = 0; i < planes.size() - 1; i++) {
if (planes.get(i).capacity() > planes.get(i + 1).capacity()) {
Plane temp = planes.get(i);
planes.set(i, planes.get(i + 1));
planes.set(i + 1, temp);
swapped = true;
}
}
} while (swapped);
for (Plane plane : planes) {
System.out.printf("Plane %s: %s (brand: %s)\n", plane.getId(), plane.getName(), plane.getPlaneBrand());
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name for the transport company:");
String name = scanner.next();
TransportCompany transportCompany = new TransportCompany(name);
boolean running = true;
while (running) {
System.out.println("Menu:");
System.out.println("1 - Add plane to the company.");
System.out.println("2 - List the plane that the company currently holds.");
System.out.println("3 - Call for a transport job.");
System.out.println("4 - Show how many money the company has.");
System.out.println("5 - Exit");
String choice = scanner.next();
switch (choice) {
case "1" -> {
System.out.print("Enter a name for the plane: ");
String planeName = scanner.next();
System.out.print("Enter a capacity for the plane: ");
int capacity = scanner.nextInt();
System.out.print("Enter a brand for the plane: ");
boolean valid = true;
String brandString = scanner.next().toUpperCase();
PlaneBrands planeBrand = null;
switch (brandString) {
case "AIRBUS" -> planeBrand = PlaneBrands.AIRBUS;
case "BOEING" -> planeBrand = PlaneBrands.BOEING;
case "COMAC" -> planeBrand = PlaneBrands.COMAC;
default -> valid = false;
}
if (!valid) {
System.out.println("Plane Brand Invalid!");
break;
}
System.out.print("Enter the cost to fly the plane: ");
int cost = scanner.nextInt();
System.out.print("Enter the price of a ticket: ");
int ticket = scanner.nextInt();
Plane plane = new Plane(name, capacity, planeBrand, cost, ticket);
transportCompany.planes.add(plane);
}
case "2" -> transportCompany.showVehicles();
case "3" -> {
System.out.print("Enter the ID of the plane you want to fly: ");
String id = scanner.next();
Plane plane = null;
for (Plane p : transportCompany.planes) {
if (p.getId().equalsIgnoreCase(id)) {
plane = p;
break;
}
}
if (plane == null) {
System.out.println("The ID you entered cannot be found!");
break;
}
System.out.print("Enter the number of people onboard: ");
int people = -1;
boolean valid = false;
while (!valid) {
try {
people = scanner.nextInt();
} catch (InputMismatchException e) {
scanner.next();
System.out.println("The number you entered is in a wrong format, retry.");
continue;
}
valid = true;
}
if (people > plane.capacity()) {
System.out.println("The plane cannot fit that much people!");
break;
}
System.out.print("Enter the departure place of the plane: ");
String departure = scanner.next();
System.out.print("Enter the destination place of the plane: ");
String destination = scanner.next();
plane.transport(people, departure, destination);
transportCompany.spend(plane.getCost());
transportCompany.gain(plane.getTicket() * people);
}
case "4" -> System.out.printf("The company currently has %d $\n", transportCompany.getBalance());
case "5" -> running = false;
default -> System.out.println("Invalid choice!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment