java login 和小练习


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

public class Demo {
	
	/*
	 * 需求1:注册
	 * 需求2:登录案例
	 * 
	 * Properties
	 * Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
	 */
	
	 static Scanner sc = new Scanner(System.in);
	 static Map<String, String> map = new HashMap<String, String>();
	    /*
		 * 封装用户信息
		 */
	 static{
		//1创建一个无默认值的空属性列表。
			Properties p = new Properties();
			//2: 通过输入流把properties文件中的属性集,读取到集合中
			//按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
			try {
				p.load(new BufferedReader(new FileReader("user.properties")));
				//3:通过键获取值
				 String userName = p.getProperty("userName");
				 String password = p.getProperty("password");
				 String realName = p.getProperty("realName");
				 //4:将属性集保存在map集合
				
				 map.put("userName", userName);
				 map.put("password", password);
				 map.put("realName", realName);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		 
	 }
	public static void register() throws IOException{
		//1:注册信息
		System.out.println("请输入用户名");
		String userName = sc.nextLine();
		System.out.println("请输入密码");
		String password = sc.nextLine();
		System.out.println("请输入真实姓名");
		String realName = sc.nextLine();
		//2:将用户信息保存在User.txt文件中
		BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt"));
		bw.write("userName="+userName);
		bw.newLine();
		bw.write("password="+password);
		bw.newLine();
		bw.write("realName="+realName);
		bw.newLine();
		//3:关闭流文件
		bw.close();
		
		System.out.println("注册成功");
	}
	
	/*public static void login() throws IOException{
		System.out.println("---------登录--------");
		//1:填写登录信息
		System.out.println("请输入用户名");
		String userName  = sc.nextLine();
		System.out.println("请输入密码");
		String password  = sc.nextLine();
		
		//2读取user.txt中用户信息
		BufferedReader br = new BufferedReader(new FileReader("user.txt"));
		String name = br.readLine();
		System.out.println(name);
		int index = name.indexOf("=");
		if(index!=-1){
			System.out.println(name.substring(index+1));
		}
		
		String pwd = br.readLine();
		String rname = br.readLine();
		
		
	}*/
	
	
	
	public static void login() throws  IOException{
	
		//1:填写登录信息
		System.out.println("请输入用户名");
		String userName  = sc.nextLine();
		System.out.println("请输入密码");
		String password  = sc.nextLine();
		
		if(userName.equals(map.get("userName"))&&password.equals(map.get("password"))){
		  System.out.println("欢迎"+map.get("realName")+"登陆");	
		}else{
		   System.out.println("用户名或密码有误");
		}	
	}
	
	public static void main(String[] args) {
		//register();
		try {
			login();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


public class User {
	
	private String userName;
	private String password;
	private String realName;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public User(String userName, String password, String realName) {
		super();
		this.userName = userName;
		this.password = password;
		this.realName = realName;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	

}

homeWork


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;

public class Demo {

	
	/*
	 * 3.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;
	 * 将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,
	 * 要求集合中的元素按照年龄大小正序排序;
	 * 最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行。
	 *
	 *技能点
	 *  Scanner
	 *  对象
	 *  集合
	 *  流
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		TreeSet<Student> ts = new TreeSet<>();
		System.out.println("请输入学生信息按照如下格式:1001#张三#20");
		while(true){
			String info = sc.nextLine();
			if("exit".equals(info)){
				break;
			}
			//切割字符串
			String[] str = info.split("#");
			//分别获取属性信息
			Student student = new Student(str[0], str[1], Integer.parseInt(str[2]));
			//将对象封装到集合
			ts.add(student);
		}
		//创建字符缓冲输出流
		BufferedWriter bw = null;
		try {
			 bw = new BufferedWriter(new FileWriter("Student.docx"));
			 //遍历集合
			 bw.write("学号\t姓名\t年龄");
			 bw.newLine();
			 for(Student stu:ts){
				bw.write(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge());
				bw.newLine();
			 }
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("学生信息录入完毕");
		
		
	}
}


public class Student implements Comparable<Student> {
	
	private String id;
	private String name;
	private Integer age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Student(String id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public int compareTo(Student s) {
		//主要条件 按照年龄排序
		int num = this.getAge()-s.getAge();
		//次要条件1 年龄相同,学号不一定相同
		num = num==0?this.getId().compareTo(s.getId()):num;
		//次要条件2 年龄,学号相同,姓名不一定相同
		num = num==0?this.getName().compareTo(s.getName()):num;
		
		return num;
	}
	
	

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值