父类 抽象类 Car 定义了两个抽象方法
package com.dada;
public abstract class Car {
private int num;//序号
private String name;//车名
private int price;//价格
int pNum;//载入数
private int tNum;//载货数
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
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 getpNum() {
return pNum;
}
public void setpNum(int pNum) {
this.pNum = pNum;
}
public int gettNum() {
return tNum;
}
public void settNum(int tNum) {
this.tNum = tNum;
}
public abstract void run();
public abstract void show();
}
奥迪A4类
package com.dada;
public class Aodi extends Car {
@Override
public void run() {
setNum(1);
setName("奥迪A4");
setPrice(500);
setpNum(4);
settNum(0);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
马自达6类
package com.dada;
public class Mazida extends Car{
@Override
public void run() {
setNum(2);
setName("马自达6");
setPrice(400);
setpNum(4);
settNum(0);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
皮卡雪6类
package com.dada;
public class Pika extends Car{
@Override
public void run() {
setNum(3);
setName("皮卡雪6");
setPrice(450);
setpNum(4);
settNum(2);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
金龙类
package com.dada;
public class Jinlong extends Car{
@Override
public void run() {
setNum(4);
setName("金龙");
setPrice(800);
setpNum(20);
settNum(0);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
松花江类
package com.dada;
public class Songhua extends Car{
@Override
public void run() {
setNum(5);
setName("松花江");
setPrice(400);
setpNum(0);
settNum(4);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
依维柯类
package com.dada;
public class Yiweike extends Car{
@Override
public void run() {
setNum(6);
setName("依维柯");
setPrice(1000);
setpNum(0);
settNum(20);
}
@Override
public void show() {
System.out.print(getNum() + ".\t" + getName() + "\t" + getPrice() + "元/天 ");
if (getpNum() > 0)
System.out.print(" 载人:" + getpNum() + "人");
if (gettNum() > 0)
System.out.print(" 载货:" + gettNum() + "吨");
System.out.println();
}
}
开始类
package com.dada;
import java.util.Scanner;
public class Start {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
String s = sc.next();
if (s.equals("1")) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t 容量");
Aodi c1 = new Aodi();
Mazida c2 = new Mazida();
Pika c3 = new Pika();
Jinlong c4 = new Jinlong();
Songhua c5 = new Songhua();
Yiweike c6 = new Yiweike();
c1.run();
c2.run();
c3.run();
c4.run();
c5.run();
c6.run();
c1.show();
c2.show();
c3.show();
c4.show();
c5.show();
c6.show();
System.out.println("请输入您要租汽车的数量:");
int num = sc.nextInt();//租车数
String[] name = new String[num];
int[] price = new int[num];
int[] pNum = new int[num];
int[] tNum = new int[num];
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号:");
String x = sc.next();
if (x.equals("1")) {
name[i] = c1.getName();
price[i] = c1.getPrice();
pNum[i] = c1.getpNum();
tNum[i] = c1.gettNum();
} else if (x.equals("2")) {
name[i] = c2.getName();
price[i] = c2.getPrice();
pNum[i] = c2.getpNum();
tNum[i] = c2.gettNum();
} else if (x.equals("3")) {
name[i] = c3.getName();
price[i] = c3.getPrice();
pNum[i] = c3.getpNum();
tNum[i] = c3.gettNum();
} else if (x.equals("4")) {
name[i] = c4.getName();
price[i] = c4.getPrice();
pNum[i] = c4.getpNum();
tNum[i] = c4.gettNum();
} else if (x.equals("5")) {
name[i] = c5.getName();
price[i] = c5.getPrice();
pNum[i] = c5.getpNum();
tNum[i] = c5.gettNum();
} else if (x.equals("6")) {
name[i] = c6.getName();
price[i] = c6.getPrice();
pNum[i] = c6.getpNum();
tNum[i] = c6.gettNum();
} else {
System.out.println("输入错误!请重新输入!");
i--;
}
}
System.out.println("请输入租车天数:");
int day = sc.nextInt();
System.out.println("您的账单:");
int pSum = 0;
String pName = "";
int tSum = 0;
String tName = "";
int prSum = 0;
for (int i = 0; i < num; i++) {
pSum += pNum[i];
tSum += tNum[i];
prSum += price[i] * day;
if (pNum[i] > 0)
if (pName.indexOf(name[i]) == -1)
pName = pName + name[i] + " ";
if (tNum[i] > 0)
if (tName.indexOf(name[i]) == -1)
tName = tName + name[i] + " ";
}
if (pSum > 0)
System.out.println("可载人的车有:" + pName + "共载人:" + pSum + "人");
if (tSum > 0)
System.out.println("可载货的车有:" + tName + "共载货:" + tSum + "吨");
System.out.println("租车总价格:" + prSum + "元");
sc.close();
break;
} else if (s.equals("0")) {
break;
} else {
System.out.println("输入错误!请重新输入!");
}
}
}
}
源码压缩包可以在http://javatest.ys168.com下载
本人新手,欢迎各位提出意见