主函数
package com.dadarent;
import java.util.Scanner;
public class Dadarent {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用答答租车");
System.out.println("是否租车,是请按1,否请按0");
//输入1或者0
Scanner s=new Scanner(System.in);
int n=s.nextInt();
//创建租车类型及其价目表数组
Car[] cars={new Keche(1,"奥迪A4",500,4),new Keche(2,"马自达6",400,4),new Pika(3,"皮卡雪 6",450,4,2),
new Keche(4,"金龙",800,20),new Huoche(5,"松花江",400,4),
new Huoche(6,"依维柯",1000,20) };
if(n==0){
System.out.println("欢迎您再次使用");
}else{
System.out.println("您可租车的类型及其价目表");
System.out.println("序号\t"+"汽车名称\t"+"租金\t"+"容量\t");//表头
//遍历数组,显示内容
for(int i=0;i<cars.length;i++){
System.out.println(cars[i].showmsg());
}
}
System.out.println("请输入要租汽车的数量:");
//获取租车数量
int n2=s.nextInt();
int[] pri=new int[n2];
int[] per=new int[n2];
int[] hei=new int[n2];
String[] type=new String[n2];
for(int i=1;i<=n2;i++){
System.out.println("请输入第"+i+"量车的序号:");
//获取租车序号
int n3=s.nextInt();
//得到价格,载人,载货,类型数组
pri[i-1]=cars[n3-1].GetPrice();
per[i-1]=cars[n3-1].GetPeople();
type[i-1]=cars[n3-1].GetType();
hei[i-1]=cars[n3-1].GetHeight();
}
System.out.println("请输入租车天数:");
int n4=s.nextInt();
int day=n4;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for(int i=1;i<=n2;i++){
if(per[i-1]!=0)
System.out.print(type[i-1]+" ");
}
int sum=0;
for(int i=1;i<=n2;i++){
sum+=per[i-1];
}
System.out.println("共载:"+sum+"人");
System.out.println("***载货的车有:");
for(int i=1;i<=n2;i++){
if(hei[i-1]!=0)
System.out.println(type[i-1]+" ");
}
int sum2=0;
for(int i=1;i<=n2;i++){
sum2+=hei[i-1];
}
System.out.println("共载"+sum2+"吨");
int price_sum=0;
for(int i=1;i<=n2;i++){
price_sum+=pri[i-1];
}
System.out.println("***租车总价格:"+price_sum*day);
}
}
Car类
package com.dadarent;
public abstract class Car {
public int id;//序号
public String type;//车型
public int price;//价格
public int people;//载人数目
public int height;//载货重量
public abstract String showmsg();
public abstract int GetPrice();
public abstract int GetPeople();
public abstract int GetHeight();
public abstract String GetType();
}
Keche类
package com.dadarent;
public class Keche extends Car {
public Keche(int id,String type,int price,int people){
this.id=id;
this.type=type;
this.price=price;
this.people=people;
}
public String showmsg(){
return(id+"\t"+type+"\t"+price+"元/天\t"+"载人:"+people+"人\t");
}
public int GetPrice(){
return price;
}
public int GetPeople(){
return people;
}
public String GetType(){
return type;
}
public int GetHeight(){
return height;
}
}
Huoche类
package com.dadarent;
public class Huoche extends Car {
public Huoche(int id,String type,int price,int height){
this.id=id;
this.type=type;
this.price= price;
this.height=height;
}
public String showmsg(){
return(id+"\t"+type+"\t"+price+"元/天\t"+"载货:"+height+"吨\t");
}
public int GetPrice(){
return price;
}
public int GetHeight(){
return height;
}
public String GetType(){
return type;
}
public int GetPeople(){
return people;
}
}
Pika类
package com.dadarent;
public class Pika extends Car {
public Pika(int id,String type,int price,int people,int height){
this.id=id;
this.type=type;
this.price=price;
this.people=people;
this.height=height;
}
public String showmsg(){
return(id+"\t"+type+"\t"+price+"元/天\t"+"载人:"+people+"人\t"+"载货:"+height+"吨\t");
}
public int GetPrice(){
return price;
}
public int GetPeople(){
return people;
}
public String GetType(){
return type;
}
public int GetHeight(){
return height;
}
}
热门评论
写得很好,先前还晕晕的,现在一下就明白了,赞一个.
不错不错,赞一个,不过height是高度,weight才是重量
写的很漂亮,不过直接用接口就可以,使用抽象类感觉多此一举了