黑马程序员技术交流社区

标题: 模拟数据库做了一个类 [打印本页]

作者: java程序猿    时间: 2016-9-16 21:43
标题: 模拟数据库做了一个类
* map中有如下数据(电话号=套餐价格)
* [13265477888=168,15241698745=11,13699989898=20,1898686666=120]
* 在ip为127.0.0.1数据库名为stdb
* ,连接数据库的用户名和密码为:admin和123456中有一个numinfo表相关字段为(id,iphonenum,tomoney)(15分)
* (1)将map中的手机号码取出来打印到控制台上(3分)
* (2)判断map中所有的手机号在numinfo表中是否存在存在则输出"该主机已登录"如果不存在将该号码及对应的套餐价格存入到numinfo表中.(12分)
* (map中的数据不需要修改)


[AppleScript] 纯文本查看 复制代码
public class StrongTest2 {
        public static void main(String[] args) {
                Map<String, Integer> map = new HashMap<>();
                map.put("13265477888", 168);
                map.put("15241698745", 11);
                map.put("13699989898", 20);
                map.put("1898686666", 120);

                for (String str : map.keySet()) {
                        System.out.println(str + "=" + map.get(str));
                }
                stdb db = null;
                try {
                        db = new stdb("admin","123456");
                }catch(Exception e) {
                        db = null;
                        e.printStackTrace();
                }
               
                String key = "15241698745";
               
                if(db != null) {
                        if(db.getMap().containsKey(key)) {
                                System.out.println("该主机已登录");
                        }else {
                                db.getMap().put(key, map.get(key));
                        }
                }
               
                System.out.println("数据库中存在以下号码套餐");
                for(String str :db.getMap().keySet()) {
                        System.out.println(str + "=" + db.getMap().get(str));
                }
        }
}

// 模拟数据库
class stdb {
        private static Map<String, Integer> map = new HashMap<>();// 模拟numinfo表
        public static final String username = "admin";
        public static final String password = "123456";

        public Map<String,Integer> getMap() {
                map.put("13265477888", 168);
                return map;
        }

        public static String getUsername() {
                return username;
        }

        public static String getPassword() {
                return password;
        }

        public stdb(String un, String pw) throws Exception {
                if(!username.equals(un) || !password.equals(pw)) {
                        System.out.println("用户名或密码输入错误");
                        throw new Exception("用户名或密码输入错误");
                }
               
        }
       
        private stdb() {}

}





不喜勿喷,小菜制作,大神谅解






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2