最近看了慕课网的《Java入门 第二季》,来实际操作一下里面的一个小项目“答答租车系统”,由于笔者刚刚入门学习Java面向对象,只是个计算机小白,便只能用命令行执行,如果有待改进,希望大家能够给我建议。 其中使用了自定义异常、抽象类的方法重写和继承。
题目要求:
1.建立三种不同车型的抽象类 建立三种抽象类来区分三种车型,只能载人的汽车,只能载货的货车和两者都能载的皮卡。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 //car.class public abstract class car { public abstract String carName(); public abstract int money(); public abstract int people(); } //pika.class public abstract class pika { public abstract String pikaName(); public abstract int money(); public abstract int people(); public abstract int content(); } //trunk.class public abstract class trunk { public abstract String trunkName(); public abstract int money(); public abstract int content(); }
2.重写不同品牌类型的车辆 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 //audi.class public class audi extends car { @Override public String carName() { return "奥迪A4"; } @Override public int money() { return 500; } @Override public int people() { return 4; } } //jinlong.class public class jinlong extends car { @Override public String carName() { return "金龙"; } @Override public int money() { return 800; } @Override public int people() { return 20; } } //mazida.class public class mazida extends car{ @Override public String carName() { return "马自达6"; } @Override public int money() { return 400; } @Override public int people() { return 4; } } //pikaxue.class public class pikaxue extends pika { @Override public String pikaName() { return "皮卡雪6"; } @Override public int money() { return 450; } @Override public int people() { return 4; } @Override public int content() { return 2; } } //songhuajiang.class public class songhuajiang extends trunk { @Override public String trunkName() { return "松花江"; } @Override public int money() { return 400; } @Override public int content() { return 4; } } //yiweike.class public class songhuajiang extends trunk { @Override public String trunkName() { return "松花江"; } @Override public int money() { return 400; } @Override public int content() { return 4; } }
3.用户界面(user.class) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 import java.util.Scanner; public class user { public static void main(String[] args){ System.out.printf("欢迎使用答答租车系统n您是否要租车?1是 2否n");//用户进入界面 Scanner kb = new Scanner(System.in);//输入流 /* 欢迎使用答答租车系统 您是否要租车?1是 2否 1 */ int beginNum = kb.nextInt(); if (beginNum == 1) { car ad = new audi();//导入奥迪A4 car mzd = new mazida();//马自达 car jl = new jinlong();//金龙 pika pkx = new pikaxue();//皮卡雪 trunk shj = new songhuajiang();//松花江 trunk ywk = new yiweike();//依维柯 System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名称 租金 容量");//价目表头 System.out.println("1. " + ad.carName() + " " + ad.money() + "元/天 " + ad.people() + "人"); //1. 奥迪A4 500元/天 4人 System.out.println("2. " + mzd.carName() + " " + mzd.money() + "元/天 " + mzd.people() + "人"); System.out.println("3. " + jl.carName() + " " + jl.money() + "元/天 " + jl.people() + "人"); System.out.println("4. " + pkx.pikaName() + " " + pkx.money() + "元/天 " + pkx.people() + "人"+" "+pkx.content()+"吨"); System.out.println("5. " + shj.trunkName() + " " + shj.money() + "元/天 " + shj.content() + "吨"); System.out.println("6. " + ywk.trunkName() + " " + ywk.money() + "元/天 " + ywk.content() + "吨"); //输入租车天数 System.out.println("请输入您要租车的天数"); int Day=kb.nextInt(); //统计每种车的输入次数 int[] k=new int[7]; for(int i=0;i<=6;i++) k[i]=0; //输入您想租赁的汽车数量 System.out.println("请输入您想租赁的汽车数量。"); int n=kb.nextInt();//n为汽车数量 int moneyCount=0;//存储金额 int peopleCount=0;//总载客人数 int contentCount=0;//总载货量 for(int i=0;i<n;i++){ System.out.printf("请输入您选择的第%d辆车的序号,以回车键结束。n",i+1); int temp=kb.nextInt(); if(temp==1){ moneyCount+=ad.money()*Day; peopleCount+=ad.people(); k[1]++; } else if(temp==2){ moneyCount+=mzd.money()*Day; peopleCount+=mzd.people(); k[2]++; } else if(temp==3){ moneyCount+=jl.money()*Day; peopleCount+=jl.people(); k[3]++; } else if(temp==4){ moneyCount+=pkx.money()*Day; peopleCount+=pkx.people(); contentCount+=pkx.content(); k[4]++; } else if(temp==5){ moneyCount+=shj.money()*Day; contentCount+=shj.content(); k[5]++; } else if(temp==6){ moneyCount+=ywk.money()*Day; contentCount+=ywk.content(); k[6]++; } else{ //System.out.println("您的输入有误,请重新输入!"); i--; try { throw new MyException("您的输入有误,请重新输入!"); }catch (MyException e){ System.out.println(e.getMessage()); } } } System.out.println("**************您的账单如下:**************"); System.out.println(); System.out.println("可以载人的车:"); for(int i=1;i<=4;i++){ if(k[i]!=0){ if(i==1) System.out.print(ad.carName()+" "); if(i==2) System.out.print(mzd.carName()+" "); if(i==3) System.out.print(jl.carName()+" "); if(i==4) System.out.print(pkx.pikaName()+" "); } } System.out.println("总计可载"+peopleCount+"人"); System.out.println("可以载货的车:"); for(int i=4;i<=6;i++) { if (k[i] != 0) { if (i == 4) System.out.print(pkx.pikaName() + " "); if (i == 5) System.out.print(shj.trunkName() + " "); if (i == 6) System.out.print(ywk.trunkName() + " "); } } System.out.println("总计可载货"+contentCount+"吨"); System.out.println("****总计费用为"+moneyCount+"元****"); } else System.out.println("欢迎您的下次使用,谢谢!"); } }
4.还有自定义异常: 1 2 3 4 5 6 7 8 9 10 package Exception; public class MyException extends Exception{ public MyException(){ } public MyException(String message){ super(message); } }
实际执行界面: