JAVASE的学习笔记(九)
Properties类
简单操作(添加,取出)
import java.util.*;
public class F07 {
public static void main(String[] args) {
Properties props = new Properties();
props.put("九天十地","荒天帝");
props.setProperty("遮天","叶天帝");
System.out.println(props.get("九天十地"));
System.out.println(props.getProperty("叶天帝"));
System.out.println(props.getProperty("仙古"));
System.out.println(props.getProperty("星空之下","王升"));
}
}
结果:
属性文件读取配置信息(重要)
Java语言中因为由了Propertie的工具类,它可以读取特点的某些文件,例如
常用属性文件(后缀名.properties)
或者XML文件
属性文件:常常用来配置一些不变的数据,问题:我在Java中配置常量不也是不变的量吗?但是不方便修改,如果在生产机器(只有我们class文件,无法修改源文件)
jdbc.properties
# "#"代表注释
# KEY=VALUE KEY:String VALUE:String
# 独立一行
# setProperty("bookName","诛仙");
bookName=诛仙
# setProperty("my.author","萧");
my.author=萧
# 该文件我们可以随时进行修改
属性写入信息(追加)
import java.util.*;
import java.io.*;
public class Demo03{
public static void main(String...args)throws Exception{
//property的复数properties 表示属性
Properties props = new Properties();
props.setProperty("name","加油");
props.setProperty("remark","Happy");
//特意写一个重复的KEY
props.setProperty("bookName","大白");
//原来的东西重新替换掉了
/*
props.store(new FileOutputStream("k:/jdbc.properties"),
"zhe ge shi zhu shi");
*/
//在文件的末尾进行追加
props.store(new FileOutputStream("k:/jdbc.properties",true),
"zhe ge shi zhu shi");
}
}
面向接口编程(小程序)
做一个简单的在控制台运行的宗门分部管理小程序;
1.设置宗门分部类
package com.yu.test;
public class Department {
private Integer deptCode;//使用包装类,因为包装类可以存入null的数据,而基本数据类型必须得有数据
private String deptName;
private String userName;
public Department(){};
public Department(Integer deptCode, String deptName, String userName) {
this.deptCode = deptCode;
this.deptName = deptName;
this.userName = userName;
}
public Integer getDeptCode() {
return deptCode;
}
public void setDeptCode(Integer deptCode) {
this.deptCode = deptCode;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
2.初始化宗门各部
package com.yu.test;
import java.util.ArrayList;
import java.util.List;
/*只可被继承*/
public abstract class DateRepository {//数据仓库类,初始化数据,提供给其他类使用
protected static List<Department> dates;//static共享了数据(重要)
static {//初始化数据
dates = new ArrayList<>();
dates.add(new Department(1001,"斗部","苏宇"));
dates.add(new Department(1002,"影部","冰台"));
dates.add(new Department(1003,"商部","三万"));
}
}
3.设定程序基本功能接口
package com.yu.test;
import java.util.List;
public interface DepartmentDao {
//1.返回所有部门信息
List<Department> list();
//2.根据提供的部门编号,删除对应记录
boolean delete(Integer departmentCode);
//3.新增部门,提供一个新的部门对象
boolean add(Department department);
}
4.实现接口
package com.yu.test;
import java.util.List;
public class DepartmentDaoImpl extends DateRepository implements DepartmentDao{
@Override
public List<Department> list() {
return dates;
}
@Override
public boolean delete(Integer departmentCode) {
//输入部门编号的时候,有两种情况:1.存在 2.不存在
boolean flag = true;
int index = -1;
for (int i = 0; i <dates.size() ; i++) {
Department dept = dates.get(i);
Integer code = dept.getDeptCode();//超出-128~127的范围
if (code.equals(departmentCode)){
index = i;//找到索引
}
}
//判断索引是否找到
if (index != -1){
dates.remove(index);
return true;
}else {
return false;
}
}
@Override
public boolean add(Department department) {
int index = -1;
for (int i = 0; i <dates.size() ; i++) {
Department dept = dates.get(i);
Integer code = dept.getDeptCode();//超出-128~127的范围
if (code.equals(department.getDeptCode())){
index = i;//找到索引
}
}
if (index!=-1){
return false;
}else {
dates.add(department);
return true;
}
}
}
5.主程序
package com.yu.test;
import java.util.List;
import java.util.Scanner;
public class DepartmentRun {
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args){
//接口回调
DepartmentDao departmentDao = new DepartmentDaoImpl();
while(true){
System.out.println("欢迎使用宗门各部管理系统");
System.out.println("A.查看部门列表");
System.out.println("B.添加部门信息");
System.out.println("C.注销部门");
System.out.println("请输入指令:");
String str = scan.next();
//比较的时候忽略大小写
if ("A".equalsIgnoreCase(str)){
List<Department> list = departmentDao.list();
if (!list.isEmpty()){
System.out.println("部门编号\t\t部门名称\t\t部门首领");
for (Department dept : list){
System.out.println(dept.getDeptCode()+"\t\t"+dept.getDeptName()+"\t\t\t"+dept.getUserName());
}
}else {
System.out.println("目前无数据");
}
}else if ("B".equalsIgnoreCase(str)){
System.out.println("请输入部门编号:");
int code = scan.nextInt();
System.out.println("请输入部门名称:");
String dept_name = scan.next();
System.out.println("请输入首领姓名:");
String usre_name = scan.next();
//封装成部门对象
Department dept = new Department(code,dept_name,usre_name);
boolean flag = departmentDao.add(dept);
if (flag){
System.out.println("添加成功");
}else {
System.out.println("编号重复,添加失败");
}
}else if ("C".equalsIgnoreCase(str)){
System.out.println("请输入部门编号:");
int code = scan.nextInt();
boolean flag = departmentDao.delete(code);
if (flag){
System.out.println("删除成功");
}else {
System.out.println("未找到编号");
}
}else{
System.out.println("您输入的指令有误,请重新输入");
System.out.println();
System.out.println();
System.out.println("=================");
}
}
}
}