import java.io.*;
import java.util.*;
/**
* Add cars to an electronic parking lot.
*
* @author Michael
* @version 1.0.0
*/
public class ParkingLotApplication {
private static BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out, true);
private static PrintWriter stdErr = new PrintWriter(System.err, true);
private ParkingLot parkingLot;
private static final int MIN_NUMBER = 0;
private static final int MAX_NUMBER = 50;
private static final double MIN_COST = 2.0;
private static final double MAX_COST = 50.0;
/**
* construct an instance of a ParkingLotApplication
*
* @param args
* not used
* @throws IOException
* if error reading from standard input
*/
public static void main(String[] args) throws IOException {
ParkingLotApplication application = new ParkingLotApplication();
application.run();
}
private void run() throws IOException {
parkingLot = new ParkingLot();
int choice = getChoice();
while (choice != 0) {
if (choice == 1) {
parkingLot.addCar(readCar());
} else if (choice == 2) {
stdOut.println(parkingLot.toString());
} else if (choice == 3) {
stdOut.println("Total Cost: " + parkingLot.getTotalCost());
}
choice = getChoice();
}
}
private int getChoice() throws IOException {
do {
int input;
try {
stdErr.println();
stdErr.print("[0] Quit\n" + "[1] Add Car\n"
+ "[2] Display cars\n" + "[3] Display total cost\n"
+ "choice>");
stdErr.flush();
input = Integer.parseInt(stdIn.readLine());
if (0 <= input && 3 >= input) {
return input;
} else {
stdErr.println("Invalid choice: " + input);
}
} catch (NumberFormatException nfe) {
stdErr.println(nfe);
}
} while (true);
}
private Car readCar() throws IOException {
/* PLACE YOUR CODE HERE */
String owner = "";
String brand = "";
int number = 0;
double cost = 0.0;
do {
try {
stdErr.print("Car [onwer_plate_no_cost]>");
stdErr.flush();
StringTokenizer st = new StringTokenizer(stdIn.readLine(), "_");
if (st.countTokens() != 4) {
stdErr.println("Invalid input line");
continue;
} else {
owner = st.nextToken();
brand = st.nextToken();
number = Integer.parseInt(st.nextToken());
cost = Double.parseDouble(st.nextToken());
if ((number <= 0) || (cost <= 0)) {
stdErr.println("Number is out-of-range");
continue;
}
}
} catch (NumberFormatException nfe) {
stdErr.println(nfe);
}
return new Car(owner, brand, number, cost);
} while (true);
}
}
ssd3 exam1
需积分: 9 107 浏览量
2009-01-15
14:36:35
上传
评论
收藏 48KB RAR 举报
chinesesoftwaredn
- 粉丝: 2
- 资源: 21