在学校里面,老师讲的特别基础。往往都是通过一个java文件去解决问题,在面对对象设计这一块就显得特别的薄弱
所以在学习与书写的过程中一直要求自己运用面对对象的设计思想去书写。
总觉得自己想法很笨,虽然能够完成老师所要求的任务。但是总觉得自己方法不够便捷。坦白的说我不是一个什么创新的人,反而一切是那么的循规蹈矩。我觉得这个问题对于搞编程的开发者来说是致命的。
好了。说了这么多废话,亮出自己的代码,如有不足的地方,请各位指正。
今天有看了各位前辈写的,自己又进行一些修改。总算是学习了多态的用法
抽象类:traffic
public abstract class traffic {
public abstract void info() ;
}
修改后
public class traffic {
public void info() {
};
public String CarName() {
return null;
};
public int Manned() {
return 0;
};
public int money() {
return 0;
};
public int load() {
return 0;
};
}
子类:car
public class car extends traffic {
String CarName;
int money;
int Manned;
public car(String CarName, int money, int Manned) {
this.CarName = CarName;
this.money = money;
this.Manned = Manned;
}
public void info() {
System.out.println(CarName + " " + money + "元/天" + " " + "载客"
+ Manned + "人");
}
}
修改后
public class car extends traffic {
String CarName;
int money;
int Manned;
public car(String CarName, int money, int Manned) {
this.CarName = CarName;
this.money = money;
this.Manned = Manned;
}
public void info() {
System.out.println(CarName + " " + money + "元/天" + " " + "载客"
+ Manned + "人");
}
@Override
public String CarName() {
// TODO Auto-generated method stub
return CarName;
}
@Override
public int Manned() {
// TODO Auto-generated method stub
return Manned;
}
@Override
public int money() {
// TODO Auto-generated method stub
return money;
}
@Override
public int load() {
// TODO Auto-generated method stub
return 0;
}
}
子类 bus
public class bus extends traffic {
String BusName;
int money;
int Manned;
public bus(String BusName,int money,int Manned) {
this.BusName=BusName;
this.money =money;
this.Manned =Manned;
}
public void info() {
System.out.println(BusName+" "+money+"元/天"+" "+"载客"+Manned+"人");
}
}
修改后
public class bus extends traffic {
String BusName;
int money;
int Manned;
public bus(String BusName,int money,int Manned) {
this.BusName=BusName;
this.money =money;
this.Manned =Manned;
}
public void info() {
System.out.println(BusName+" "+money+"元/天"+" "+"载客"+Manned+"人");
}
@Override
public String CarName() {
// TODO Auto-generated method stub
return BusName;
}
@Override
public int Manned() {
// TODO Auto-generated method stub
return Manned;
}
@Override
public int money() {
// TODO Auto-generated method stub
return money;
}
@Override
public int load() {
// TODO Auto-generated method stub
return 0;
}
}
子类 pickup
public class pickup extends traffic {
String PkName;
int money;
int Manned;
int load;
public pickup (String PkName, int money, int Manned ,int load) {
this.PkName = PkName;
this.money = money;
this.Manned = Manned;
this.load= load;
}
public void info() {
System.out.println(PkName + " " + money + "元/天" + " " + "载客"
+ Manned + "人"+"载货"+load+"吨");
}
}
修改后
public class pickup extends traffic {
String PkName;
int money;
int Manned;
int load;
public pickup (String PkName, int money, int Manned ,int load) {
this.PkName = PkName;
this.money = money;
this.Manned = Manned;
this.load= load;
}
@Override
public String CarName() {
// TODO Auto-generated method stub
return PkName;
}
@Override
public int Manned() {
// TODO Auto-generated method stub
return Manned;
}
@Override
public int money() {
// TODO Auto-generated method stub
return money;
}
@Override
public int load() {
// TODO Auto-generated method stub
return load;
}
public void info() {
System.out.println(PkName + " " + money + "元/天" + " " + "载客"
+ Manned + "人"+"载货"+load+"吨");
}
}
测试类 test
用while循环去解决因用户输入问题,而导致程序出错
public class Test {
static car[] car = { new car("奥迪A4", 500, 4), new car("马自达6", 400, 4) };
static bus[] bus = { new bus("金龙", 800, 20), new bus("依维柯", 1000, 20) };
static pickup[] pickup = { new pickup("皮卡雪6", 450, 4, 2) };
static int x = 1;
static int sum = 0;
static int sumPerson = 0;
static int sumload = 0;
static ArrayList<Integer> chooseCar = new ArrayList<Integer>();
static ArrayList<Integer> chooseBus = new ArrayList<Integer>();
static ArrayList<Integer> choosePickup = new ArrayList<Integer>();
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
while (true) {
Scanner scanner = new Scanner(System.in);
int flag = scanner.nextInt();
if (flag == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号" + " 汽车名称 " + " 租金 "
+ " 容量 ");
for (int i = 0; i < car.length; i++) {
x = x + i;
System.out.print(x + " ");
car[i].info();
}
x = x + 1;
for (int i = 0; i < bus.length; i++) {
x = x + i;
System.out.print(x + " ");
bus[i].info();
}
x = x + 1;
for (int i = 0; i < pickup.length; i++) {
x = x + i;
System.out.print(x + " ");
pickup[i].info();
}
System.out.println("请输入您要租汽车的数量");
Scanner scanner2 = new Scanner(System.in);
int renNumber = scanner2.nextInt();
for (int i = 1; i <= renNumber; i++) {
System.out.println("请输入第" + i + "俩车的序号");
boolean falge =true;
while (falge) {
Scanner scanner3 = new Scanner(System.in);
int chooseSelect = scanner3.nextInt();
if (chooseSelect > car.length + bus.length + pickup.length) {
System.out.println("请您按照要求填写序号");
} else {
if (chooseSelect - car.length <= 0) {
chooseCar.add(chooseSelect - 1);
} else if (chooseSelect - car.length - bus.length > 0) {
choosePickup.add(chooseSelect - car.length - bus.length
- 1);
}
else if (chooseSelect - car.length >= 0) {
chooseBus.add(chooseSelect - car.length - 1);
}
falge =false;
}
}
}
System.out.println("********请输入租车天数*********");
Scanner scanner3 = new Scanner(System.in);
int Day = scanner3.nextInt();
System.out.println("您的账单有:");
System.out.println("****可载人的车有");
if (chooseCar.size() != 0) {
for (int i = 0; i < chooseCar.size(); i++) {
System.out.print(car[chooseCar.get(i)].CarName);
System.out.print(" ");
sum = sum + car[chooseCar.get(i)].money;
sumPerson = sumPerson + car[chooseCar.get(i)].Manned;
}
}
if (chooseBus.size() != 0) {
for (int i = 0; i < chooseBus.size(); i++) {
System.out.print(bus[chooseBus.get(i)].BusName);
System.out.print(" ");
sum = sum + bus[chooseBus.get(i)].money;
sumPerson = sumPerson + bus[chooseBus.get(i)].Manned;
}
}
if (choosePickup.size() != 0) {
for (int i = 0; i < choosePickup.size(); i++) {
System.out.print(pickup[choosePickup.get(i)].PkName);
System.out.print(" ");
sumPerson = sumPerson + pickup[choosePickup.get(i)].Manned;
}
}
System.out.println("共载人:" + sumPerson + "人");
if (choosePickup.size() != 0) {
System.out.println("****载货的车有");
for (int i = 0; i < choosePickup.size(); i++) {
System.out.print(pickup[choosePickup.get(i)].PkName);
System.out.print(" ");
sum = sum + pickup[choosePickup.get(i)].money;
sumload = sumload + pickup[choosePickup.get(i)].load;
}
System.out.println("共载货" + sumload + "吨");
}
System.out.println("租车总价格:" + sum * Day + "元");
} else if (flag == 0) {
System.out.println("系统关闭中...");
System.exit(0);
} else {
System.out.println("您输入的信息不符合提示要求");
// System.out.println("系统关闭中...");
// System.exit(0);
}
}
}
}
修改后
public class Test {
static traffic[] traffics = { new car("奥迪A4", 500, 4),
new car("马自达6", 400, 4), new bus("金龙", 800, 20),
new bus("依维柯", 1000, 20), new pickup("皮卡雪6", 450, 4, 2) };
static int sum = 0;
static int sumPerson = 0;
static int sumload = 0;
static ArrayList<Integer> choose = new ArrayList<Integer>();
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
while (true) {
Scanner scanner = new Scanner(System.in);
int flag = scanner.nextInt();
if (flag == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号" + " 汽车名称 " + " 租金 "
+ " 容量 ");
for (int i = 1; i <= traffics.length; i++) {
System.out.print(i + " ");
traffics[i - 1].info();
}
System.out.println("请输入您要租汽车的数量");
Scanner scanner2 = new Scanner(System.in);
int renNumber = scanner2.nextInt();
for (int i = 1; i <= renNumber; i++) {
System.out.println("请输入第" + i + "俩车的序号");
boolean falge = true;
while (falge) {
Scanner scanner3 = new Scanner(System.in);
int chooseSelect = scanner3.nextInt();
if (chooseSelect > traffics.length) {
System.out.println("请您按照要求填写序号");
} else {
choose.add(chooseSelect - 1);
falge = false;
}
}
}
System.out.println("********请输入租车天数*********");
Scanner scanner3 = new Scanner(System.in);
int Day = scanner3.nextInt();
System.out.println("您的账单有:");
System.out.println("****可载人的车有");
for (int i = 0; i < choose.size(); i++) {
System.out.print(traffics[choose.get(i)].CarName());
System.out.print(" ");
sum = sum + traffics[choose.get(i)].money();
sumPerson = sumPerson + traffics[choose.get(i)].Manned();
}
System.out.println("共载人:" + sumPerson + "人");
System.out.println("****载货的车有");
for (int i = 0; i < choose.size(); i++) {
if (traffics[choose.get(i)].load() != 0) {
System.out.print(traffics[choose.get(i)].CarName());
System.out.print(" ");
sumload = sumload + traffics[choose.get(i)].load();
}
}
System.out.println("共载货" + sumload + "吨");
System.out.println("租车总价格:" + sum * Day + "元");
} else if (flag == 0) {
System.out.println("系统关闭中...");
System.exit(0);
} else {
System.out.println("您输入的信息不符合提示要求");
// System.out.println("系统关闭中...");
// System.exit(0);
}
}
}
}
/**/
修改后觉得主要是test方法里面的逻辑更加简单
感谢各位前辈所留下的指导
/****后记****/
不知道自己还能坚持走多久,很彷徨,有的时候在认为自己这条路是否是真的走错了。坚持吧!
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦