一直在Imooc上自学java,在第二季的末尾有一个作业,我完成了,分享一下让我的博客也能有点生机。。。。。。

下面的一共有8个类(有点多),实现的是一个控制台的租车功能

/*ZaiHuo 是货车类的父类*/
public
class ZaiHuo { char id; String name; int liang; int money; public String xiaoHuoche() { return null; } public String daHuoche() { return null; } }

/*ZaiKe是客车的父类*/
public class ZaiKe {

    char id;
    String name;
    int people;
    int money;

    public String Xiaoqiche() {
        return null;

    }

    public String Dakeche() {
        return null;

    }
}

/*抽象类ZaiKeAndRen是皮卡这一特殊类(既可以载人也可以载货)的父类,为什么一定用抽象类呢?其实是为了复习抽象类*/
public
abstract class ZaiKeAndRen { char id; String name; int liang; int people; int money; public abstract String yunhuoAndzairen(); }
/*huoChe 继承了 ZaiHuo类 ,在main方法中会新建ZaiHuo类的对象,主要是数据模型需要*/
public
class huoChe extends ZaiHuo { char id; String name; int liang; int money; public huoChe(char newId, String newName, int newLiang, int newMenoy) { // TODO Auto-generated constructor stub this.id = newId; this.name = newName; this.liang = newLiang; this.money = newMenoy; } public String xiaoHuoche() { return id + " " + name + " "+" 载货量:" + liang + " 租金:" + money + "每天"; } public huoChe(String newName, char newId, int newLiang, int newMenoy) { // TODO Auto-generated constructor stub this.id = newId; this.name = newName; this.liang = newLiang; this.money = newMenoy; } //为啥会有两个 有参数的构造方法呢?因为是 货车 有 大货车和小货车 ,分开,节省代码(个人观点) public String daHuoche() { return id + " " + name +" "+ " 载货量:" + liang + " 租金:" + money + "每天"; } }

 

/* keChe 类继承了Zaike类 在main 方法新建它的对象,主要也是数据模型需要*/
public
class keChe extends ZaiKe { char id; String name; int people; int money; public keChe(char newId, String newName, int newpeople, int newMenoy) { // TODO Auto-generated constructor stub this.id = newId; this.name = newName; this.people = newpeople; this.money = newMenoy; } public String Xiaoqiche() { return id + " " + name + " 载货量:" + people + " 租金:" + money + "每天"; } public keChe(String newName, char newId, int newpeople, int newMenoy) { // TODO Auto-generated constructor stub this.id = newId; this.name = newName; this.people = newpeople; this.money = newMenoy; } //客车也有 大客车和小客车 所以有两个有参数的构造方法并且有两个 重写的方法 public String Dakeche() { return id + " " + name + " 载货量:" + people + " 租金:" + money + "每天"; } }

 

/*皮卡 继承了 ZaiKeAndRen 抽象的父类 同样在main 方法中会新建它的对象,同样主要也是数据模型需要*/
public
class pickUp extends ZaiKeAndRen { char id; String name; int liang; int poeple; int money; public pickUp(char newId, String newName, int newLiang, int newpeople, int newMenoy) { // TODO Auto-generated constructor stub this.id = newId; this.name = newName; this.liang = newLiang; this.poeple = newpeople; this.money = newMenoy; } @Override//Override 重写 public String yunhuoAndzairen() { return id + " " + name + " "+" 载货量:" + liang + " 载人量:" + poeple+ " 租金:"+money + "每天"; } }

 

/*该类为主方法所在的类,前面几个类是给出 数据模型(车)即现实世界的事物主要特征,为我们要现实的信息系统工作。那么一定还有业务类构成租车系统的一部分*/
public
class TestCar { /** * @param args */ static Scanner sinput = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub while (true) { //一直循环 直到不满足条件 Business business = new Business(); Business.welcome(); // 类名直接调用 静态方法 int choose = sinput.nextInt(); if (choose == 1) { ZaiKe ke1 = new keChe('1', "奥迪A4", 5, 100); ZaiKe ke2 = new keChe("金龙-8", '2', 30, 300); ZaiHuo huo1 = new huoChe('3', "小东风", 5, 150); ZaiHuo huo2 = new huoChe("解放", '4', 30, 350); ZaiKeAndRen keAndRen = new pickUp('5', "小力帆", 5, 5, 200); System.out.println("序号"+" "+"牌子"+" "+" 容量"+ " "+"租金"); System.out.println(ke1.Xiaoqiche() + "\n" + ke2.Dakeche() + "\n" + huo1.xiaoHuoche() + "\n" + huo2.daHuoche() + "\n" + keAndRen.yunhuoAndzairen()); System.out.println("请输入租车天数"); int day = sinput.nextInt(); String zhangdan = business.jisuan(day); //调用业务类韩餐方法 System.out.println(zhangdan); } else if (choose == 0) { System.out.println("谢谢惠顾"); break; //输入 0 跳出 循环 } } } }

 

/*英文水平低,就随便用了个名字。这个Business类(业务类)用来统计租车天数,金额,载货量,载客量,在main方法中会新建它的对象来实现*/
public
class Business { int allMoney = 0; //对于 总的统计的变量 先赋值 int allHuo = 0; int allPeople = 0; int allDay; int momey[] = new int[] { 100, 300, 150, 350, 200 }; int liang[][] = new int[][] { { 5, 0 }, { 30, 0 }, { 0, 5 }, { 0, 35 }, { 5, 5 } }; static Scanner scanner = new Scanner(System.in); public static void welcome() { System.out.println("欢迎你来到答答租车系统,输入 1 租车,输入 0 退出 "); } public String jisuan(int day) { allDay = day; System.out.println("请输入租车数量:"); int number = scanner.nextInt(); for (int i = 0; i < number; i++) { System.out.println("请输入第" + (i + 1) + "辆车序号"); int s = scanner.nextInt(); allMoney += momey[s - 1]; for (int j = s; j <= s; j++) { allPeople += liang[s - 1][0]; allHuo += liang[s - 1][1]; } } allMoney = allMoney * allDay; return "租车天数:"+ allDay + " 总金额:"+ allMoney + " 总载货量:"+ allHuo +" 总载人量:" + allPeople; } }

 


没有异常处理,只是简单的输入输出,谢谢大家。


posted on 2017-01-05 11:22  interestOrMoney  阅读(315)  评论(0编辑  收藏  举报