import java.util.*;
//载客车的抽象父类
abstract class Zkc{
public abstract int money(int days);
}
//奥迪A4载客车
class AudiA4 extends Zkc{
String name = "奥迪A4";
int price = 500;
int p_number = 4;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//马自达6载客车
class MaZiDa6 extends Zkc{
String name = "马自达6";
int price = 400;
int p_number = 4;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//金龙载客车
class JinLong extends Zkc{
String name = "金龙";
int price = 800;
int p_number = 20;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//载货车的抽象父类
abstract class Zhc{
public abstract int money(int days);
}
//松花江载货车
class SongHuaJiang extends Zhc{
String name = "松花江";
int price = 400;
int c_number = 4;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//依维柯载货车
class YiWeiKe extends Zhc{
String name = "依维柯";
int price = 1000;
int c_number = 20;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//皮卡车的抽象父类
abstract class Pkc{
public abstract int money(int days);
}
//皮卡雪6皮卡车
class PiKaXue6 extends Pkc{
String name = "皮卡雪6";
int price = 450;
int p_number = 4;
int c_number = 2;
int money;
public int money(int days){
money = price*days;
return money;
}
}
//答答租车系统
public class Ddzcxt{
public static void main(String[] args){
//通过父类引用调用所有车的对象
//载客车
AudiA4 audia4 = new AudiA4();//奥迪A4
MaZiDa6 mazida6 = new MaZiDa6();//马自达6
JinLong jinlong = new JinLong();//金龙
//载货车
SongHuaJiang songhuajiang = new SongHuaJiang();//松花江
YiWeiKe yiweike = new YiWeiKe();//依维柯
//皮卡车
PiKaXue6 pikaxue6 = new PiKaXue6();
//答答租车系统执行代码
System.out.println("******欢迎使用答答租车系统******");
System.out.println("您是否要租车:1.是 0.否(选否直接退出程序)");
String shi_fou;//定义此变量用来接收用户的输入,用String类型防止用户胡乱输入
Scanner sf = new Scanner(System.in);
int count = 0;//用来记录用户输错的次数
while(true){
shi_fou = sf.next();//用户输入
if(shi_fou.equals("1"))
break;//用户输入1则退出循环
else if(shi_fou.equals("0"))
System.exit(0);//用户输入0则结束程序
else{
System.out.println("您输入的信息有误,请重新输入,输错3次将直接退出程序!");
System.out.println("您是否要租车:1.是 0.否(选否直接退出程序)");
count++;//输错的次数加1
}
if(count==3)
System.exit(0);//用户输错3次退出程序
}
System.out.println("您可租车的类型及其价目表:");//价目信息表
System.out.println("序号 汽车名称 租金 容量 ");
System.out.println("1. 奥迪A4 500元/天 载人:4人");
System.out.println("2. 马自达6 400元/天 载人:4人");
System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货:2吨");
System.out.println("4. 金龙 800元/天 载人:20人");
System.out.println("5. 松花江 400元/天 载货:4吨");
System.out.println("6. 依维柯 1000元/天 载货:20吨");
int carNumber;//租几辆车
int xuhao;//车的序号
int tianshu;//租车的天数
int money = 0;//一辆车的钱
int allmoney = 0;//总金额
int zks = 0;//载客数
int allzks = 0;//总载客数
int zhs = 0;//载货数
int allzhs = 0;//总载货数
int zkcount = 0;//载客车数
int zhcount = 0;//载货车数
ArrayList<String> zkcar = new ArrayList<String>();//定义字符串数组用来存储选购的载人车名
ArrayList<String> zhcar = new ArrayList<String>();//定义字符串数组用来存储选购的载客车名
System.out.println("请输入您要租车的数量:");
Scanner zc_number = new Scanner(System.in);
carNumber = zc_number.nextInt();//用户输入租几辆车
for(int i = 1;i<=carNumber;i++){
System.out.println("请输入第"+i+"辆车的序号(注意,输入没有的序号程序将会退出)");
Scanner xh = new Scanner(System.in);
xuhao = xh.nextInt();//用户输入要租的车的序号
System.out.println("请输入租车的天数:");
Scanner ts = new Scanner(System.in);
tianshu = ts.nextInt();//用户输入这车要租几天
switch(xuhao){
case 1:
money = audia4.money(tianshu);
allmoney = allmoney + money;
zkcar.add(audia4.name);
zks = audia4.p_number;
allzks = allzks + zks;
zkcount++;
break;
case 2:
money = mazida6.money(tianshu);
allmoney = allmoney + money;
zkcar.add(mazida6.name);
zks = mazida6.p_number;
allzks = allzks + zks;
zkcount++;
break;
case 3:
money = pikaxue6.money(tianshu);
allmoney = allmoney + money;
zkcar.add(pikaxue6.name);
zhcar.add(pikaxue6.name);
zks = pikaxue6.p_number;
allzks = allzks + zks;
zhs = pikaxue6.c_number;
allzhs = allzhs + zhs;
zkcount++;
zhcount++;
break;
case 4:
money = jinlong.money(tianshu);
allmoney = allmoney + money;
zkcar.add(jinlong.name);
zks = jinlong.p_number;
allzks = allzks + zks;
zkcount++;
break;
case 5:
money = songhuajiang.money(tianshu);
allmoney = allmoney + money;
zhcar.add(songhuajiang.name);
zhs = songhuajiang.c_number;
allzhs = allzhs + zhs;
zhcount++;
break;
case 6:
money = yiweike.money(tianshu);
allmoney = allmoney + money;
zhcar.add(yiweike.name);
zhs = yiweike.c_number;
allzhs = allzhs + zhs;
zhcount++;
break;
default:
System.exit(0);
}
}
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for(int j = 0;j<zkcount;j++){
System.out.print(zkcar.get(j)+" ");
}
System.out.println("共载人:"+allzks+"人");
System.out.println("***载货的车有:");
for(int z = 0;z<zhcount;z++){
System.out.print(zhcar.get(z)+" ");
}
System.out.println("共载货:"+allzhs+"吨");
System.out.println("***租车总价格:"+allmoney+"元");
}
}
打开App,阅读手记
热门评论
为什么我的会出现个越界错误?
//用户输错系统改成以下代码用户体验会更好一点
}else{
if(count==3){
System.out.println("您已经输错三次,感谢您的使用,再见!");
System.exit(0);
}
System.out.println("您输入的信息有误,请重新输入 输错3次将自动退出系统");
System.out.println("您是否要租车:1、是 0、否");
count++;
}