答答租车系统
父类
package dada;
public abstract class Car {
String name;
int goods;
int passenger;
int price;
public abstract void run();
public abstract String getinfo();
public String getList(){
String list=" "+this.name+" "+this.price+"元 ";
return list;
}
}
子类1 载客车类
package dada;
public class SmallCar extends Car {
public SmallCar(String name,int passenger,int price){
this.name=name;
this.passenger=passenger;
this.price=price;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
public String getinfo() {
// TODO Auto-generated method stub
String info=this.name+" "+this.price+"元 "+this.passenger+"人";
return info;
}
}
子类2 货车类
package dada;
public class huoche extends Car {
public huoche(String name,int goods,int price){
this.name=name;
this.goods=goods;
this.price=price;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
public String getinfo() {
// TODO Auto-generated method stub
String info=this.name+" "+this.price+"元 "+this.goods+"吨";
return info;
}
}
子类3 皮卡车类
package dada;
public class pika extends Car {
public pika(String name,int goods,int passenger,int price){
this.name=name;
this.goods=goods;
this.passenger=passenger;
this.price=price;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
public String getinfo() {
String info=this.name+" "+this.price+"元 "+this.passenger+"人 "+this.goods+"吨 " ;
return info;
}
}
主方法
package dada;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
while(true){
System.out.println("欢迎进入答答租车系统");
System.out.println("是否租车? (1-是 、2-否)");
Scanner input =new Scanner(System.in);
int a=input.nextInt();
if(a==2){
System.out.println("欢迎下次使用答答租车系统!");
break;
}
//创建实例对象
else if(a==1){
Car cars[]={
new SmallCar("奥迪A4", 4, 500),
new SmallCar("马自达 ", 4, 400),
new pika("皮卡雪 ", 2, 4, 450),
new SmallCar("金龙 ", 20, 800),
new huoche("松花江 ", 4, 400),
new huoche("依维柯 ",20, 1000)
};
System.out.println("请选择车类别:1-小客车、2-货车、3-皮卡、4-全部" );
int b=input.nextInt();
//显示全部车辆
if(b==4){
System.out.println("----------今日可租车清单----------");
System.out.println("序号 汽车名称 租金/天 容量");
for(int i=0;i<cars.length;i++){
System.out.println(i+" "+cars[i].getinfo());
}
}
//显示皮卡车
if(b==3){
for(int i=0;i<cars.length;i++){
if(cars[i] instanceof pika)
System.out.println(i+" "+cars[i].getinfo());
}
}
//显示货车
else if(b==2){
for(int i=0;i<cars.length;i++){
if(cars[i] instanceof huoche)
System.out.println(i+" "+cars[i].getinfo());
}
}
//显示小汽车
else if(b==1){
for(int i=0;i<cars.length;i++){
if(cars[i] instanceof SmallCar)
System.out.println(i+" "+cars[i].getinfo());
}
}
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]].getList());
result=cars[nums[i]].price+result;
}
System.out.printf("%50s", "总价格:"+result+"\n");
System.out.println("确定-1 取消-2");
int c=input.nextInt();
if(c==1){
System.out.println("租车成功!欢迎下次使用!");
break;
}
else{
System.out.println("请重新选择");
}
}
else {
System.out.println("输入错误!请重新输入");
}
}
}
}
点击查看更多内容
21人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦