终于写完了,真是坎坷呀
Car.java
package com.test;
/**
*
* @param price汽车总类
*/
public class Car {
private String name; //车名
private int id; //汽车id
private int price; //租车价格
public Car(int id,String name,int price) {
super();
this.id = id;
this.name =name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
PCar.java
package com.test;
/**
*
* @param price皮卡类
*/
public class PCar extends Car{
private int snum; //载货量
private int pnum; //载人数
public PCar(int id, String name, int price,int snum, int pnum) {
super(id, name, price);
// TODO Auto-generated constructor stub
this.pnum = pnum;
this.snum = snum;
}
public int getSnum() {
return snum;
}
public void setSnum(int snum) {
this.snum = snum;
}
public int getPnum() {
return pnum;
}
public void setPnum(int pnum) {
this.pnum = pnum;
}
}
PeopleCar.java
package com.test;
/**
*
* @param price载人汽车类
*/
public class PeopleCar extends Car {
private int proplenum; //载人数
public PeopleCar(int id, String name, int price,int peoplenum) {
super(id, name, price);
// TODO Auto-generated constructor stub
this.proplenum = peoplenum;
}
public int getProplenum() {
return proplenum;
}
public void setProplenum(int proplenum) {
this.proplenum = proplenum;
}
}
SCar.java
package com.test;
/**
*
* @param price货车类
*/
public class SCar extends Car {
private int snum; //载货量
public SCar(int id, String name, int price ,int snum) {
super(id, name, price);
// TODO Auto-generated constructor stub
this.snum = snum;
}
public int getSnum() {
return snum;
}
public void setSnum(int snum) {
this.snum = snum;
}
}
Test.java
package com.test;
/**
*
* @param price测试类
*/
import java.util.Scanner;
import com.test.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int peoplesum=0;
int goodsum=0;
int pricesum=0;
String carname="";
PeopleCar pc1 = new PeopleCar(1,"奥迪A4",500,4);
PeopleCar pc2 = new PeopleCar(2,"马自达6",400,4);
PCar gpc = new PCar(3,"皮卡雪6",450,2,4);
PeopleCar pc3 = new PeopleCar(4,"金龙",800,20);
SCar gc1 = new SCar(5,"松花江",400,4);
SCar gc2 = new SCar(6,"依维柯",1000,20);
Scanner sc = new Scanner(System.in); //获取控制台输入
boolean keep =true; //保持while循环运行
System.out.println("*************欢迎使用哒哒租车系统***********");
while(keep) {
System.out.println("是否租用汽车1/0:");
String Q = sc.next();
if("0".equals(Q)){
System.out.println("*********系统已退出*********");
break;
}
else if("1".equals(Q)) {
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");
System.out.print("输入你租用车的数量:");
int carnum = sc.nextInt();
System.out.print("请输入你租用车的天数:");
int day =sc.nextInt();
for(int i=0;i<carnum;){
System.out.print("输入你租用第" + (i+1) + "辆车的id:");
int id = sc.nextInt();
switch(id) {
case 1 :
carname +=pc1.getName()+" ";
peoplesum += pc1.getProplenum();
pricesum += pc1.getPrice()*day;
i++;
break;
case 2 :
carname +=pc2.getName()+" ";
peoplesum += pc2.getProplenum();
pricesum += pc2.getPrice()*day;
i++;
break;
case 3 :
carname +=gpc.getName()+" ";
peoplesum += gpc.getPnum();
goodsum += gpc.getSnum();
pricesum += gpc.getPrice()*day;
i++;
break;
case 4 :
carname +=pc3.getName()+" ";
goodsum += pc3.getProplenum();
pricesum += pc3.getPrice()*day;
i++;
break;
case 5 :
carname +=gc1.getName()+" ";
peoplesum += gc1.getSnum();
pricesum += gc1.getPrice()*day;
i++;
break;
case 6 :
carname +=gc2.getName()+" ";
peoplesum += gc2.getSnum();
pricesum += gc2.getPrice()*day;
i++;
break;
default:
System.out.println("您输入的车辆id有误,请重新输入");
}
}
}
else
System.out.println("您的输入有误,请重新输入!");
System.out.println("选择的车有:"+carname );
System.out.println("租金:"+pricesum);
System.out.println("载人量:"+peoplesum+"人");
System.out.println("载货量:"+goodsum+"吨");
System.out.print("返回租车系统首页.");
try { //系统等待500毫秒
Thread.currentThread().sleep(500);
System.out.print(".");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
System.out.println(".");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.currentThread().sleep(2000);
System.out.println("*************欢迎使用哒哒租车系统***********");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}