课程作业嗒嗒租车系统 源码如下 不同看法可以交流
Car.java
package com.dada.test;
public class Car {
private int id;
private String name;
private 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;
}
public Car(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
GoodCar.java
package com.dada.test;
public class GoodCar extends Car {
private int goodcapacity;
public GoodCar(int id, String name, int price, int goodcapacity) {
super(id, name, price);
this.goodcapacity = goodcapacity;
}
public int getGoodcapacity() {
return goodcapacity;
}
public void setGoodcapacity(int goodcapacity) {
this.goodcapacity = goodcapacity;
}
}
GPCar.java
package com.dada.test;
public class GPCar extends Car {
private int goodcapacity;
private int peoplecapacity;
public GPCar(int id, String name, int price, int goodcapacity, int peoplecapacity) {
super(id, name, price);
this.goodcapacity = goodcapacity;
this.peoplecapacity = peoplecapacity;
}
public int getGoodcapacity() {
return goodcapacity;
}
public void setGoodcapacity(int goodcapacity) {
this.goodcapacity = goodcapacity;
}
public int getPeoplecapacity() {
return peoplecapacity;
}
public void setPeoplecapacity(int peoplecapacity) {
this.peoplecapacity = peoplecapacity;
}
}
PeopleCar.java
package com.dada.test;
public class PeopleCar extends Car {
private int peoplecapacity;
public PeopleCar(int id, String name, int price, int peoplecapacity) {
super(id, name, price);
this.peoplecapacity = peoplecapacity;
}
public int getPeoplecapacity() {
return peoplecapacity;
}
public void setPeoplecapacity(int peoplecapacity) {
this.peoplecapacity = peoplecapacity;
}
}
Test.java
package com.dada.test;
import java.util.Scanner;
import com.dada.test.*;
public class Test {
public static void main(String[] args) {
PeopleCar pc1 = new PeopleCar(1,"奥迪A4",500,4);
PeopleCar pc2 = new PeopleCar(2,"马自达6",400,4);
GPCar gpc = new GPCar(3,"皮卡雪6",450,2,4);
PeopleCar pc3 = new PeopleCar(4,"金龙",800,20);
GoodCar gc1 = new GoodCar(5,"松花江",400,4);
GoodCar gc2 = new GoodCar(6,"依维柯",1000,20);
Scanner sc = new Scanner(System.in);
boolean keep = true;
System.out.println("*********嗒嗒租车系统*********");
while(keep){
System.out.print("是否租用汽车1/0:");
if("0".equals(sc.next())){
System.out.println("*********系统已退出*********");
break;
}
System.out.println("您可租车的类型及价目表:");
System.out.println("1. 奥迪A4 500元/天 载人:4人\n" +"2. 马自达6 400元/天 载人:4人\n" +"3. 皮卡雪6 450元/天 载人:4人载货:2吨\n"+"4. 金龙 800元/天 载人:20人\n"+"5. 松花江 400元/天 载货:4吨\n"+"6. 依维柯 1000元/天 载货:20吨\n");
int peoplesum=0;
int goodsum=0;
int pricesum=0;
String carname="";
System.out.print("输入你租用车的数量:");
int carnum = sc.nextInt();
System.out.print("请输入你租用车的天数:");
int day =sc.nextInt();
for(int i=0;i<carnum;i++){
System.out.print("输入你租用第" + (i+1) + "俩车的id:");
int id = sc.nextInt();
if(id==1){
carname +=pc1.getName()+" ";
peoplesum += pc1.getPeoplecapacity();
pricesum += pc1.getPrice()*day;
}else if(id==2){
carname +=pc2.getName()+" ";
peoplesum += pc2.getPeoplecapacity();
pricesum += pc2.getPrice()*day;
}else if(id==3){
carname +=gpc.getName()+" ";
peoplesum += gpc.getPeoplecapacity();
goodsum += gpc.getGoodcapacity();
pricesum += gpc.getPrice()*day;
}else if(id==4){
carname +=pc3.getName()+" ";
peoplesum += pc3.getPeoplecapacity();
pricesum += pc3.getPrice()*day;
}else if(id==5){
carname +=gc1.getName()+" ";
goodsum += gc1.getGoodcapacity();
pricesum += gc1.getPrice()*day;
}else if(id==6){
carname +=gc2.getName()+" ";
goodsum += gc2.getGoodcapacity();
pricesum += gc2.getPrice()*day;
}else{
System.out.println("您输入的id有误!");
}
}
System.out.println("选择的车有:"+carname );
System.out.println("租金:"+pricesum);
System.out.println("载人量:"+peoplesum+"人");
System.out.println("载货量:"+goodsum+"吨");
}
}
}