菜鸟的羽毛
2016-06-29 10:36
求答答租车系统标准代码,最好带注释
ackage java.util;
import java.util.Scanner;
public class car {
String carName;
int carNumber;
int carMonney;
public void carr(){
System.out.println("1号车");
}
public void pipk(){
System.out.println("2号车");
}
public static void mian(String[] ages){
car net=new car();
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.print("欢迎使用租车系统");
System.out.println("回复1:继续租车 回复 2 退出系统");
int x=sc.nextInt();
if(x==1){
net.carr();
}else {
net.pipk();
}
为什么我这里键盘输入1之后没反应!求解!
/*******************程序说明************************
1、每个类要放在独立的java文件中,文件命名方式如注释
2、Coach表示客车,Truck表示火车,Pickup表示皮卡
***************代码已通过验证,有任何问题欢迎交流****************/
/************View.java**********************/
package recentcar;
import java.util.Scanner;
import java.util.ArrayList;
public class View {
public static void main(String args[]){
Scanner input=new Scanner(System.in); // 保存用户输入值
/********可租车辆信息*********/
Car cars[]=new Car[7];
cars[1]=new Coach("奥迪A4",500,4);
cars[2]=new Coach("马自达6",400,4);
cars[3]=new Pickup("皮卡雪6",450,4,2);
cars[4]=new Coach("金龙",800,20);
cars[5]=new Truck("松花江",400,4);
cars[6]=new Truck("依维柯",1000,20);
ArrayList<Car> people=new ArrayList<Car>(); //租车中载人车辆动态数组
ArrayList<Car> goods=new ArrayList<Car>(); //租车中载货车辆动态数组
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
int number=input.nextInt();
if(number==1){
System.out.println("您可租车的类型及价目表:");
show(cars);
System.out.println("请输入您要租汽车的数量:");
number=input.nextInt();
int[] order=new int[number+1]; //根据租车数量得到租车序列号数组
/*************获取用户租车序列号并判断是载人还是载货,放入相应的动态数组中**********************/
for(int i=1;i<=number;i++){
System.out.println("请输入第"+i+"辆车的序号:");
order[i]=input.nextInt();
if((cars[order[i]] instanceof Coach)){
people.add(cars[order[i]]);
}
else if((cars[order[i]] instanceof Truck)){
goods.add(cars[order[i]]);
}
else if((cars[order[i]] instanceof Pickup)){
people.add(cars[order[i]]);
goods.add(cars[order[i]]);
}
}
System.out.println("请输入租车天数:");
int day=input.nextInt();//租车天数
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
/*************输出载人车辆名称*********************/
for(int i=0;i<people.size();i++){
Coach outpeople;
Pickup outpickup;
if(people.get(i) instanceof Coach){
outpeople=(Coach)people.get(i);
System.out.print(outpeople.getname());
}
else if(people.get(i) instanceof Pickup){
outpickup=(Pickup)people.get(i);
System.out.print(outpickup.getname());
}
System.out.print("\t");
}
System.out.println("共载人"+calPerson(people)); //总人数
/*************输出载货车辆名称*********************/
System.out.println("***载货的车有:");
for(int i=0;i<goods.size();i++){
Truck outtruck;
Pickup outpickup;
if(goods.get(i) instanceof Truck){
outtruck=(Truck)goods.get(i);
System.out.print(outtruck.getname());
}
else if(goods.get(i) instanceof Pickup){
outpickup=(Pickup)goods.get(i);
System.out.print(outpickup.getname());
}
System.out.print("\t");
}
System.out.println("共载货"+calGoods(goods));//计算载货总量
System.out.println("***租车总价格:"+calMoney(order,cars,day)); //计算总金额
}
input.close();
}
/********************显示可租车辆信息的函数*****************************/
public static void show(Car[] cars){
Coach outpeople;
Pickup outpickup;
Truck outtruck;
System.out.println("序号\t汽车名称\t租金\t容量");
for(int i=1;i<cars.length;i++){
if(cars[i] instanceof Coach){
outpeople=(Coach)cars[i];
System.out.println(i+".\t"+outpeople.getname()+"\t"+outpeople.getmoney()+"元/天\t载人:"+outpeople.getbusload());
}
else if(cars[i] instanceof Truck){
outtruck=(Truck)cars[i];
System.out.println(i+".\t"+outtruck.getname()+"\t"+outtruck.getmoney()+"元/天\t载货:"+outtruck.getboatload());
}
else if(cars[i] instanceof Pickup){
outpickup=(Pickup)cars[i];
System.out.println(i+".\t"+outpickup.getname()+"\t"+outpickup.getmoney()+"元/天\t载人:"+outpickup.getbusload()+" 载货:"+outpickup.getboatload());
}
}
}
/********************计算总人数的函数*****************************/
public static int calPerson(ArrayList<Car> people){
int total=0;
Coach outpeople;
Pickup outpickup;
for(int i=0;i<people.size();i++){
if(people.get(i) instanceof Coach){
outpeople=(Coach)people.get(i);
total=outpeople.getbusload()+total;
}
else if(people.get(i) instanceof Pickup){
outpickup=(Pickup)people.get(i);
total=outpickup.getbusload()+total;
}
}
return total;
}
/********************计算总载货量的函数*****************************/
public static int calGoods(ArrayList<Car> goods){
int total=0;
Truck outtruck;
Pickup outpickup;
for(int i=0;i<goods.size();i++){
if(goods.get(i) instanceof Truck){
outtruck=(Truck)goods.get(i);
total=outtruck.getboatload()+total;
}
else if(goods.get(i) instanceof Pickup){
outpickup=(Pickup)goods.get(i);
total=outpickup.getboatload()+total;
}
}
return total;
}
/********************计算总金额的函数*****************************/
public static int calMoney(int[] order,Car[] cars,int day){
int total=0;
for(int i=1;i<order.length;i++){
total=cars[order[i]].getmoney()+total;
}
total=total*day;
return total;
}
}
/***********************Car.java***************************/
package recentcar;
public class Car {
private String name;
private int money;
Car(String name,int money){
this.name=name;
this.money=money;
}
public void setmoney(int money){
this.money=money;
}
public int getmoney(){
return money;
}
public void setname(String name){
this.name=name;
}
public String getname(){
return name;
}
}
/**********************Coach.java*****************************/
package recentcar;
public class Coach extends Car{
int busload;
Coach(String name,int money,int busload){
super(name,money);
this.busload=busload;
}
public void setbusload(int busload){
this.busload=busload;
}
public int getbusload(){
return busload;
}
}
/*******************Truck.java*****************/
package recentcar;
public class Truck extends Car{
int boatload;
Truck(String name,int money,int boatload){
super(name,money);
this.boatload=boatload;
}
public void setboatload(int boatload){
this.boatload=boatload;
}
public int getboatload(){
return boatload;
}
}
/*****************Pickup.java*********************/
package recentcar;
public class Pickup extends Car{
int busload;
int boatload;
Pickup(String name,int money,int busload,int boatload){
super(name,money);
this.busload=busload;
this.boatload=boatload;
}
public void setbusload(int busload){
this.busload=busload;
}
public int getbusload(){
return busload;
}
public void setboatload(int boatload){
this.boatload=boatload;
}
public int getboatload(){
return boatload;
}
}
我的思路是
1.定义了一个Car抽象类
2.定义了一个载客接口和一个载货接口
3.定义具体类轿车继承Car实现载客接口
4.定义具体类客车继承Car实现载客接口
5.定义具体类轻卡继承Car实现载货接口
6.定义具体类重卡继承Car实现载货接口
7.定义具体类皮卡继承Car实现载货接口和载客接口
8.定义一个测试类实现哒哒租车系统
这个是我写的测试类,写的不好,请见谅
package com.dada_02;
import java.util.Scanner;
import com.dada_01.Car;
import com.dada_03.BigTruck;
import com.dada_03.Bus;
import com.dada_03.PickUp;
import com.dada_03.SmallCar;
import com.dada_03.SmallTruck;
public class CarTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到哒哒租车系统,是否需要租车? 1.是 2.否");
int x = sc.nextInt();
if(x == 1){
System.out.println("请输入你想选择哪种车型? 1.载客 2.载货 3.皮卡");
int y = sc.nextInt();
if(y == 1){
System.out.println("请输入你想选择哪种车型? 1.轿车 2.客车");
int z = sc.nextInt();
if(z == 1){
showSmallCar();
}else if (z == 2){
showBus();
}
}else if(y == 2){
System.out.println("请输入你想选择哪种车型? 1.轻卡 2.重卡");
int a = sc.nextInt();
if(a == 1){
showSmallTruck();
}else if (a == 2){
showBigTruck();
}else{
System.out.println("你的输入有误");
}
}else if(y == 3){
showPickUp();
}else {
System.out.println("你的输入有误!");
}
}else if(x == 2){
System.out.println("已退出哒哒租车程序");
}else{
System.out.println("你的输入有误!");
}
}
public static void showSmallCar(){
Car c = new SmallCar();
SmallCar sc1 = (SmallCar) c ;
sc1.setBrand("奥迪A4");
sc1.setColor("黑色");
sc1.setPrice(500);
System.out.println("品牌为:"+sc1.getBrand()+", 颜色为:"+sc1.getColor()+", 价格为:"+sc1.getPrice()+"元/天");
sc1.passengernum();
sc1.run();
}
public static void showBus(){
Car c = new Bus("金龙汽车", "白色",600);
Bus b = (Bus) c ;
System.out.println("品牌为:"+b.getBrand()+", 颜色为:"+b.getColor()+", 价格为:"+b.getPrice()+"元/天");
b.passengernum();
b.run();
}
public static void showSmallTruck(){
Car c = new SmallTruck("江铃","白色",400);
SmallTruck st =(SmallTruck) c;
System.out.println("品牌为:"+st.getBrand()+", 颜色为:"+st.getColor()+", 价格为:"+st.getPrice()+"元/天");
st.loadnum();
st.run();
}
public static void showBigTruck(){
Car c = new BigTruck("陕西重汽","红色",1000);
BigTruck bt = (BigTruck) c;
System.out.println("品牌为:"+bt.getBrand()+", 颜色为:"+bt.getColor()+", 价格为:"+bt.getPrice()+"元/天");
bt.loadnum();
bt.run();
}
public static void showPickUp(){
Car c = new PickUp("福特皮卡","黑色",500);
PickUp pu = (PickUp) c ;
System.out.println("品牌为:"+pu.getBrand()+", 颜色为:"+pu.getColor()+", 价格为:"+pu.getPrice()+"元/天");
pu.passengernum();
pu.loadnum();
pu.run();
}
}
Java入门第二季 升级版
530653 学习 · 6091 问题
相似问题