栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习第三十二天<房屋出租项目><类变量和类方法>

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习第三十二天<房屋出租项目><类变量和类方法>

房屋出租项目 工具包
package chapter09.D10房屋出租项目.工具包;
​
​
​

​
import java.util.*;

public class Utility {
    //静态属性。。。
    private static Scanner scanner = new Scanner(System.in);
​
​
    
    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
​
    
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    
​
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
​
    
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, false);//一个整数,长度<=10位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
​
            //异常处理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
​
    
​
    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }
​
    
​
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }
​
​
    
    public static char readConfirmSelection() {
        System.out.println("请输入你的选择(Y/N): 请小心选择");
        char c;
        for (; ; ) {//无限循环
            //在这里,将接受到字符,转成了大写字母
            //y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }
​
    
    private static String readKeyBoard(int limit, boolean blankReturn) {
​
        //定义了字符串
        String line = "";
​
        //scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//读取这一行
​
            //如果line.length=0, 即用户没有输入任何内容,直接回车
            if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串,必须输入内容
            }
​
            //如果用户输入的内容大于了 limit,就提示重写输入
            //如果用户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }
​
        return line;
    }
}

显示界面
package chapter09.D10房屋出租项目.显示界面;
​
import chapter09.D10房屋出租项目.业务处理.HouseService;
import chapter09.D10房屋出租项目.工具包.Utility;
import chapter09.D10房屋出租项目.数据库.House;
​
public class HouseView {
    boolean flag = true;
    HouseService houseService = new HouseService();//调用业务层方法
    public void view(int n){
        houseService.setnums(n);
        do {
            System.out.println("---------房屋出租系统--------");
            System.out.println("tt1 新增房源");
            System.out.println("tt2 查找房源");
            System.out.println("tt3 删除房源");
            System.out.println("tt4 修改房源");
            System.out.println("tt5 房源列表");
            System.out.println("tt6 退出");
            System.out.println("请输入(1-6):");
            int s= Utility.readInt();
            switch (s){
                case 1:
                    System.out.println("新增房源");
                    addhouse();
                    break;
                case 2:
                    System.out.println("查找房源");
                    searchhouse();
                    break;
                case 3:
                    System.out.println("删除房源");
                    deletehouse();
                    break;
                case 4:
                    System.out.println("修改房源");
                    changehouse();
                    break;
                case 5:
                    System.out.println("房源列表");
                    System.out.println("编号t房东姓名t房东电话t地址t租金t状态");
                    hoselist();
                    break;
                case 6:
                    exit();
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }while (flag);
    }
​
    public void hoselist(){
        House []hs= houseService.list();
        for (int i = 0; i  

数据库
package chapter09.D10房屋出租项目.数据库;
​
public class House {
    private int ID;
    private String name;
    private int tel;
    private String address;
    private int rent;
    private String state;
​
    public House(String name, int tel, String address, int rent, String state) {
        this.name = name;
        this.tel = tel;
        this.address = address;
        this.rent = rent;
        this.state = state;
    }
​
    public int getID() {
        return ID;
    }
​
    public void setID(int ID) {
        this.ID = ID;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public int getTel() {
        return tel;
    }
​
    public void setTel(int tel) {
        this.tel = tel;
    }
​
    public String getAddress() {
        return address;
    }
​
    public void setAddress(String address) {
        this.address = address;
    }
​
    public int getRent() {
        return rent;
    }
​
    public void setRent(int rent) {
        this.rent = rent;
    }
​
    public String getState() {
        return state;
    }
​
    public void setState(String state) {
        this.state = state;
    }
​
    @Override
    public String toString() {
        return ID +"t"+ name +"t"+ tel +"t"+ address +"t"+ rent +"t" + state ;
​
    }
}

业务处理
package chapter09.D10房屋出租项目.业务处理;
​
import chapter09.D10房屋出租项目.工具包.Utility;
import chapter09.D10房屋出租项目.数据库.House;
​
public class HouseService {
    private House []houses;//放外面,方法都能调用到
    public int housenums=0;
    public void add(House h){
        if (housenums==houses.length){
            System.out.println("超出最大限制,添加失败!");
        }else {
            houses[housenums]=h;
            houses[housenums].setID(housenums+1);//自动生成编号
            housenums++;
            System.out.println("添加成功!");
        }
    }
    public void delete(int d){
        int index=-1;
        for (int i = 0; i  

调用主类
package chapter09.D10房屋出租项目;
​
import chapter09.D10房屋出租项目.工具包.Utility;
import chapter09.D10房屋出租项目.显示界面.HouseView;
​
public class HouseRentApp {
    public static void main(String[] args) {
       new HouseView().view(4);
    }
}

类变量和类方法
package chapter10.D1类变量和类方法;
//static(静态变量)类变量是同一个类所有对象共享的,类变量在类加载的时候就生成了
public class ChildGame {
    public static void main(String[] args) {
​
        Child c1 = new Child("xx");
        c1.join();
        Child.count++;//类变量在类加载的时候就生成,所以没有创建对象就可以调用
        //c1.conut++;
        Child c2 = new Child("yy");
        c2.join();
        c2.count++;
        Child c3 = new Child("zz");
        c3.join();
        Child.count++;
        System.out.println(Child.count);//等同c1.count
        System.out.println(c2.count);//等同c1.count
​
​
    }
      
}
class Child{
    private String name;
    public static int count=0;//类变量遵守修饰符访问权限
    public static int h;
    public Child(String name){
        this.name=name;
    }
    public void join(){
        System.out.println(name+"加入了游戏...");
    }
}

package chapter10.D1类变量和类方法;
​
public class StaticMethod {
    public static void main(String[] args) {
        Stu xx = new Stu("xx");
        Stu yy = new Stu("yy");
        xx.payFee(200);
        yy.payFee(100);
        Stu.showFee();
    }
}
class Stu{
    private String name;
    private static double fee=0;
    public int n=10;
​
    public Stu(String name) {
        this.name = name;
    }
    public void payFee(double fee){
        Stu.fee+=fee;
    }
    public static void showFee(){//类方法随类的加载而加载,无this参数,普通方法隐含this参数
        System.out.println(Stu.fee);
        //System.out.println(n);//静态方法只能调静态属性和方法,普通方法不管静态不静态全可调用
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777997.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号