基于io流的小区管理系统

        作为一个编程小白,当听到要写一个完整的后台管理系统时,我明显是懵懵的,根本没接触国写后台管理系统的概念。但这也是后端学习最基础的了,做后端就是要与更种各样的系统打交道,当然还有其他。下面则是我写小区管理系统时的思路与实现。

        1.创建Resident类写基本功能的get和set方法,并且实现Serializable接口:


public class Resident implements Serializable{

	private String name;
	private String phone;
	private String unit;
	private String room;
	private String phone1;
	
	public Resident() {
		super();
	}
	
	public Resident(String name,String phone,String unit,String room,String phone1) {
		this.name = name;
		this.phone = phone;
		this.unit = unit;
		this.room = room;
		this.phone1 = phone1;
	}
	
	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 getUnit() {
		return unit;
	}
	
	public void setUnit(String unit) {
		this.unit = unit;
	}
	
	public String getRoom() {
		return room;
	}
	
	public void setRoom(String room) {
		this.room = room;
	}
	
	public String getPhone1() {
		return phone1;
	}
	
	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}
	
	@Override
	public String toString() {
		return name + "," + phone + "," + unit + "," + room + "," + phone1;
	}

	
}

        2.创建功能接口:

public interface Function {

	public void add(Resident re);
	public void delete(String phone);
	public void find(String phone);
	public void update(Resident re);
	public void show();
	
}

        当然还有其他功能,目前我所能写好的就是基本的增删改查了。

        3.创建页面类,用以显示用户要进行的操作:

public class Page {

	private Function rede = new Realize();
	private Scanner input = new Scanner(System.in);
	
	public Resident inputResidentInfo() {
		Resident re = new Resident();
		
		System.out.println("请输入住户姓名");
		re.setName(input.next());
		
		System.out.println("请输入住户电话号");
		re.setPhone(input.next());
		
		System.out.println("请输入住户单元号");
		re.setUnit(input.next());
		
		System.out.println("请输入住户户号");
		re.setRoom(input.next());
		
		return re;
		
	}
	
	
	public void showMenu() {
		Resident re = null;
		String phone = null;
		try {
			while (true) {
				System.out.println("小区住户管理系统");
				System.out.println("1-添加住户信息");
				System.out.println("2-删除住户信息");
				System.out.println("3-查找住户信息");
				System.out.println("4-修改住户信息");
				System.out.println("5-显示住户信息");
				System.out.println("0-退出系统");
				System.out.println("请在0-5间选择操作");

				String operation = input.next();

				switch (operation) {
				case "1":
					System.out.println("小区住户管理系统->添加");
					System.out.println("请输入要添加的住户电话号");
					re = inputResidentInfo();
					rede.add(re);
					break;
				case "2":
					System.out.println("小区管理系统->删除");
					System.out.println("请输入要删除的住户的电话号");
					phone = input.next();
					rede.delete(phone);
					break;
				case "3":
					System.out.println("小区管理系统->查找");
					System.out.println("请输入要查找的住户的电话号");
					phone = input.next();
					rede.find(phone);
					
					if (rede == null) {
						System.out.println("没有该住户信息");
					}
					break;
				case "4":
					System.out.println("小区管理系统->修改");
					System.out.println("请输入修改的住户的电话号");
					phone = input.next();
					//查询要修改的住户
					rede.find(phone);
					if(rede == null) {
						System.out.println("没有该住户的信息");
					}else {
						System.out.println("请输入要修改的住户电话号");
						re = inputResidentInfo();
						re.setPhone1(phone);
						rede.update(re);//保存
					}
					break;
				case "5":
					System.out.println("小区管理系统->显示");
					rede.show();
					break;
				case "0":
					System.out.println("退出系统");
					System.exit(0);
					break;
				default:
					System.out.println("请在0-5间选择数字");
					break;
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

        4.创建实现类执行功能类:


public class Realize implements Function{
	private ArrayList<Resident> resi= null;

	@Override
	public void add(Resident re) {
		//获取文件中保存的住户信息
		resi = (ArrayList<Resident>) Util.findResidentListFromFile();
		if(resi == null || resi.isEmpty()) {
			resi = new ArrayList<Resident>();
			resi.add(re);
		}else {
			List<String> rede = new ArrayList<String>();
			for(Resident ost : resi) {
				rede.add((String) ost.getPhone());
			}
			resi.add(re);//将住户添加到集合中
			
		}
		
		//保存对象到文件中
		boolean flag = Util.ResidentListToFile(resi);
		if(flag) {
			System.out.println("住户信息保存成功");
		}else {
			System.out.println("住户信息保存失败");
		}
	}

	@Override
	public void delete(String phone) {
		//获取保存的集合对象
		resi = (ArrayList<Resident>) Util.findResidentListFromFile();
		if(resi == null || resi.isEmpty()) {
			System.out.println("没有住户信息,请添加");
			return;
		}
		
		//定义对象在集合中的索引位置
		int index = -1;
		
		int i = 0;//集合的下标索引
		for(Resident res : resi) {
			if(res.getPhone().equals(phone)) {
				index = i;//找到住户
				break;
			}
			i++;//移动下标位置
		}
		
		if(index != -1) {
			//删除住户对象
			resi.remove(index);
			//保存住户对象到文件
			Util.ResidentListToFile(resi);
			System.out.println("住户信息删除成功");
		}else {
			System.out.println("住户信息删除失败");
		}
		
	}

	@Override
	public void find(String phone) {
		//获取文件中的住户对象
		resi = (ArrayList<Resident>) Util.findResidentListFromFile();
		if(resi == null || resi.isEmpty()) {
			System.out.println("没有住户信息,请添加");
			return;
		}
		//定义对象在集合中的索引位置
		int index = -1;
		
		int i = 0;
		for(Resident rd : resi) {		
			if(rd.getPhone().equals(phone)) {
				index = i;
				break;
			}
			i++;
		}
		
		if(index != -1) {
			System.out.println("找到住户信息");
			//从集合中获取住户信息
			Resident re = resi.get(index);
			System.out.println("姓名\t手机号\t单元号\t户号");
			System.out.println("================");
			System.out.println(re.getName() + "\t" + re.getPhone() + "\t" + re.getUnit() + "\t" + re.getRoom());
			System.out.println("================");
		}else {
			System.out.println("没有找到住户信息");
			return;
		}
		
	}

	@Override
	public void update(Resident re) {
		//获取文件中的住户对象
		resi = (ArrayList<Resident>) Util.findResidentListFromFile();
		if(resi == null || resi.isEmpty()) {
			System.out.println("没有住户信息,请添加");
			return;
		}
		
		//定义对象在集合中的索引位置
		int index = -1;
		int i = 0;
		for(Resident rsi : resi) {
			if(rsi.getPhone().equals(re.getPhone1())) {
				index = i;
				break;
			}
			i++;
		}
		
		if(index != -1) {
			//从集合修改对象
			resi.set(index,re);
			
			//保存住户对象到文件
			Util.ResidentListToFile(resi);
			System.out.println("住户信息更新完成");
		}else {
			System.out.println("住户信息更新失败");
		}
		
	}

	@Override
	public void show() {
		//获取文件中的住户对象
		resi = (ArrayList<Resident>) Util.findResidentListFromFile();
		if(resi == null || resi.isEmpty()) {
			System.out.println("没有住户信息,请添加");
			return;
		}
		System.out.println("姓名\t电话号\t单元号\t户号");
		System.out.println("==============");
		for(Resident re : resi) {
			System.out.println(re.getName() + "\t" + re.getPhone() + "\t" + re.getUnit() + "\t" + re.getRoom());
		}
		System.out.println("=============");
		
		
	}
}

        5.创建util类,用于将数据通过io流进行的储存和调用:


public class Util {

	private static File file = new File("d://Resident.txt");
	
	public static boolean ResidentListToFile(ArrayList<Resident> resi) {
		
		try {
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			ObjectOutputStream objOut = new ObjectOutputStream(bos);
			objOut.writeObject(resi);
			objOut.flush();
			objOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public static  List<Resident> findResidentListFromFile(){
		if(!file.exists()&&file.length()==0) {
			System.out.println("文件不存在或无住户信息,请添加!");
			return null;
		}
		
		try {
			FileInputStream fis = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(fis);
			ObjectInputStream ois = new ObjectInputStream(bis);
			List<Resident> resi = (List<Resident>) ois.readObject();
			return resi;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}

        6.创建main类在其中用main方法完成登录系统的操作和调用小区管理系统的操作:


public class Main {
	
	public static final String DATA_PATH = "d://user.txt";

	public static void main(String[] args) throws IOException, ClassNotFoundException{
		
		   Scanner scan=new Scanner(System.in);
	       
	        while(true){
	        System.out.println("===登录系统===");
	        System.out.println("1.注册");
	        System.out.println("2.登录");
	        System.out.println("0.退出");
	        System.out.println("==注意== 未注册的用户请先注册");
	        System.out.println("请输入编号进行操作");
	        
	        String action = scan.next();
	        switch(action) {
	        case "1":
	        	register();
	        	break;
	        case "2":
	        	if(login()) {
	        		System.out.println("登录成功");
	        		 //进入小区住户管理系统
	        		Page page = new Page();
      				page.showMenu();
	        	}else {
	        		System.out.println("登录失败");
	        	}
	        	break;
	        case "0":
	        	System.exit(0);
	        	break;
	        default :
	        	System.out.println("无效操作");
	        	break;
	        } 
	     }
	}
	
	public static void register() {
		
		try {
			FileWriter fw = new FileWriter(DATA_PATH, true);
			BufferedWriter bw = new BufferedWriter(fw);
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入账户名");
			String username = sc.nextLine();
			System.out.println("请输入密码");
			String userpassword = sc.nextLine();
			bw.write(username + " " + userpassword);
			bw.newLine();
			bw.close();
			System.out.println("注册成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static boolean login() {
		
		try {
			FileReader fr = new FileReader(DATA_PATH);
			BufferedReader br = new BufferedReader(fr);
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入账户名");
			String username = sc.next();
			System.out.println("请输入密码");
			String userpassword = sc.next();
			String line;
			while ((line = br.readLine()) != null) {
				String[] userData = line.split(" ");
				if (userData[0].equals(username) && userData[1].equals(userpassword)) {
					return true;
				}
			} 
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}

        这只是我这一个小白的基本的系统编程,若有问题,欢迎指出,我很听劝的。      

        总结:后端的学习真的时有点难的,学习区域跨度也较大,一阶段与一阶段间的学习内容的练习并不是很大,难易程度也不一,这在一定程度上也加大了学习的难度。但书山有路勤为径,学海无涯苦作舟。熟能生巧,多练多学一定能将知识融会贯通。

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值