需求分析:
1.需要显示页面
2.需要定义一个房屋类的数组对房屋进行存储
3.需要增删查改等功能
大概设计:
1.界面显示类(class View)
(1).显示菜单以及各个功能的实现
(2).接收用户的输入
public class View {
private boolean loop=true;
private int key;
Scanner in=new Scanner(System.in);
private Crud houseservice=new Crud(10);
public void mainMenu(){
do {
System.out.println("\n----------- 房屋出租系统菜单 -----------" +
"\n\t1.新增房源" +
"\n\t2.查找房屋" +
"\n\t3.删除房屋信息" +
"\n\t4.修改房屋信息" +
"\n\t5.房屋列表" +
"\n\t6.退出");
key=in.nextInt();
switch (key){
case 1:
System.out.println("-----新增房源------");
AddHouse();
break;
case 2:
System.out.println("-----查找房屋------");
FindHouse();
break;
case 3:
System.out.println("-----删除房屋------");
DeleteView();
break;
case 4:
System.out.println("-----修改房屋信息------");
XiugaiHouse();
break;
case 5:
System.out.println("-----房屋列表------");
listHouses();
break;
case 6:
System.out.println("-----退出------");
loop=false;
break;
}
}while (loop);
}
public void listHouses(){//房屋界面
System.out.println("编号\t房主\t电话\t地址\t月租\t状态");
House[] houses=houseservice.list();
for(int i=0;i<houses.length;i++){
if(houses[i]==null){
break;
}
System.out.println(houses[i]);
}
}
public void AddHouse(){//界面,接受用户输入
String name="";
String phone="";
String adress="";
int rent=0;
String state="";
int IdCounter=1;
System.out.println("姓名:");
name =in.next();
System.out.println("电话:");
phone =in.next();
System.out.println("地址:");
adress =in.next();
System.out.println("租金:");
rent=in.nextInt();
System.out.println("状态:");
state=in.next();
House house = new House(IdCounter,name,phone,adress,rent,state);
IdCounter++;
if (houseservice.add(house)){
System.out.println("\t房屋添加成功");
}else{
System.out.println("\t添加房屋失败");
}
}
public void DeleteView(){
System.out.println("请选择待删除的房屋编号:(-1退出)");
int DelId;
DelId=in.nextInt();
if(DelId==-1){
System.out.println("\t放弃删除房屋信息");
return;
}
if(houseservice.DeleteHouse(DelId)){
System.out.println("\t删除成功");
}else{
System.out.println("\t删除失败(没有相关id的房屋)");
}
}
public void XiugaiHouse(){
System.out.println("\t请输入要修改的房屋id:");
int id=in.nextInt();
House house=houseservice.Find(id);
if(house!=null){
System.out.println(house);
System.out.println("姓名:");
String name=in.next();
if(!name.equals("")){
house.setName(name);
}
System.out.println("电话:");
String phone=in.next();
if(!phone.equals("")){
house.setPhone(phone);
}
System.out.println("地址:");
String adress=in.next();
if(!adress.equals("")){
house.setAdress(adress);
}
System.out.println("租金:");
int rent=in.nextInt();
house.setRent(rent);
System.out.println("状态:");
String state=in.next();
if(!state.equals("")){
house.setState(state);
}
System.out.println("修改房屋信息成功");
}
else{
System.out.println("\t要修改的房屋信息不存在");
return;
}
}
public void FindHouse(){
System.out.println("\t请输入要查找的ID");
int id=in.nextInt();
House house=houseservice.Find(id);
if(house!=null){
System.out.println(house);
}
else {
System.out.println("\t查找失败(没有相关id的房屋)");
return;
}
}
}
2.房屋(class House)
(1).定义房屋的属性,以及构造方法
public class House {
private int id;
private String name;
private String phone;
private String adress;
private int rent;
private String state;
public House(int id,String name, String phone, String adress, int rent, String state) {
this.name = name;
this.id = id;
this.phone = phone;
this.adress = adress;
this.rent = rent;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
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" + adress +
"\t" + rent +
"\t" + state;
}
}
3.增删查找类(classCrud)
(1).实现增删改查等功能
public class Crud {
private House[] houses;//保存house对象
private int housenumber=0;
public Crud(int size){
houses=new House[size];
}
public House[] list(){//返回所有的房屋信息
return houses;
}
public boolean add(House Newhouse){
if(housenumber>=houses.length){
System.out.println("数组已满,不能添加");
return false;
}
houses[housenumber++]=Newhouse;
return true;
}
public boolean DeleteHouse(int Delid){
int index=-1;
for (int i=0;i<housenumber;i++){
if(Delid==houses[i].getId()){
index=i;
}
}
if(index==-1){
return false;
}else {
for(int i=index;i<housenumber-1;i++){//数组往前移动,实现数组的删除
houses[i]=houses[i+1];
}
houses[--housenumber] =null;//数组下标从0开始,房屋数量从1开始
return true;
}
}
public House Find(int Id){
for(int i=0;i<housenumber;i++) {
if (houses[i].getId() == Id) {
return houses[i];
}
}
return null;
}
}
4.App入口类
public class App {
public static void main(String[] args) {
View view=new View();
view.mainMenu();
}
}
代码实现显示:
遇到的问题总结:
无法连续输入问题,连续使用nextLine()无法等待用户输入
改进为next()后可以等待用户输入