借鉴地址:http://www.imooc.com/qadetail/144753
//父类
package com.imooc;
public class Car {
private String name;
private int price;
private int sum;
private int zaizhong;
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 int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getZaizhong() {
return zaizhong;
}
public void setZaizhong(int zaizhong) {
this.zaizhong = zaizhong;
}
}
//子类 载人汽车类
package com.imooc;
public class bus extends Car {
public bus(String name,int price,int sum,int zaizhong){
this.setName(name);
this.setPrice(price);
this.setSum(sum);
this.setZaizhong(zaizhong);
}
public int getBusLoad() {
// TODO Auto-generated method stub
return 0;
}
}
//子类 载货车
package com.imooc;
public class truck extends Car {
public truck(String name,int price,int sum,int zaizhong){
this.setName(name);
this.setPrice(price);
this.setSum(sum);
this.setZaizhong(zaizhong);
}
}
//子类 即可载人又可载货类
package com.imooc;
public class truck extends Car {
public truck(String name,int price,int sum,int zaizhong){
this.setName(name);
this.setPrice(price);
this.setSum(sum);
this.setZaizhong(zaizhong);
}
}
主体代码
package com.imooc;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
bus car1 = new bus("奥迪A4",500,4,0);
bus car2 = new bus("马自达6",400,4,0);
pika car3= new pika("皮卡雪6",450,4,2);
bus car4 = new bus("金龙",800,20,0);
truck car5=new truck("松花江",400,0,4);
truck car6 = new truck("依维柯",1000,0,20);
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否\t");
Scanner choose=new Scanner(System.in);
int input=choose.nextInt();
if(input==1){
System.out.println("你可以租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量\t");
System.out.println(1+"\t"+car1.getName()+"\t"+car1.getPrice()+"元/天"+"\t\t"+"载人:"+car1.getSum()+"人");
System.out.println(2+"\t"+car2.getName()+"\t"+car2.getPrice()+"元/天"+"\t\t"+"载人:"+car2.getSum()+"人");
System.out.println(3+"\t"+car3.getName()+"\t"+car3.getPrice()+"元/天"+"\t\t"+"载人:"+car3.getSum()+"人"+"载货:"+car3.getZaizhong()+"吨");
System.out.println(4+"\t"+car4.getName()+"\t"+car4.getPrice()+"元/天"+"\t\t"+"载人:"+car2.getSum()+"人");
System.out.println(5+"\t"+car5.getName()+"\t"+car5.getPrice()+"元/天"+"\t\t"+"载货:"+car5.getZaizhong()+"吨");
System.out.println(6+"\t"+car6.getName()+"\t"+car6.getPrice()+"元/天"+"\t"+"载货:"+car6.getZaizhong()+"吨");
System.out.println("请输入你要租汽车的数量");
int inputNum=choose.nextInt();
String nsum1="";//初始化属性
String nsum2="";//初始化属性
String nsum3="";//初始化属性
String nsum4="";//初始化属性
String nsum5="";//初始化属性
String nsum6="";//初始化属性
int psum=0;//初始化属性
double hsum=0;//初始化属性
double jsum=0;//初始化属性
for(int i=1;i<=inputNum;i++){
System.out.println("请输入第"+i+"辆车的序号");
int num=choose.nextInt();
if(num>=1&&num<=6){
if(num==1){
nsum1=car1.getName();
jsum+=car1.getPrice();
psum+=car1.getSum();
}
if(num==2){
nsum2=car2.getName();
jsum+=car2.getPrice();
psum+=car2.getSum();
}
if(num==3){
nsum3=car3.getName();
jsum+=car3.getPrice();
psum+=car3.getSum();
hsum+=car3.getZaizhong();
}
if(num==4){
nsum4=car4.getName();
jsum+=car4.getPrice();
psum+=car4.getSum();
}
if(num==5){
nsum5=car5.getName();
jsum+=car5.getPrice();
hsum+=car5.getZaizhong();
}
if(num==6){
nsum6=car6.getName();
jsum+=car6.getPrice();
hsum+=car6.getZaizhong();
}
}
}
System.out.println("请输入租车天数:");
int day=choose.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println("\t"+nsum1+"\t"+nsum2+"\t"+nsum3+"\t"+nsum4+"\t共载:"+psum+"人");
System.out.println("***载货的车有:");
System.out.println("\t"+nsum3+"\t"+nsum5+"\t"+nsum6+"\t"+"共载货:"+hsum+"吨");
jsum=jsum*day;
System.out.println("***租车总价格:"+jsum+"元");
}
if(input==0){
System.out.println("欢迎下次再来");
}
}
}