java课程2 写一个租车系统
理解成了全部变成交互。。于是还处理了输入异常等等情况
进入:
package com.imooc;
public class Initial {
public static void main(String[] args) {
System.out.println("Welcome to rent car system");
User user = new User();
user.chooseV();
}
}
User 类
package com.imooc;
import java.util.Scanner;
public class User {
private String name;
private Vehicle ve;
public User() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter user name: ");
name = input.next();
}
public void chooseV() {
Scanner input = new Scanner(System.in);
System.out.println("Hi " + name + ", please enter the class of vehicle you want to rent");
System.out.println("1. Truck - for goods only");
System.out.println("2. Car - for people only");
System.out.println("3. Van - for both goods and people");
System.out.println("Other number to exit");
int label = 0;
try {
label = input.nextInt();
} catch (Exception e) {
System.out.println("!!!!Input error!!!!");
chooseV();
return;
}
if (label == 1) {
ve = new Truck();
} else if (label == 2) {
ve = new Car();
} else if (label == 3) {
ve = new Van();
} else {
System.out.println("~~~~Bye~~~~");
return;
}
rent();
}
public void rent() {
Scanner input = new Scanner(System.in);
System.out.println("Ability to load people: " + ve.getLoadPeopleAbility());
System.out.println("Ability to load goods: " + ve.getLoadGoodsAbility());
System.out.println("-----------------------------");
if (ve instanceof Truck) {
System.out.println("Please enter the weight of goods to load: ");
float weight = 0f;
try {
weight = input.nextFloat();
} catch (Exception e) {
System.out.println("!!!!Input error!!!!");
rent();
return;
}
if (weight <= 5.0f) {
ve = new LightTruck();
} else {
ve = new HeavyTruck();
}
} else if (ve instanceof Car) {
System.out.println("Please enter the number of people to load: ");
int people = 0;
try {
people = input.nextInt();
} catch (Exception e) {
System.out.println("!!!!Input error!!!!");
rent();
return;
}
if (people >= 1 && people <= 5) {
ve = new LightTruck();
} else if (people >= 10) {
ve = new HeavyTruck();
} else if (people < 10 && people > 5) {
System.out.println("!!!!You should choose a van!!!!");
chooseV();
return;
} else {
System.out.println("!!!!Wrong number of people!!!!");
rent();
return;
}
}
System.out.println("Please enter the number of days to rent: ");
int day = input.nextInt();
ve.rent(day);
}
}
Vehicle abstract class
package com.imooc;
public abstract class Vehicle {
public abstract float getPrice();
public abstract String getName();
public abstract boolean getLoadPeopleAbility();
public abstract boolean getLoadGoodsAbility();
public abstract void rent(int day);
}
五种车型, truck,car,van继承vehicle,其中truck和car又分两种型号
package com.imooc;
public class Truck extends Vehicle {
@Override
public boolean getLoadPeopleAbility() {
return false;
}
@Override
public boolean getLoadGoodsAbility() {
return true;
}
@Override
public float getPrice() {
return 0f;
}
@Override
public String getName() {
return "Truck";
}
public void rent(int day) {}
}
package com.imooc;
public class LightTruck extends Truck {
private float loadGoodsMin = 0f;
private float loadGoodsMax = 5.0f;
private String name = "Light truck";
private float price = 200f;
public float getLoadGoodsMax() {
return loadGoodsMax;
}
public float getLoadGoodsMin() {
return loadGoodsMin;
}
@Override
public float getPrice() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public void rent(int day) {
float money = price * day;
System.out.println("You rent a " + name +
" for " + day + " days, needing RMB: " + money);
}
}
package com.imooc;
public class HeavyTruck extends Truck {
private float loadGoodsMin = 5.0f;
private float loadGoodsMax = 20.0f;
private String name = "Heavy truck";
private float price = 500f;
public float getLoadGoodsMax() {
return loadGoodsMax;
}
public float getLoadGoodsMin() {
return loadGoodsMin;
}
@Override
public float getPrice() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public void rent(int day) {
float money = price * day;
System.out.println("You rent a " + name +
" for " + day + " days, needing RMB: " + money);
}
}
package com.imooc;
public class Car extends Vehicle {
@Override
public float getPrice() {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public boolean getLoadPeopleAbility() {
return true;
}
@Override
public boolean getLoadGoodsAbility() {
return false;
}
@Override
public void rent(int day) {
}
}
package com.imooc;
public class SmallCar extends Car {
private int loadPeopleMin = 1;
private int loadPeopleMax = 5;
private float price = 80f;
private String name = "Small car";
public int getLoadPeopleMax() {
return loadPeopleMax;
}
public int getLoadPeopleMin() {
return loadPeopleMin;
}
@Override
public void rent(int day) {
float money = price * day;
System.out.println("You rent a " + name +
" for " + day + " days, needing RMB: " + money);
}
}
package com.imooc;
public class LargeCar extends Car {
private int loadPeopleMin = 10;
private int loadPeopleMax = 50;
private String name = "Large car";
private float price = 250f;
public int getLoadPeopleMax() {
return loadPeopleMax;
}
public int getLoadPeopleMin() {
return loadPeopleMin;
}
@Override
public void rent(int day) {
float money = price * day;
System.out.println("You rent a " + name +
" for " + day + " days, needing RMB: " + money);
}
}
package com.imooc;
import java.util.Scanner;
public class Van extends Vehicle {
private int people;
private float weight;
private String name = "Van";
private float price = 400f;
public Van() {
loadPeople();
}
private void loadGoods() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter weight of goods to load");
try {
weight = input.nextFloat();
} catch (Exception e) {
System.out.println("!!!!Wrong form for weight of goods!!!!");
loadGoods();
}
if (weight < 0) {
System.out.println("!!!!Wrong weight of goods!!!!");
loadGoods();
}
}
private void loadPeople() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter number of people to load: ");
try {
people = input.nextInt();
} catch (Exception e) {
System.out.println("!!!!Wrong form for number of people!!!!");
}
if (people <= 0) {
System.out.println("!!!!Wrong number of people!!!!");
loadPeople();
}
}
public float getWeight() {
return weight;
}
public int getPeople() {
return people;
}
@Override
public float getPrice() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public boolean getLoadPeopleAbility() {
return true;
}
@Override
public boolean getLoadGoodsAbility() {
return true;
}
@Override
public void rent(int day) {
float money = price * day;
System.out.println("You rent a " + name +
" for " + day + " days, needing RMB: " + money);
}
}
热门评论
那么多代码,想哭 。。。。