【java】自定义封装工具和继承多态练习(自留)

Java异常处理小练习

  • 封装一个键盘输入通用工具类
    可以接受键盘命令行输入的各种数据、整数、小数(单双精度)、字符串、字符、读取一行等,有异常处理
package com.exception.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class MyException {
    public static void main(String[] args) throws DataHouseException {
        int ch=1;
        while(ch!=0) {
            ch = choiceStyle();
            try{
                Objects.GetBalanceInfo(ch);
            }catch(Exception e){
                System.out.println("something wrong:"+e);
                System.out.println("cause:"+e.getCause());
            }
            }

    }

    static int choiceStyle() throws DataHouseException {
        System.out.println("选择所输入的数据类型:");
        System.out.println("1.int 2.double 3.boolean 4.字符串 5.字符 6.读取一行 0.退出");
        Scanner sc=new Scanner(System.in);
        int ch=sc.nextInt();
        if (ch >= 1 && ch <= 6) {
            return ch;
        }else{
            throw new DataHouseException("输入范围1-6");
        }
    }

}


class DataHouse {
    public static void FindData(int ch) throws DataHouseException {
        if (ch >= 1 && ch <= 3) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入int/double/boolean:");
            while (sc.hasNext()) {    //判断是否有输入
                switch (ch) {
                    case 1:
                        if (sc.hasNextInt()) {
                            System.out.println(sc.nextInt()+" :int");
                            break;
                        } else{
                            throw new DataHouseException("不是int类型");
                        }

                    case 2:
                        if (sc.hasNextDouble()) {
                            System.out.println(sc.nextInt()+" :double");break;
                        } else{
                            throw new DataHouseException("不是double类型");
                        }

                    case 3:
                        if (sc.hasNextBoolean()) {
                            System.out.println(sc.nextInt()+" :boolean"); break;
                        } else{
                            throw new DataHouseException("不是boolean类型");
                        }

                }
            }
            }else{
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                String inputLine = null;
                System.out.println("请输入字符串/字符/一行:");
                try {
                    inputLine = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("输入的文本是:" + inputLine);
            }
        }
    }

class MyAppException extends Exception{
    public MyAppException(String message){
        super(message);
    }
    public MyAppException(String message,Exception cause){
        super(message,cause);
    }
    
class Objects{
    public static void GetBalanceInfo(int ch) throws MyAppException {
        try{
            DataHouse.FindData(ch);
        }catch(DataHouseException e){
            throw new MyAppException("invalid ch",e);
        }
    }
}

class DataHouseException extends Exception{
    public DataHouseException(String message){
        super(message);
    }
}

}


  • 高速公路收费系统

收费公路收费仿真模拟程序。

高速公路收费标准如下:

7座以下小汽车:0.5元/公里

大巴车:1元/公里

编写一个收费程序,可以针对不同的车型进行收费。

该程序运行后,等待用户键盘输入,输入0则退出程序,输入1则随机生成一辆来车(可能是小汽车,也可能是大巴车,车型随机,行驶里程随机(1-500公里以内),车牌号码随机(假设所有车牌都是沪A开头,后面5位任意阿拉伯数字),然后进行计算费用,输出该车辆的类型、车牌号、行驶里程和缴费金额。此过程反复循环,直到用户输入0则退出,并输出统计所收费的车辆数量,所有总收费金额合计。

要求,整个程序应该使用Java的多态实现,具体如下:

1、请用面向对象的思想为车辆设计合适的类以及继承关系结构,车辆收费金额的计算应该设计成一个方法以便实现override方法动态绑定。

2、编写一个类,该类专门用来随机生成车辆对象。

3、编写一个主程序类,该类负责界面显示和人机交互,并通过多态的方式调用2中生成的车辆对象,完成收费操作。在本类中要能够处理各种不同的车型,即使今后假如增加了一个新的车辆类型(如大货车,收费方式也不一样),本类程序也能够在不做任何修改的情况下正常运行。

====================

package com.project;

import java.util.Random;
import java.util.Scanner;

public class BillingSystem {
    public static void main(String[] args){
        double km;//公里数
        double totalBill = 0;//总租赁费用
         RandomCar cars[]=new RandomCar[100];
         Scanner sc=new Scanner(System.in);
         System.out.println("欢迎使用高速公路收费系统");
         System.out.println("1.随机计算");
         System.out.println("0.退出");

         while(true){
             int i=0;
             System.out.println("请选择:");
             int ch= sc.nextInt();
             switch (ch){
                 case 1:
                     cars[i]=new RandomCar();
                     cars[i].show();
                     i++;
                     break;
                 case 0:System.out.println("已收费"+i+"辆车,共计(元)"+totalBill);System.out.println("一路顺风");System.exit(0);
             }
         }
    }

    /**
     * 用来生成随机车辆的对象
     */
    public static class RandomCar{
        Scanner sc=new Scanner(System.in);
        Random r=new Random();
        int key=(int)(r.nextInt(3));
        Cars c;
        
        public void putOne(){
            System.out.println("请输入公里数:");
            double km=sc.nextDouble();
            c.km=km;
            if(key == 1){
                System.out.println("请输入小汽车座位数量");
                int n=sc.nextInt();
                c=new Car(c.getNo(),"小轿车",n);
            }
            if(key ==3 ){
                c=new Bus(c.getNo(), "大巴车");
            }
            if(key == 2){
                System.out.println("请输入大货车吨数");
                int n=sc.nextInt();
                c=new Truck(c.getNo(),"大货车",n);
            }

        }
        
        public void show(){
            System.out.println("该车辆类型为:"+c.getType());
            System.out.println("车牌号为:"+getNo());
            System.out.println("行驶里程:"+c.km);
            System.out.println("缴费金额:"+c.calBill(c.km));
        }
    }

    /**
     * 随机生成子类对象
     * @param key 获取的随即数1或2或3
     * @return 子类对象
     */
    /*public static Cars getInstance(int key){
        switch(key){
            case 1:return new Car();
            case 2:return new Bus();
            case 3:return new Truck();
        }
        return null;
    }*/

    /**
     * 用来随机生成车牌号
     */
    public static StringBuilder getNo(){
        int[] numbers={0,1,2,3,4,5,6,7,8,9};
        Random random=new Random();
        StringBuilder sb=new StringBuilder();
        sb.append("沪A");
        String s="";
        for(int i=0;i<5;i++){
            int next=random.nextInt(10000);
            s+=numbers[next%10];
        }
        return sb.append(s);
    }
}

汽车父类

package com.project;

public class Cars {
    public int seat;
    public int ton;
    public double km;
    private String no;//汽车车牌号
    private String type;//汽车种类
    public Cars(String no,String type){
        this.no=no;
        this.type=type;
    }
    public String getNo(){
        return no;
    }
    public String getType(){
        return type;
    }

    public double calBill(double km) {
        return 0;
    }

    public void charge(){
        System.out.println("高速公路收费标准如下:");
        System.out.println("7座一下小汽车:0.5元/公里  大巴车:1元/公里 其他");
    }
}



大巴车的类

package com.project;

public class Bus extends Cars{
    private double unitPrice=1.0;//计费单位
    private String type;
    public Bus(String no, String type) {
        super(no, "大巴车");
    }
    public double getUnitPrice(){
        return unitPrice;
    }
    public String getType(){
        return type;
    }
    public double calBill(double km){
        return km*1.0;
    }
}

小汽车的类

package com.project;

public class Car extends Cars {
    int seat;//座位数

    public Car(String no, String type,int seat) {
        super(no, "小轿车");
        this.seat=seat;
    }
    public int getSeat(){
        return seat;
    }
    public double calBill(double km){
        if(seat<7){
            return km*0.5;
        }
        else{
            return km*1;
        }
    }
}

大货车的类

package com.project;

public class Truck extends Cars {
    int ton;//货车吨位

    /**
     * 有参构造
     * @param no 汽车牌号
     * @param type  汽车类型
     * @param ton  吨位
     */
    public Truck(String no, String type,int ton) {
        super(no, "大货车");
        this.ton=ton;
    }
    public int getTon(){
        return ton;
    }

    /**
     * 计算货车计费
     * @param km 公里数
     * @return  费用
     */
    public double calBill(double km){
        double v = km * ton * 5;
        return v;
    }
}

  • 自定义菜单工具类
package com.java01.demo;

import java.util.Scanner;

public class Menus {
    Scanner sc=new Scanner(System.in);
    //接受一个菜单的定义
    private String menuName;
    private String[] menuContent;
    private Menus parent;//父菜单
    public Menus(){
    }
    public void setMenuName(String menuName){
        this.menuName=menuName;
    }
    public String getMenuName(){
        return menuName;
    }
    public void setMenu(String[] menu){
        this.menuContent=menu.clone();
    }
    public void setParent(Menus parent){
        this.parent=parent;
    }

    /**
     * 打印显示菜单
     *
     * @return
     */
    public void show(){
        System.out.println("======"+this.getMenuName()+"======");
        for(int i=0;i<this.menuContent.length;i++)
        {
            System.out.println("===="+(i+1)+this.menuContent[i]);
        }
        System.out.println("====0.退出");

    }

    /**
     * 读取键盘选择
     * @return 选择的功能序号
     */
    public int getChoice(){
        System.out.print("请选择:");
        int ch=sc.nextInt();//接收选项
        return ch;
    }

    /**
     * 接收选项并判断选项是否为0
     * 选项不为0则进入循环,选项为0则返回到上级菜单
     * @return 返回选项是否为0
     */
    public boolean returnToTheParent(){
        int ch=this.getChoice();
        if(ch==0) {
            this.parent.show();
            return false;
        }
        return true;
    }
}

接下来对菜单进行调试

package com.java01.demo;

public class MenuDemo {
    public static void main(String[] args) {
        Menus currentMenu=new Menus(); //实例化当前菜单
        Menus parentMenu=new Menus(); //实例化上一级菜单
        //录入菜单名称
        parentMenu.setMenuName("学生选课系统总界面");
        currentMenu.setMenuName("学生选课系统管理员界面");
        //录入菜单内容
        String[] menu1={"管理员身份","学生身份"};
        String[] menu2={"录入课程信息","录入学生信息"};
        parentMenu.setMenu(menu1);
        currentMenu.setMenu(menu2);
        //设置当前菜单的上一级菜单
        currentMenu.setParent(parentMenu);
        //展示菜单(从当前菜单开始)
        currentMenu.show();
        while(true){
            switch(currentMenu.getChoice()){
                case 1:System.out.println("功能1 略");break;
                case 2:System.out.println("功能2 略");break;
                case 0:currentMenu.returnToTheParent();System.exit(0); //输入0返回上一级菜单,退出循环
        }

        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值