一开始毫无思路,程序copy自http://www.imooc.com/article/20396 ,按照自己设想完善后如下(感觉代码可以精简好多,欢迎指正,顺便。。。。有没有小伙伴一起学习进步啊)
代码结构:
public abstract class CarStore//汽车虚拟类
public interface CarryingGoods//载货接口
public interface CarryingPeople//载客接口
public class Car extends CarStore implements CarryingPeople//客车类
public class PickUp extends CarStore implements CarryingGoods, CarryingPeople//皮卡类
public class Van extends CarStore implements CarryingGoods// 货车类
public class RentSystem//租车系统类
public static void main(String[] args)//主函数
package com.rentcar.system;
public abstract class CarStore {
private int id;
private String name;
private double price;//租金
private int rentNum;//租车数量
private int rentDate;//租车时间
public int getRentNum() {
return rentNum;
}
public void setRentNum(int rentNum) {
this.rentNum = rentNum;
}
public int getRentDate() {
return rentDate;
}
public void setRentDate(int rentDate) {
this.rentDate = rentDate;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.rentcar.system;
public interface CarryingGoods {
public void setGoods(int n);
public int getGoods();
}
package com.rentcar.system;
public interface CarryingPeople {
public void setPeople(int n);
public int getPeople();
}
package com.rentcar.system;
public class Car extends CarStore implements CarryingPeople {
private int people; // 初始化值
public Car(int id, String name, double price, int people,int rentNum,int rentDate) {//加入租期、租车数量的构造函数
super.setId(id);
super.setName(name);
super.setPrice(price);
super.setRentNum(rentNum);
super.setRentDate(rentDate);
this.setPeople(people);
}
@Override
public void setPeople(int people) {
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public String toString() {
return "\t" + super.getId() + "\t" + super.getName() + "\t"
+ super.getPrice() + "元/天\t"
+ this.getPeople() + "人"+"\t"+getRentNum()+"\t" + getRentDate();
}
}
package com.rentcar.system;
public class PickUp extends CarStore implements CarryingGoods, CarryingPeople {
private int goods;
private int people; // 初始化值
public PickUp(int id, String name, double price, int people, int goods,int rentNum,int rentDate) {
super.setId(id);
super.setName(name);
super.setPrice(price);
super.setRentNum(rentNum);
super.setRentDate(rentDate);
this.setPeople(people);
this.setGoods(goods);
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public int getGoods() {
return goods;
}
@Override
public void setPeople(int people) {
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public String toString() {
return "\t"+ super.getId() + "\t" + super.getName() + "\t"
+ super.getPrice() + "元/天\t"
+ this.getPeople() + "人" +" "+ this.getGoods() + "T"+"\t"+getRentNum()+"\t" + getRentDate();
}
}
package com.rentcar.system;
public class Van extends CarStore implements CarryingGoods {
private int goods; // 初始化值
public Van(int id, String name, double price, int goods,int rentNum,int rentDate) {
super.setId(id);
super.setName(name);
super.setPrice(price);
super.setRentNum(rentNum);
super.setRentDate(rentDate);
this.setGoods(goods);
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public int getGoods() {
return goods;
}
@Override
public String toString() {
return "\t"+ super.getId() + "\t" + super.getName() + "\t"
+ super.getPrice() + "元/天\t" + this.getGoods()
+ "T"+"\t"+getRentNum()+"\t" + getRentDate();
}
}
package com.rentcar.system;
import java.util.Scanner;
public class RentSystem {
CarStore[] cars = { new Car(1, "奥迪A4", 500, 4, 0, 0),
new Car(2, "马自达", 400, 4, 0, 0),
new PickUp(3, "皮卡雪", 450, 4, 20, 0, 0),
new Car(4, "保时捷", 800, 20, 0, 0), new Van(5, "松花江", 400, 4, 0, 0),
new Van(6, "依维柯", 1000, 20, 0, 0) };
Scanner in = new Scanner(System.in);
public void rent() {
System.out.println("欢迎使用租车系统: \n是否租车??? 请输入:“是”或“否”");
String input = in.nextLine();
if (input.equals("是")) {
System.out.println("您可租车的类型和价目表: " + "\n =======序号" + "======"
+ "车名" + "========" + "租金" + "==========" + "容量" + "====="
+ "租车数" + "====" + "租期");
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
} else if (input.equals("否")) {
System.out.println("欢迎下次光临");
System.exit(0);
} else {
System.out.println("ERROR INPUT,SYATEM EXIT");
System.exit(0);
}
// 获取用户租车信息
System.out.println("===================================================");
System.out.println("请依次输入您要租每种车的数量 (不组请输入0)");
for (int i = 0; i < cars.length; i++) {
cars[i].setRentNum(in.nextInt());
}
System.out.println("===================================================");
System.out.println("请依次输入您要租每种车的租期 (不组请输入0)");
for (int i = 0; i < cars.length; i++) {
cars[i].setRentDate(in.nextInt());
}
// 计算订单
double sum = 0;
for (int i = 0; i < cars.length; i++) {
if (cars[i].getRentNum() != 0) {
sum += cars[i].getRentNum() * cars[i].getPrice()
* cars[i].getRentDate();
}
}
// 输出订单
System.out.println("您的账单是:");
System.out.println("========序号" + "======" + "车名" + "========" + "租金"
+ "==========" + "容量" + "====" + "租车数量" + "====" + "租期");
for (int i = 0; i < cars.length; i++) {
if (cars[i].getRentNum() != 0) {
System.out.println(cars[i]);
}
}
System.out.println("订单总计:" + sum + "元");
System.out.println("欢迎下次光临");
}
//租车系统练手项目
public static void main(String[] args) {
RentSystem rs = new RentSystem();
rs.rent();
System.exit(0);
}
}
热门评论
我想定制开发听书APP,不知道自己雇人开发好还是找外包公司好? 找外包公司的话怎么防止被坑?后期APP维护,运行需要那方面的人才?
我想定制开发听书APP,不知道自己雇人开发好还是找外包公司好? 找外包公司的话怎么防止被坑?后期APP维护,运行需要那方面的人才?
System.out.println(cars[i]);输不出来吧