没有改bug,大家可以自己试着改哦
package per.llldf.house;
import java.util.*;
public class Main {
public void menu() {
System.out.println("*******************************");
System.out.println(" 房源中心");
System.out.println(" 1--添加房源");
System.out.println(" 2--查看具体房源信息");
System.out.println(" 3--区间查看房源信息");
System.out.println(" 0--退出房源中心");
System.out.println("*******************************");
}
public static void main(String[] args) {
Main lxlx = new Main();
int i = 0, n = 0;
Scanner lx = new Scanner(System.in);
List houseList = new ArrayList();
while (true) {
lxlx.menu();
System.out.println("请输入对应数字进行操作:");
n = lx.nextInt();
if (n == 0) {
System.out.println("结束程序");
break;
}
switch(n) {
case 1:i = lxlx.addHouse(i, houseList);break;
case 2:lxlx.houseInfo(houseList); break;
case 3:lxlx.houseInfoSec(houseList); break;
default:System.out.println("输入数据不匹配,请重新输入");
}
}
}
public int addHouse(int i, List list) {
Scanner lx = new Scanner(System.in);
int m = 0;
String s1, s2, s3;
System.out.println("添加房源");
System.out.println("输入房源编号:");
System.out.println("编号从0开始,并为连续数值,如:0, 1, 2, 3");
m = lx.nextInt();
if (m != i) {
System.out.println("请输入连续的编号!");
return i;
}
System.out.println("输入房源名称:");
s1 = lx.next();
System.out.println("输入房源地址:");
s2 = lx.next();
System.out.println("输入房源户型:");
s3 = lx.next();
House house = new House(m, s1, s2, s3);
list.add(house);
return i + 1;
}
public void houseInfo(List list) {
Scanner lx = new Scanner(System.in);
int m = 0;
System.out.println("全部房源信息");
System.out.println("房源编号 房源名称");
for (int i = 0; i < list.size(); i++)
System.out.println(((House)list.get(i)).getHouseId() + " " + ((House)(list.get(i))).getHouseName());
System.out.println("请输入要查看的具体房源编号:");
m = lx.nextInt();
System.out.println(((House)(list.get(m))).toString());
}
public void houseInfoSec(List list) {
Scanner lx = new Scanner(System.in);
int m = 0, n = 0;
System.out.println("全部房源信息");
System.out.println("房源编号 房源名称");
for (int i = 0; i < list.size(); i++)
System.out.println(((House)list.get(i)).getHouseId() + " " + ((House)(list.get(i))).getHouseName());
System.out.println("请输入要查看房源信息的第一个编号:");
m = lx.nextInt();
System.out.println("请输入要查看房源信息的第二个编号:");
n = lx.nextInt();
for (; m < n; m++)
System.out.println(((House)(list.get(m))).toString());
}
}
package per.llldf.house;
public class House {
private int houseId;
private String houseName;
private String houseAddress;
private String houseType;
public House() {
}
public House(int houseId, String houseName, String houseAddress, String houseType) {
this.setHouseId(houseId);
this.setHouseName(houseName);
this.setHouseAddress(houseAddress);
this.setHouseType(houseType);
}
public int getHouseId() {
return houseId;
}
public void setHouseId(int houseId) {
this.houseId = houseId;
}
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getHouseAddress() {
return houseAddress;
}
public void setHouseAddress(String houseAddress) {
this.houseAddress = houseAddress;
}
public String getHouseType() {
return houseType;
}
public void setHouseType(String houseType) {
this.houseType = houseType;
}
@Override
public String toString() {
String str = "房源编号:" + this.getHouseId() + "\n房源名称:" + this.getHouseName() + "\n房源地址:" + this.getHouseAddress() + "\n房屋类型:" + this.getHouseType();
return str;
}
}