父类
public class Car {
private int price;//价格
private int sum;//载人
private int again;//载物
private String name;
private int id;
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;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getAgain() {
return again;
}
public void setAgain(int again) {
this.again = again;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
子类1
public class Bus extends Car {
public Bus(int newId ,int newPrice,int newSum,int newAgain,String newName)
{
this.setAgain(newAgain);
this.setId(newId);
this.setName(newName);
this.setPrice(newPrice);
this.setSum(newSum);
}
}
子类2
public class Pick extends Car {
public Pick(int newId,int newPrice,int newSum,int newAgain,String newName)
{
this.setId(newId);
this.setAgain(newAgain);
this.setName(newName);
this.setPrice(newPrice);
this.setSum(newSum);
}
}
子类3
public class Trcuk extends Car {
public Trcuk(int newId,int newPrice,int newSum,int newAgain,String newName )
{
this.setSum(newSum);
this.setId(newId);
this.setAgain(newAgain);
this.setName(newName);
this.setPrice(newPrice);
}
}
测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("*********欢迎使用达达租车系统**********");
System.out.println("是否进行选车 1:是 2:否");
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
int inputNum=input.nextInt();
if(inputNum==1)
{
System.out.println("********以下是租车的类型和价格*******");
System.out.println("汽车ID"+"\t"+"汽车价格"+"\t"+"载人量"+"\t"+"载物量"+"\t"+"汽车名称");
Car[] carMassage ={new Bus(1,500,5,0,"奥迪A4"),new Bus(2,1000,20,0,"马自达"),new Bus(3,8000,45,0,"金龙客车"),new Trcuk(4,5000,0,1000,"松花江"),new Trcuk(5,200000,0,1000,"重型运货舱"),new Pick(6,5,20,3000,"皮卡秋")};
for(Car i:carMassage)
{
if(i instanceof Bus)
{
System.out.println(i.getId()+"\t"+i.getPrice()+"\t"+i.getSum()+"\t"+i.getAgain()+"\t"+i.getName());
}
if(i instanceof Trcuk )
{
System.out.println(i.getId()+"\t"+i.getPrice()+"\t"+i.getSum()+"\t"+i.getAgain()+"\t"+i.getName());
}
if(i instanceof Pick)
{
System.out.println(i.getId()+"\t"+i.getPrice()+"\t"+i.getSum()+"\t"+i.getAgain()+"\t"+i.getName());
}
}
System.out.println("你要选择几辆车");
int number=input.nextInt();
int pNumber=0;
int money=0;
int weight=0;
for(int i=0;i<number;i++)
{
System.out.println("请输入你要选择的第"+i+1+"量车的序号");
int choseNum=input.nextInt();
System.out.println("你选择的车型是");
if(carMassage[choseNum-1] instanceof Bus)
{
System.out.println(carMassage[choseNum-1].getId()+"\t"+"\t"+carMassage[choseNum-1].getPrice()+"\t"+carMassage[choseNum-1].getSum()+"\t"+carMassage[choseNum-1].getAgain()+"\t"+carMassage[choseNum-1].getName());
money+=carMassage[choseNum-1].getPrice();
pNumber+=carMassage[choseNum-1].getSum();
}
if(carMassage[choseNum-1] instanceof Pick)
{
System.out.println(carMassage[choseNum-1].getId()+"\t"+"\t"+carMassage[choseNum-1].getPrice()+"\t"+carMassage[choseNum-1].getSum()+"\t"+carMassage[choseNum-1].getAgain()+"\t"+carMassage[choseNum-1].getName());
money+=carMassage[choseNum-1].getPrice();
pNumber+=carMassage[choseNum-1].getSum();
weight+=carMassage[choseNum-1].getAgain();
}
if(carMassage[choseNum-1] instanceof Trcuk)
{
System.out.println(carMassage[choseNum-1].getId()+"\t"+"\t"+carMassage[choseNum-1].getPrice()+"\t"+carMassage[choseNum-1].getSum()+"\t"+carMassage[choseNum-1].getAgain()+"\t"+carMassage[choseNum-1].getName());
money+=carMassage[choseNum-1].getPrice();
weight+=carMassage[choseNum-1].getAgain();
}
}
System.out.println("租多少天");
int day =input.nextInt();
int sumMoney =day * money;
System.out.println("总共租了"+number+"辆车");
System.out.println("总共有"+pNumber+"人");
System.out.println("总共有"+weight+"顿");
System.out.println("总共花了"+sumMoney+"钱");
}
else if(inputNum==2)
{
System.out.println("欢迎下次再来");
}
else {System.out.println("你输入的错误请重新输入");}
}
}
热门评论
根据SHOKER老师的参照自己写的 第一次自己完完整整 很开心 相信自己以后会更加努力