Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize House Rental system

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use Java to realize the housing rental system". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

HouseView

House_Sevice

House

App

Summary

HouseViewpackage house.view;import house.model.House;import house.sevice.house_Sevice;import house.tool.Utility;import java.util.Scanner;/** * 1 display interface * 2 receives user input * 3 calls houseService to complete various inputs of housing information * / public class houseView {private boolean loop=true; private char key='' / / write addhouse (), receive input, create addhouse object, call add method public void addhouse () {System.out.println ("- add house -"); System.out.print ("name:"); String name=Utility.readString (6); System.out.print ("phone:"); int phone=Utility.readInt (12) System.out.print ("address:"); String address=Utility.readString (14); System.out.print ("monthly rent:"); int rent=Utility.readInt (); System.out.print ("status:"); String state=Utility.readString (3); / / Note that id is the House newhouse=new House assigned by the system. If (house_sevice.add (newhouse)) {System.out.println ("add house succeeded");} else {System.out.println ("failed to add house");}} / / find house public void find () {System.out.println ("- find house -") System.out.print ("Please enter the house number you are looking for:"); int f=Utility.readInt (); if (house_sevice.Find (f) = = null) {System.out.println ("No information about the house found") } else {System.out.println ("numbered landlord\ t phone\ t address\ t monthly rent\ t status (rental / non-rental)\ n" + house_sevice.Find (f)) }} / / write the delhosue method, receive the input id, call service's del method public void delhouse () {System.out.println ("- delete house -"); System.out.print ("Please enter the house number to be deleted (- 1 exit):"); int delid=Utility.readInt () If (delid==-1) {System.out.println ("- exit delete -"); return;} char c=Utility.readConfirmSelection () / / call this method and loop until the user enters y or n if (cased deleted successfully Y') {if (house_sevice.del (delid)) {System.out.println ("- deleted successfully -") } else {System.out.println ("- deletion failed -");}} else {System.out.println ("- exit deletion -"); return }} / / modify housing information public void update () {System.out.println ("- modify housing information -"); System.out.print ("Please enter the house number to be modified (- 1 exit):"); int up=Utility.readInt () If (up==-1) {System.out.println ("- exit modification -"); return;} house_sevice.updata (up); System.out.println ("- modified successfully -") } / / display the housing list private house_Sevice house_sevice=new house_Sevice (10); / / set the array size to 10 public void listHouse () {System.out.println ("- House Rental list -"); System.out.println ("numbered landlord\ t phone\ t address\ t monthly rent\ t status (rental / non-rental)") House [] houses=house_sevice.list (); / / get information about all houses for (int I = 0; I

< houses.length; i++) { if(houses[i]==null){ break; } System.out.println(houses[i]); } } //主菜单 public void main_menu(){ do{ System.out.println("---------房屋出租系统-------"); System.out.println("\t\t1 新 增 房 源"); System.out.println("\t\t2 查 找 房 屋"); System.out.println("\t\t3 删 除 房 屋"); System.out.println("\t\t4 修 改 房 屋 信 息"); System.out.println("\t\t5 房 屋 列 表"); System.out.println("\t\t6 退 出"); System.out.print("请输入你的选择:"); key= Utility.readChar(); switch (key){ case '1': addhouse(); break; case '2': find(); break; case '3': delhouse(); break; case '4': update(); break; case '5': listHouse(); break; case '6': char n=Utility.readConfirmSelection(); if(n=='Y'){ loop=false; } break; default: System.out.println("输入错误!"); } }while (loop); }}house_Sevicepackage house.sevice;import house.model.House;import house.tool.Utility;/** * house_Sevice.java 类 [业务层] * //定义house[],保存house对象 * 1 响应houseView的调用 * 2 完成对房屋信息的各种操作(增删改查) */public class house_Sevice { private House[] houses;//保存house对象 private int nums=3;//记录数组内的房屋个数 private int count=3;//id //初始化房屋列表 public house_Sevice(int size){//传入数组大小 houses=new House[size]; houses[0]=new House(1,"Morty",1020,"纽约",111,"未出租"); houses[1]=new House(2,"莱月昴",1021,"东京",222,"未出租"); houses[2]=new House(3,"李星云",1022,"洛阳",333,"未出租"); } public House[] list(){ return houses;//因为重写了tostring } //添加房屋信息 public boolean add(House newhouse){ if(nums==houses.length){ return false; }else { houses[nums++]=newhouse; newhouse.setId(++count);//id自增长机制,更新newhouse的id return true; } } //删除房屋 public boolean del(int Id){ //找到要删除房屋信息元素对应的下标 int index=-1; for (int i = 0; i < nums; i++) { if(Id==houses[i].getId()){ index=i; } } if(index==-1){ return false; } for (int i = index; i < houses.length-1; i++) { houses[i]=houses[i+1];//将该位置之后的元素前移覆盖 } houses[--nums]=null;//将数组长度减一并将最后一个元素置空 return true; } //查找房屋 public House Find(int id){ //找到要查找房屋信息元素对应的下标 for (int i = 0; i < nums; i++) { if(id==houses[i].getId()){ return houses[i]; } } return null; } //修改房屋信息 public void updata(int up){ House house=Find(up); if(house==null){ System.out.println("该房屋不存在"); }else { System.out.print("姓名:("+house.getName()+"):"); String name= Utility.readString(8,"");//用户如果直接回车代表不修改,默认值为"" if(!name.equals("")){ house.setName(name);//将用户输入的name覆盖原来的name } System.out.print("手机号:("+house.getPhone()+"):"); int phone=Utility.readInt(0);//用户如果直接回车代表不修改,默认值为0 if(!(phone==0)){ house.setPhone(phone);//将用户输入的name覆盖原来的name } System.out.print("地址:("+house.getAddress()+"):"); String address=Utility.readString(8,""); if(!address.equals("")){ house.setAddress(address); } System.out.print("月租:("+house.getRent()+"):"); int rent=Utility.readInt(0); if(!(rent==0)){ house.setRent(rent); } System.out.print("状态:("+house.getState()+"):"); String state=Utility.readString(8,""); if(!state.equals("")){ house.setState(state);// } } }}Housepackage house.model;/** * house类的对象表示一个房屋的信息 */public class House { //编号 房主 电话 地址 月租 状态(出租/未出租) private int id; private String name; private int phone; private String address; private int rent; private String state; public House(int id, String name, int 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 int getPhone() { return phone; } public void setPhone(int 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; } @Override public String toString() { return id +"\t" +name +"\t" +phone +"\t" +address +"\t\t"+rent +"\t\t"+state; }}Apppackage house.view;import house.model.House;public class App { public static void main(String[] args) { //创建View对象,并显示主菜单,是整个程序的入口 new houseView().main_menu();//创建一个虚拟对象调用显示主菜单的方法 System.out.println("程序已退出"); }}

"how to use Java to achieve housing rental system" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report