父类
package com.mybbzy2;
public abstract class Car {
int money ;
int goods;
int people;
String name;
public abstract void run();
public abstract String getinfo();
public String getCar(){
String list = "\t"+name+"\t"+money+"/天";
return list;
}
}
子类(载人客车)
package com.mybbzy2;
public class Peoplecar extends Car {
public Peoplecar(String name,int money,int people){
this.name = name;
this.money = money;
this.people = people;
}
@Override
public void run() {
// TODO 自动生成的方法存根
}
@Override
public String getinfo() {
// TODO 自动生成的方法存根
String list = "\t"+this.name+"\t"+this.money+"/天"+"\t"+this.people+"人";
return list;
}
}
子类(货车)
package com.mybbzy2;
public class Truck extends Car {
public Truck(String name,int money,int goods){
this.name = name;
this.money = money;
this.goods = goods;
}
@Override
public void run() {
// TODO 自动生成的方法存根
}
@Override
public String getinfo() {
// TODO 自动生成的方法存根
String list = "\t"+this.name+"\t"+this.money+"/天"+"\t"+this.goods+"顿";
return list;
}
}
子类皮卡
package com.mybbzy2;
public class Pick extends Car {
public Pick(String name, int money,int people,int goods){
this.goods = goods;
this.money = money;
this.name = name;
this.people = people;
}
@Override
public void run() {
// TODO 自动生成的方法存根
}
@Override
public String getinfo() {
// TODO 自动生成的方法存根
String list = "\t"+this.name+"\t"+this.money+"/天"+"\t"+this.people+"人"+"\t"+this.goods+"顿";
return list;
}
}
主函数
package com.mybbzy2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("****欢迎使用达达租车系统!*****");
System.out.println("你是否要租车? 是:请安“1” 否:请按“2”");
Scanner sc = new Scanner(System.in);//获取用户的输入
int a = sc.nextInt();
if(a == 2){
System.out.println("欢迎您下次使用达达租车系统!");
}else if (a==1){
Car []cars = new Car[]{
new Peoplecar("奥迪A4",500,4),
new Peoplecar("奥迪A6",600,6),
new Truck("拖拉机",100,7),
new Truck("大卡车",300,10),
new Pick("小皮卡",200,4,5),
new Pick("大皮卡",300,6,8)
};
System.out.println("请选择车类别:1-小客车、2-货车、3-皮卡、4-全部" );
int b = sc.nextInt();
if(b==4){
System.out.println("----------今日可租车清单----------");
System.out.println("序号 汽车名称 租金/天 容量");
for(int i = 0;i<cars.length;i++){
System.out.println((i+1)+"\t"+cars[i].getinfo());
}
}else if(b==3){
for(int i = 0;i<cars.length;i++){
if(cars[i] instanceof Pick){
System.out.println((i+1)+"\t"+cars[i].getinfo());
}
}
}else if(b==2){
for(int i = 0;i<cars.length;i++){
if(cars[i] instanceof Truck){
System.out.println((i+1)+"\t"+cars[i].getinfo());
}
}
}else if(b==1){
for(int i = 0;i<cars.length;i++){
if(cars[i] instanceof Peoplecar){
System.out.println((i+1)+"\t"+cars[i].getinfo());
}
}
}else{
System.out.println("您输入的数字有误!请重新输入:");
}
System.out.println("请选择车辆(输入序号,空格隔开 ):");
Scanner scanner = new Scanner(System.in);
String input3 = scanner.nextLine();
input3 = input3.trim();
String[] temp = input3.split(" ");
int[] nums = new int[temp.length];
System.out.println("----------租车清单----------");
System.out.println("序号 名称 租金/天 ");
int result=0; for (int i = 0; i < temp.length; i++) {
nums[i] = Integer.parseInt(temp[i]);
System.out.println(i+cars[nums[i]].getinfo());
result=cars[nums[i]].money+result; }
System.out.printf("%50s", "总价格:"+result+"\n");
System.out.println("确定-1 取消-2");
int c=scanner.nextInt();
if(c==1){
System.out.println("租车成功!欢迎下次使用!");
} else{
System.out.println("请重新选择");
}
scanner.close();
}else{
System.out.println("您输入的数字有误!请重新输入:");
}
sc.close();
}
}
新手练习。还请大神多多提建议!多多关照