Ski AreaWrite an application to manage ski lifts and ski runs in a ski resortClasses are located in the itpolitoski package; the main class is SkiArea The TestExample class in the example package show
滑雪区管理应用程序
R1 - 缆车类型 通过SkiArea类与系统进行交互,可以通过提供度假村的名称来实例化该类。可以使用getter getName()来获取名称。
可以通过liftType()方法定义新的缆车类型,该方法接受代码、类别(例如拖纱绳缆车、索道、缆车)和每个单元的座位数。如果代码重复,该方法会抛出异常。
getCategory()和getCapacity()方法接受一个类型并返回类别和座位数。如果提供的类型未定义,则抛出异常。
可以通过types()方法获取所有已定义的缆车类型的列表,该方法返回一个类型名称的集合。
R2 - 缆车 通过createLift()方法定义新的缆车,该方法接受名称和缆车类型。如果提供的类型未定义,则抛出异常。
getType()方法返回给定缆车的类型。getLifts()方法按名称返回缆车的集合。
R3 - 滑雪道 createSlope()方法允许描述新的滑雪道,它接收滑雪道的名称、难度("green"、"blue"、"red"、"black")和滑雪道起点的缆车名称。如果不知道缆车的名称,则抛出异常。
getDifficulty()和getStartLift()方法接受滑雪道的名称,并分别返回难度和起点缆车的名称。
getSlopes()方法返回所有滑雪道的集合。
另外,getSlopesFrom()方法接受一个缆车的名称,并提供从该缆车起点开始的所有滑雪道的列表。
R4 - 停车场 createParking()方法允许描述一个停车场,它接受停车场的名称和停车位的数量。
getParkingSlots()方法接受一个停车场的名称,并返回该停车场可用的停车位数量。
可以使用liftServedByParking()方法指示一个缆车从一个停车场出发,该方法接受缆车的名称和停车场的名称。可以多次调用该方法以添加所有从同一停车场出发的缆车。
servedLifts()方法接受一个停车场的名称,并返回所有由该停车场提供服务的缆车的集合。
isParkingProportionate()方法检查停车场的大小是否与从该停车场出发的缆车的容量成比例,特别是如果停车场大小除以从停车场出发的缆车的座位数之和小于30,则该方法返回true。
R5 - 文件输入 readLifts()方法从文本文件中读取缆车类型和缆车的描述。文件应按行组织,每行以表示信息类型的字母开头:"T"表示缆车类型,而"L"表示缆车。缆车类型由代码、类别和座位数描述。缆车由名称和类型代码描述。行中的不同数据由";"分隔,分隔符周围的可能的空格将被忽略。
示例:
T ; S4P; seggiovia; 4 T;S;skilift;1 L;Fraiteve;S4P L;Baby;S 该方法必须传播可能的IO异常,并且必须能够跳过不符合格式要求(行上的数据数量错误)的行,并继续读取下一行。
以下是对应的Java代码:
package it.polito.ski;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SkiArea {
private String name;
private Map<String, LiftType> liftTypes;
private List<Lift> lifts;
private List<SkiSlope> slopes;
private List<Parking> parkings;
public SkiArea(String name) {
this.name = name;
this.liftTypes = new HashMap<>();
this.lifts = new ArrayList<>();
this.slopes = new ArrayList<>();
this.parkings = new ArrayList<>();
}
public String getName() {
return name;
}
public void liftType(String code, String category, int capacity) throws InvalidLiftException {
if (liftTypes.containsKey(code)) {
throw new InvalidLiftException("Duplicate lift type code");
}
LiftType liftType = new LiftType(code, category, capacity);
liftTypes.put(code, liftType);
}
public String getCategory(String type) throws InvalidLiftException {
if (!liftTypes.containsKey(type)) {
throw new InvalidLiftException("Lift type not defined");
}
LiftType liftType = liftTypes.get(type);
return liftType.getCategory();
}
public int getCapacity(String type) throws InvalidLiftException {
if (!liftTypes.containsKey(type)) {
throw new InvalidLiftException("Lift type not defined");
}
LiftType liftType = liftTypes.get(type);
return liftType.getCapacity();
}
public List<String> types() {
return new ArrayList<>(liftTypes.keySet());
}
public void createLift(String name, String type) throws InvalidLiftException {
if (!liftTypes.containsKey(type)) {
throw new InvalidLiftException("Lift type not defined");
}
LiftType liftType = liftTypes.get(type);
Lift lift = new Lift(name, liftType);
lifts.add(lift);
}
public String getType(String liftName) {
for (Lift lift : lifts) {
if (lift.getName().equals(liftName)) {
return lift.getType().getCode();
}
}
return null;
}
public List<Lift> getLifts() {
return new ArrayList<>(lifts);
}
public void createSlope(String name, String difficulty, String startLift) throws InvalidLiftException {
Lift startLiftObj = null;
for (Lift lift : lifts) {
if (lift.getName().equals(startLift)) {
startLiftObj = lift;
break;
}
}
if (startLiftObj == null) {
throw new InvalidLiftException("Start lift not found");
}
SkiSlope slope = new SkiSlope(name, difficulty, startLiftObj);
slopes.add(slope);
}
public String getDifficulty(String slopeName) {
for (SkiSlope slope : slopes) {
if (slope.getName().equals(slopeName)) {
return slope.getDifficulty();
}
}
return null;
}
public String getStartLift(String slopeName) {
for (SkiSlope slope : slopes) {
if (slope.getName().equals(slopeName)) {
return slope.getStartLift().getName();
}
}
return null;
}
public List<SkiSlope> getSlopes() {
return new ArrayList<>(slopes);
}
public List<SkiSlope> getSlopesFrom(String liftName) {
List<SkiSlope> result = new ArrayList<>();
for (SkiSlope slope : slopes) {
if (slope.getStartLift().getName().equals(liftName)) {
result.add(slope);
}
}
return result;
}
public void createParking(String name, int slots) {
Parking parking = new Parking(name, slots);
parkings.add(parking);
}
public int getParkingSlots(String parkingName) {
for (Parking parking : parkings) {
if (parking.getName().equals(parkingName)) {
return parking.getSlots();
}
}
return -1;
}
public void liftServedByParking(String liftName, String parkingName) {
Lift lift = null;
for (Lift l : lifts) {
if (l.getName().equals(liftName)) {
lift = l;
break;
}
}
Parking parking = null;
for (Parking p : parkings) {
if (p.getName().equals(parkingName)) {
parking = p;
break;
}
}
if (lift != null && parking != null) {
parking.addServedLift(lift);
}
}
public List<Lift> servedLifts(String parkingName) {
for (Parking parking : parkings) {
if (parking.getName().equals(parkingName)) {
return parking.getServedLifts();
}
}
return new ArrayList<>();
}
public boolean isParkingProportionate(String parkingName) {
Parking parking = null;
for (Parking p : parkings) {
if (p.getName().equals(parkingName)) {
parking = p;
break;
}
}
if (parking == null) {
return false;
}
int parkingSize = parking.getSlots();
int totalCapacity = 0;
for (Lift lift : parking.getServedLifts()) {
totalCapacity += lift.getType().getCapacity();
}
return parkingSize / totalCapacity < 30;
}
public void readLifts(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
if (parts.length < 2) {
continue;
}
String type = parts[0].trim();
if (type.equals("T")) {
if (parts.length != 4) {
continue;
}
String code = parts[1].trim();
String category = parts[2].trim();
int capacity = Integer.parseInt(parts[3].trim());
try {
liftType(code, category, capacity);
} catch (InvalidLiftException e) {
continue;
}
} else if (type.equals("L")) {
if (parts.length != 2) {
continue;
}
String name = parts[1].trim();
String liftType = getType(name);
if (liftType != null) {
try {
createLift(name, liftType);
} catch (InvalidLiftException e) {
continue;
}
}
}
}
reader.close();
}
}
class LiftType {
private String code;
private String category;
private int capacity;
public LiftType(String code, String category, int capacity) {
this.code = code;
this.category = category;
this.capacity = capacity;
}
public String getCode() {
return code;
}
public String getCategory() {
return category;
}
public int getCapacity() {
return capacity;
}
}
class Lift {
private String name;
private LiftType type;
public Lift(String name, LiftType type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public LiftType getType() {
return type;
}
}
class SkiSlope {
private String name;
private String difficulty;
private Lift startLift;
public SkiSlope(String name, String difficulty, Lift startLift) {
this.name = name;
this.difficulty = difficulty;
this.startLift = startLift;
}
public String getName() {
return name;
}
public String getDifficulty() {
return difficulty;
}
public Lift getStartLift() {
return startLift;
}
}
class Parking {
private String name;
private int slots;
private List<Lift> servedLifts;
public Parking(String name, int slots) {
this.name = name;
this.slots = slots;
this.servedLifts = new ArrayList<>();
}
public String getName() {
return name;
}
public int getSlots() {
return slots;
}
public void addServedLift(Lift lift) {
servedLifts.add(lift);
}
public List<Lift> getServedLifts() {
return new ArrayList<>(servedLifts);
}
}
``
原文地址: https://www.cveoy.top/t/topic/i0aA 著作权归作者所有。请勿转载和采集!