新鮮出爐,請多多指教
package?com.sun.steven.bean; public?class?Car?{ ????private?final?String?name; ????private?final?double?rentFee; ????private?final?int?personNum; ????private?final?int?goodsNum; ????public?Car(String?name,?double?rentFee,?int?personNum,?int?goodsNum)?{ ????????this.name?=?name; ????????this.rentFee?=?rentFee; ????????this.personNum?=?personNum; ????????this.goodsNum?=?goodsNum; ????} ????public?String?getName()?{ ????????return?name; ????} ????public?double?getRentFee()?{ ????????return?rentFee; ????} ????public?int?getPersonNum()?{ ????????return?personNum; ????} ????public?int?getGoodsNum()?{ ????????return?goodsNum; ????} }
package com.sun.steven.bean;
public class PersonCar extends Car {
??? public PersonCar(String name, double rentFee, int personNum, int goodsNum) {
??????? super(name, rentFee, personNum, goodsNum);
??? }
??? public PersonCar(String name, double rentFee, int personNum) {
??????? super(name, rentFee, personNum, 0);
??? }
??? @Override
??? public String toString() {
??????? return getName() + "\t" + getRentFee() + "元/天\t載人:" + getPersonNum() + "人";
??? }
}
package com.sun.steven.bean;
public class GoodsCar extends Car {
??? public GoodsCar(String name, double rentFee, int personNum, int goodsNum) {
??????? super(name, rentFee, personNum, goodsNum);
??? }
??? public GoodsCar(String name, double rentFee, int goodsNum) {
??????? super(name, rentFee, 0, goodsNum);
??? }
??? @Override
??? public String toString() {
??????? return this.getName() + "\t" + this.getRentFee() + "元/天\t載貨:" + this.getGoodsNum() + "噸";
??? }
}
package com.sun.steven.bean;
public class PersonGoodsCar extends Car {
??? public PersonGoodsCar(String name, int rentFee, int personNum, int goodsNum) {
??????? super(name, rentFee, personNum, goodsNum);
??? }
??? @Override
??? public String toString() {
??????? return getName() + "\t" + getRentFee() + "元/天\t載人" + getPersonNum() + "人載貨:" + getGoodsNum() + "噸";
??? }
}
package com.sun.steven.utils;
public class BaseCheckUtils {
??? /**
???? * 校驗(yàn)一個參數(shù)是否為整數(shù)
???? *
???? * @param in
???? * @return
???? */
??? public static int string2Integer(String in) {
??????? try {
??????????? return Integer.valueOf(in);
??????? }
??????? catch (Exception e) {
??????????? throw new IllegalArgumentException(in + "不是一個整數(shù)");
??????? }
??? }
??? /**
???? * 校驗(yàn)一個整數(shù)是否為正數(shù)
???? *
???? * @param in
???? */
??? public static void isPostiveInteger(int in) {
??????? isRangeInteger(in, 1, Integer.MAX_VALUE);
??? }
??? /**
???? * 判斷輸入是否在某個范圍之內(nèi),含邊界
???? *
???? * @param in
???? * @param min
???? * @param max
???? */
??? public static void isRangeInteger(int in, int min, int max) {
??????? if (in < min || in > max) {
??????????? throw new IllegalArgumentException("輸入" + in + "不在范圍[" + min + "," + max + "]之內(nèi)");
??????? }
??? }
}
package com.sun.steven.menu;
import java.util.Scanner;
import com.sun.steven.bean.Car;
import com.sun.steven.bean.GoodsCar;
import com.sun.steven.bean.PersonCar;
import com.sun.steven.bean.PersonGoodsCar;
import com.sun.steven.utils.BaseCheckUtils;
public class Menu {
??? public static void main(String[] str) {
??????? System.out.println("歡迎使用答答租車系統(tǒng):");
??????? System.out.println("您是否要租車:1是 0否");
??????? Scanner sca = new Scanner(System.in);
??????? int rentFlag = BaseCheckUtils.string2Integer(sca.next());
??????? BaseCheckUtils.isRangeInteger(rentFlag, 0, 1);
??????? if (0 == rentFlag) {
??????????? System.out.println("歡迎下次光臨!");
??????????? System.exit(0);
??????? }
??????? System.out.println("您可租車的類型及其價格表:");
??????? System.out.println("序號\t汽車名稱\t租金\t\t容量");
??????? Car[] cars = { new PersonCar("奧迪A4", 500, 4), new PersonCar("馬自達(dá)6", 400, 4),
??????????????? new PersonGoodsCar("皮卡雪6", 450, 4, 2), new PersonCar("金龍", 800, 20),
??????????????? new GoodsCar("松花江", 400, 2), new GoodsCar("依維柯", 1000, 20) };
??????? for (int i = 0; i < cars.length; i++) {
??????????? System.out.println((i + 1) + ".\t" + cars[i]);
??????? }
??????? System.out.println("請輸入您要租汽車的數(shù)量:");
??????? int rentCarNum = BaseCheckUtils.string2Integer(sca.next());
??????? BaseCheckUtils.isPostiveInteger(rentCarNum);
??????? Car[] rentCars = new Car[rentCarNum];
??????? for (int i = 0; i < rentCarNum; i++) {
??????????? System.out.println("請輸入第" + (i + 1) + "輛 車的序號:");
??????????? int carNo = BaseCheckUtils.string2Integer(sca.next());
??????????? BaseCheckUtils.isRangeInteger(carNo - 1, 0, cars.length - 1);
??????????? rentCars[i] = cars[carNo - 1];
??????? }
??????? System.out.println("請輸入租車天數(shù):");
??????? int rentCarDays = BaseCheckUtils.string2Integer(sca.next());
??????? BaseCheckUtils.isPostiveInteger(rentCarDays);
??????? // 計(jì)算租金和載客載貨量
??????? System.out.println("您的賬單:");
??????? String personCarInfo = "";
??????? String goodsCarInfo = "";
??????? int totalPerson, totalGood;
??????? double totalFee = 0.;
??????? totalPerson = totalGood = 0;
??????? for (int i = 0; i < rentCars.length; i++) {
??????????? if (!(rentCars[i] instanceof GoodsCar)) {
??????????????? personCarInfo += rentCars[i].getName() + " ";
??????????????? totalPerson += rentCars[i].getPersonNum();
??????????? }
??????????? if (!(rentCars[i] instanceof PersonCar)) {
??????????????? goodsCarInfo += rentCars[i].getName() + " ";
??????????????? totalGood += rentCars[i].getGoodsNum();
??????????? }
??????????? totalFee += rentCars[i].getRentFee();
??????? }
??????? System.out.println("***可載人的車有:" + personCarInfo + "\t共載人:" + totalPerson * rentCarDays + "人");
??????? System.out.println("***載貨的車有:" + goodsCarInfo + "\t共載貨:" + totalGood * rentCarDays + "噸");
??????? System.out.println("***租車總價格:" + totalFee * rentCarDays + "元");
??? }
}