100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 韩顺平Java自学笔记 项目 房屋出租

韩顺平Java自学笔记 项目 房屋出租

时间:2022-10-10 11:31:34

相关推荐

韩顺平Java自学笔记 项目 房屋出租

目录

一。需求分析

1.需求说明

2。界面要求

​编辑

3.系统设计

4.功能实现的顺序

二。使用到的类

1.工具类(Utility)

2.House类

3.HouseView类

4.HouseRentApp类

5.HouseService类 *

三。具体功能的实现

1.主菜单

2.列表

3.添加

4.删除

5.退出

6.查找

7.修改

四.总结

1.项目整体的思路 *

2.学习到的思想

一。需求分析

1.需求说明

2。界面要求

主菜单(子菜单放到了后面)

3.系统设计

框架图(明确类的设计和类之间的调用关系)

注释:设计思路(自己的理解)

启动类-----界面类-------处理类--------对象类

启动类:整个程序的入口,就像是开机键。

界面类:给用户看的,方便用户的操作。

处理类:用于处理界面传过来的数据,完成某种功能,处理业务。

对象类:就是最核心的要处理的数据是什么样子的。例如:房屋出租的对象是房屋。

工具类:单纯是为了方便开发,将一些常用的代码段放进去。

处理和对象感觉像是类当中的方法和属性

4.功能实现的顺序

最开始写的类是对象类,程序的设计应当是先总体框架的设计之后,再自下而上的实现。

实现的思路:

二。使用到的类

1.工具类(Utility)

工具类的作用:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。*/import com.houserent.domain.House;import java.util.*;/***/public class Utility {//静态属性。。。private static Scanner scanner = new Scanner(System.in);/*** 功能:读取键盘输入的一个菜单选项,值:1——5的范围* @return 1——5*/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;}/*** 功能:读取键盘输入的一个字符* @return 一个字符*/public static char readChar() {String str = readKeyBoard(1, false);//就是一个字符return str.charAt(0);}/*** 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符* @param defaultValue 指定的默认值* @return 默认值或输入的字符*/public static char readChar(char defaultValue) {String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符return (str.length() == 0) ? defaultValue : str.charAt(0);}/*** 功能:读取键盘输入的整型,长度小于2位* @return 整数*/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;}/*** 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数* @param defaultValue 指定的默认值* @return 整数或默认值*/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;}/*** 功能:读取键盘输入的指定长度的字符串* @param limit 限制的长度* @return 指定长度的字符串*/public static String readString(int limit) {return readKeyBoard(limit, false);}/*** 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串* @param limit 限制的长度* @param defaultValue 指定的默认值* @return 指定长度的字符串*/public static String readString(int limit, String defaultValue) {String str = readKeyBoard(limit, true);return str.equals("")? defaultValue : str;}/*** 功能:读取键盘输入的确认选项,Y或N* 将小的功能,封装到一个方法中.* @return Y或N*/public static char readConfirmSelection() {System.out.println("请输入你的选择(Y/N): 请小心选择");char c;for (; ; ) {//无限循环//在这里,将接受到字符,转成了大写字母//y => Y n=>NString str = readKeyBoard(1, false).toUpperCase();c = str.charAt(0);if (c == 'Y' || c == 'N') {break;} else {System.out.print("选择错误,请重新输入:");}}return c;}/*** 功能: 读取一个字符串* @param limit 读取的长度* @param blankReturn 如果为true ,表示 可以读空字符串。* 如果为false表示 不能读空字符串。**如果输入为空,或者输入大于limit的长度,就会提示重新输入。* @return*/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;}}

注释:对其中方法的讲解

Utility.readString(8,"nihao");

前一个参数是规范用户操作,输入的长度不能超过8,后面是默认值,当用户什么都没输入,直接回车时,返回默认值。

Utility.readInt(-1)

要求输入一个int类型数据,长度小于10,参数为直接回车之后的默认返回值

(其他方法的作用看注释吧)

2.House类

主要是属性的分析,操作由其他类来实现

代码(toStrng方法已经修改)

public class House {private int id;private String name;private String phone;private String address;private int rent;private String state;public House() {}public House(int id, String name, String phone, String address, int rent, String state) {this.id = id;this.name = name;this.phone = phone;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 String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}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;}@Overridepublic String toString() {return id +"\t\t" + name +"\t" + phone +"\t\t" + address +"\t" + rent +"\t" + state ;}}

注意:有参构造参数的顺序需要注意,不然会和老师写的不一样,使用构造方法的时候。

3.HouseView类

直接是最终状态的代码,不想再往回改了

import com.houserent.domain.House;import com.houserent.serivice.HouseService;import com.houserent.util.Utility;import javax.swing.*;public class HouseView {private boolean loop = true;private char key = ' ';private HouseService houseService = new HouseService(10);House[] houses = houseService.list();public void exit(){char c = Utility.readConfirmSelection();if (c == 'Y') {loop=false;}}public void findHouse(){System.out.println("=======查找房屋信息========");System.out.println("请输入要查找的Id");int findId = Utility.readInt();House house = houseService.findById(findId);if (house==null){System.out.println("======没有找到您要找的房间=======");} else {System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");System.out.println(house);}}public void update(){System.out.println("=======修改房屋信息======");System.out.println("请选择待修改的房屋编号(-1表示退出)");int update = Utility.readInt();if (update==-1){System.out.println("放弃修改信息");return;}House house = houseService.findById(update);if (house==null){System.out.println("======您要修改的房间不存在=======");return;}System.out.println("姓名("+house.getName()+"): ");String name = Utility.readString(8, "");if (!"".equals(name)){house.setName(name);}System.out.println("电话("+house.getPhone()+"): ");String phone = Utility.readString(12, "");if (!"".equals(phone)){house.setPhone(phone);}System.out.println("地址("+house.getAddress()+"): ");String address= Utility.readString(18, "");if (!"".equals(address)){house.setAddress(address);}System.out.println("租金("+house.getRent()+"): ");int sent = Utility.readInt(-1);if (sent!=-1){house.setRent(sent);}System.out.println("状态("+house.getState()+"): ");String state= Utility.readString(3, "");if (!"".equals(state)){house.setState(state);}System.out.println("修改房屋出租数据成功");}public void addHouse(){System.out.println("================添加房屋=============");System.out.println("姓名");String name = Utility.readString(8);System.out.println("电话");String phone = Utility.readString(12);System.out.println("地址");String address = Utility.readString(16);System.out.println("月租");int rent = Utility.readInt();System.out.println("状态");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)) {System.out.println("========房屋添加成功========");}else{System.out.println("========房屋添加失败=========");}}public void delHouse(){System.out.println("============添加房屋信息===========");System.out.println("请输入房间编号。(-1退出)");int delId = Utility.readInt();if(delId==-1){System.out.println("========放弃删除房屋信息========");return;}char c = Utility.readConfirmSelection();if (c=='Y'){if (houseService.del(delId)){System.out.println("========删除房屋信息成功=======");}else {System.out.println("========输入的房间编号不存在=======");}}else{System.out.println("========放弃删除房屋信息========");}}public void houseList(){System.out.println("==================房屋列表==============");System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");for (int i=0;i<houses.length;i++){if (houses[i]==null){break;}System.out.println(houses[i]);}System.out.println("=================房屋列表完毕=============");}public void mainMenu(){do{System.out.println("\n===========房屋出租系统菜单===========");System.out.println("\t\t\t 1.新增 房源");System.out.println("\t\t\t 2.查找 房屋");System.out.println("\t\t\t 3.删除房屋信息");System.out.println("\t\t\t 4.修改房屋信息");System.out.println("\t\t\t 5.房屋 列表");System.out.println("\t\t\t 6.退出");System.out.println("请输入你的选择(1-6)");key= Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':delHouse();break;case '4':update();break;case '5':houseList();break;case '6':exit();break;}}while (loop);}}

4.HouseRentApp类

启动类,一般写好就不用改了。

public class HouseRentApp {public static void main(String[] args) {new HouseView().mainMenu();System.out.println("======你退出了房屋出租系统======");}}

5.HouseService类 *

public class HouseService {private House[] houses;private int houseNumbers = 1;private int idCounter =1;public HouseService(int size){houses = new House[size];houses[0] = new House(1,"并地下红","113","阿里布达",300,"未出租");}public House findById(int findId){for (int i=0;i<houseNumbers;i++){if (findId==houses[i].getId());return houses[i];}return null;}public boolean del(int delId){int index = -1;for (int i=0;i<houseNumbers;i++){if(delId==houses[i].getId()){index = i;}}if (index==-1){return false;}for (int i = index;i<houseNumbers-1;i++){houses[i]=houses[i+1];}houses[--houseNumbers] = null;return true;}public boolean add (House house){if (houseNumbers>=houses.length){System.out.println("数组已满,不能再添加了。。");return false;}houses[houseNumbers++] = house;house.setId(++idCounter);return true;}public House[] list(){return houses;}}

三。具体功能的实现

1.主菜单

界面模样

使用Utility当中的方法来接收数据。使用switch/case来处理用户选择的选项

public void mainMenu(){do{System.out.println("\n===========房屋出租系统菜单===========");System.out.println("\t\t\t 1.新增 房源");System.out.println("\t\t\t 2.查找 房屋");System.out.println("\t\t\t 3.删除房屋信息");System.out.println("\t\t\t 4.修改房屋信息");System.out.println("\t\t\t 5.房屋 列表");System.out.println("\t\t\t 6.退出");System.out.println("请输入你的选择(1-6)");key= Utility.readChar();switch (key){case '1':addHouse();break;case '2':findHouse();break;case '3':delHouse();break;case '4':update();break;case '5':houseList();break;case '6':exit();break;}}while (loop);}

2.列表

页面

涉及两个类,一个类进行处理,并返回处理结果,一个类负责接受处理的结果,并进行打印。

HouseService类当中创建用于储存操作对象的数组,并进行默认设置

private House[] houses;public HouseService(int size){houses = new House[size];houses[0] = new House(1,"并地下红","113","阿里布达",300,"未出租");}

HouseView类进行输出(通过遍历的方式来打印所有的房屋信息)

private HouseService houseService = new HouseService(10);public void houseList(){System.out.println("==================房屋列表==============");System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");for (int i=0;i<houses.length;i++){if (houses[i]==null){break;}System.out.println(houses[i]);}System.out.println("=================房屋列表完毕=============");}

思路:先创HouseService类对象,再调用对象方法,接收HouseService类返回的结果。

3.添加

界面

代码实现思路

HouseView类当中的代码

public void addHouse(){System.out.println("================添加房屋=============");System.out.println("姓名");String name = Utility.readString(8);System.out.println("电话");String phone = Utility.readString(12);System.out.println("地址");String address = Utility.readString(16);System.out.println("月租");int rent = Utility.readInt();System.out.println("状态");String state = Utility.readString(3);House house = new House(0, name, phone, address, rent, state);if (houseService.add(house)) {System.out.println("========房屋添加成功========");}else{System.out.println("========房屋添加失败=========");}}

提示:当中的很多方法都是Utility当中的方法(用来接受键盘输入信息)

HouseService类当中的代码

public boolean add (House house){if (houseNumbers>=houses.length){System.out.println("数组已满,不能再添加了。。");return false;}houses[houseNumbers++] = house;house.setId(++idCounter);return true;}

提示:add方法是用来判断数组是否满了的(还可不可以添加房屋),idCounter是使房间id自增的。

4.删除

界面

设计思路:

HouseService代码

public boolean del(int delId){int index = -1;for (int i=0;i<houseNumbers;i++){if(delId==houses[i].getId()){index = i;}}if (index==-1){return false;}for (int i = index;i<houseNumbers-1;i++){houses[i]=houses[i+1];}houses[--houseNumbers] = null;return true;}

思路:找不到房屋返回false,找的到就进行删除并返回true。第一个for循环遍历房屋对象id,看看存不存在要找的对象。第二个for是将删除对对象后面的对象整体迁移,以补充空缺。

HouseView代码

public void delHouse(){System.out.println("============添加房屋信息===========");System.out.println("请输入房间编号。(-1退出)");int delId = Utility.readInt();if(delId==-1){System.out.println("========放弃删除房屋信息========");return;}char c = Utility.readConfirmSelection();if (c=='Y'){if (houseService.del(delId)){System.out.println("========删除房屋信息成功=======");}else {System.out.println("========输入的房间编号不存在=======");}}else{System.out.println("========放弃删除房屋信息========");}}

5.退出

功能说明

实现代码比较简单(使用到了工具类当中的方法)

只有HouseView类当中有代码(exit)

public void exit(){char c = Utility.readConfirmSelection();if (c == 'Y') {loop=false;}}

6.查找

界面

实现思路

HouseService代码(findById)

public House findById(int findId){for (int i=0;i<houseNumbers;i++){if (findId==houses[i].getId());return houses[i];}return null;}

HouseView 代码(findHouse)

public void findHouse(){System.out.println("=======查找房屋信息========");System.out.println("请输入要查找的Id");int findId = Utility.readInt();House house = houseService.findById(findId);if (house==null){System.out.println("======没有找到您要找的房间=======");} else {System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");System.out.println(house);}}

7.修改

界面

实现思路

HouseView代码

public void update(){System.out.println("=======修改房屋信息======");System.out.println("请选择待修改的房屋编号(-1表示退出)");int update = Utility.readInt();if (update==-1){System.out.println("放弃修改信息");return;}House house = houseService.findById(update);if (house==null){System.out.println("======您要修改的房间不存在=======");return;}System.out.println("姓名("+house.getName()+"): ");String name = Utility.readString(8, "");if (!"".equals(name)){house.setName(name);}System.out.println("电话("+house.getPhone()+"): ");String phone = Utility.readString(12, "");if (!"".equals(phone)){house.setPhone(phone);}System.out.println("地址("+house.getAddress()+"): ");String address= Utility.readString(18, "");if (!"".equals(address)){house.setAddress(address);}System.out.println("租金("+house.getRent()+"): ");int sent = Utility.readInt(-1);if (sent!=-1){house.setRent(sent);}System.out.println("状态("+house.getState()+"): ");String state= Utility.readString(3, "");if (!"".equals(state)){house.setState(state);}System.out.println("修改房屋出租数据成功");}

使用到了之前的方法写的查询方法:houseService.findById(update)用来查询自己要修改的房间是不是存在的。

四.总结

1.项目整体的思路 *

2.学习到的思想

界面:界面的设计(这应该是前端关注的)

类:有那些类,这些类具体实现的是什么功能。类与类之间的调用关系又是怎么样的。(记得画类图,一个类当中的一个功能(业务)为一个方法)。

方法:比类的设计更为具体。主要涉及到具体实现什么功能,和如何实现。(方法成对,一个用于处理,一个用于接收)

数据的设计:设计的属性负责什么功能,是用于记录对象的数量的,还是用于判断循环命令是否执行的判断。

开发视角

设计:设计是从用户的角度出发,自上而下

实现:是从开发者的角度出发,自下而上

注释:*为我认为的重点标志

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。