```//父类
package com.buaa.car;
public class Car {
protected int id;
protected String name;
protected int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "车名:"+name+",租金:"+price+"元";
}
}
子类
``package com.buaa.car;
import com.buaa.car.impl.Zaihuo;
import com.buaa.car.impl.Zaike;
public class PickUp extends Car implements Zaike,Zaihuo{
public PickUp() {
name = "F150";
price = 5000;
}
@Override
public double cargo() {
int weight = 3;
return weight;
}
@Override
public int passengers() {
int num = 5;
return num;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString()+"载重:"+cargo()+"吨,载客:"+passengers()+"人";
}
}
package com.buaa.car;
import com.buaa.car.impl.Zaike;
public class Bus extends Car implements Zaike{
public Bus(){
name = "宇通";
price = 6000;
}
@Override
public int passengers() {
int num = 45 ;
return num;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString()+"载客量:"+passengers()+"人";
}
}
```输入代码
package com.buaa.car;
import com.buaa.car.impl.Zaihuo;
public class Truck extends Car implements Zaihuo{
public Truck(){
name = "擎天柱";
price = 8000;
}
@Override
public double cargo() {
int weight = 25;
return weight;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString()+"载重:"+cargo()+"吨";
}
}
业务逻辑
``package com.buaa.main;
import java.util.Scanner;
import com.buaa.car.Bus;
import com.buaa.car.Car;
import com.buaa.car.PickUp;
import com.buaa.car.Truck;
public class Main {
static int totalPrice = 0;
static int totalCargo = 0;
static int totalPass = 0;
static Scanner input1;
static Scanner input2;
static Car[] c;
public static void main(String[] args) {
// 欢迎页
System.out.println("选择租车类型");
c = new Car[] { new Bus(), new Truck(), new PickUp() };
int count = 0;
// 遍历所有的车
for (Car List : c) {
count++;
System.out.println(count + ":" + List.toString());
}
System.out.print("请输入选择车型: ");
input1 = new Scanner(System.in);
int type = input1.nextInt();
if (type > 0 && type <= c.length) {
choose(type);
} else {
System.out.println("请输入正确的编号!");
main(args);
}
}
public static void choose(int type) {
System.out.print("请输入租赁数量: ");
input2 = new Scanner(System.in);
int num = input2.nextInt();
if (type == 1) {
Bus b = new Bus();
totalPrice = (int) b.getPrice() * num;
totalPass = b.passengers() * num;
System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载客" + totalPass + "人");
}
if (type == 2) {
Truck t = new Truck();
totalPrice = (int) t.getPrice() * num;
totalCargo = (int) t.cargo() * num;
System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨");
}
if (type == 3) {
PickUp p = new PickUp();
totalPrice = (int) p.getPrice() * num;
totalCargo = (int) p.cargo() * num;
totalPass = p.passengers() * num;
System.out.println("租用大巴" + num + "辆,共" + totalPrice + "元,可载重" + totalCargo + "吨,可载客" + totalPass + "人");
}
}
}
热门评论
看不懂(#-.-)