public User getUserDetails(String userId) { PreparedStatement ps = null; ResultSet rs = null; User user = null; RegisterService registerService = new RegisterService(); try { ps = connection.prepareStatement("SELECT * FROM user WHERE username=? LIMIT 1"); ps.setString(1, username); rs = ps.executeQuery(); if (rs != null && rs.next()) { user = new User(userId); user.setFirstName(rs.getString("firstname")); user.setLastName(rs.getString("lastname")); user.setEmail(rs.getString("email")); user.setPhoneNumber(rs.getString("primaryPhone")); user.setCellphone(rs.getString("secondaryPhone")); user.setAddress(registerService.createAddressFromID(rs.getInt("addressID"))); user.setCard(registerService.createCardFromID(rs.getInt("creditCardID"))); } } catch (SQLException e) { e.printStackTrace(); } return user; }
public PagedResult<User> readUsers(String source) throws ReviewboardException { try { JSONObject rootObject = checkedGetJSonRootObject(source); int totalResults = rootObject.getInt("total_results"); JSONArray jsonUsers = rootObject.getJSONArray("users"); List<User> users = new ArrayList<User>(); for (int i = 0; i < jsonUsers.length(); i++) { JSONObject jsonUser = jsonUsers.getJSONObject(i); User user = new User(); user.setId(jsonUser.getInt("id")); user.setUrl(jsonUser.getString("url")); user.setUsername(jsonUser.getString("username")); // some fields are not set for private profiles user.setEmail(jsonUser.has("email") ? jsonUser.getString("email") : ""); user.setFirstName(jsonUser.has("first_name") ? jsonUser.getString("first_name") : ""); user.setLastName(jsonUser.has("last_name") ? jsonUser.getString("last_name") : ""); users.add(user); } return PagedResult.create(users, totalResults); } catch (JSONException e) { throw new ReviewboardException(e.getMessage(), e); } }
@Override public User updateUser( long userId, String username, String password, String firstName, String lastName) { User user = userRepository.findOne(userId); user.setUsername(username); user.setFirstName(firstName); user.setLastName(lastName); user.setPassword(password); return this.userRepository.save(user); }
public void load(InputStream inputStream) throws Exception { BufferedReader bur = new BufferedReader(new InputStreamReader(inputStream)); while (bur.ready()) { User user = new User(); user.setFirstName(bur.readLine()); user.setLastName(bur.readLine()); user.setBirthDate(new Date(Long.parseLong(bur.readLine()))); user.setMale(Boolean.parseBoolean(bur.readLine())); user.setCountry(User.Country.valueOf(bur.readLine())); users.add(user); } bur.close(); }
public User createUser(HttpServletRequest req) { User user = new User(); Set<MusicType> musicTypes = new HashSet<>(); String[] musics = req.getParameterValues("musicTypes"); for (int i = 0; i < musics.length; i++) { musicTypes.add(new MusicTypeDAOImp().read(Integer.valueOf(musics[i]))); } Adress adress = new Adress(); adress.setCountry(req.getParameter("country")); adress.setStreet(req.getParameter("street")); adress.setIndex(Integer.valueOf(req.getParameter("zip"))); user.setLogin(req.getParameter("login")); user.setPassword(req.getParameter("password")); user.setFistName(req.getParameter("firstName")); user.setLastName(req.getParameter("lastName")); user.setAdress(adress); user.setRole(new RoleDAOImp().getDefaultRole()); user.setMusicTypes(musicTypes); return user; }
public static boolean createAdmin() { User myAdmin = new Admin("admin"); myAdmin.setEmail("*****@*****.**"); myAdmin.setFirstName("FirstAdmin"); myAdmin.setLastName("lastAdmin"); myAdmin.setgNumber("00800000"); myAdmin.setPassword("password"); myAdmin.setPhoneNumber("5555555555"); String username = JOptionPane.showInputDialog(null, "Enter admin username"); String password = JOptionPane.showInputDialog(null, "Enter admin Password"); if (username.equals(myAdmin.getUsername()) && password.equals(myAdmin.getPassword())) { return true; } else { JOptionPane.showMessageDialog( null, "Bad password: **Hint: username = '******' :: password = '******'"); return false; } }
/** * @param userList * @return Student Register new student by prompting them for user info */ public static Student registerStudentAccount(LinkedList<User> userList) { String username = ""; String password = ""; String first = ""; String last = ""; String GNum = ""; String phoneNum = ""; String email = ""; String address = ""; User aStudent = null; // prompt for username until available username is entered do { username = JOptionPane.showInputDialog("Please enter desired username"); aStudent = validateUsername(username, userList); if (aStudent != null) { JOptionPane.showMessageDialog(null, "This username is already in use!\nPlease try again"); } } while (aStudent != null); // create student object aStudent = new Student(username); // prompt for password until valid entry is given do { password = JOptionPane.showInputDialog("Please enter desired password"); if (!aStudent.setPassword(password)) { JOptionPane.showMessageDialog( null, "Password does not meet requirements. Minimum 8 characters\nTry Again.", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setPassword(password)); // prompt for first name until valid entry is made do { first = JOptionPane.showInputDialog("Please enter your first name"); if (!aStudent.setFirstName(first)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again."); } } while (!aStudent.setFirstName(first)); // prompt for last name until valid entry is made do { last = (JOptionPane.showInputDialog("Please enter your last name")); if (!aStudent.setLastName(last)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again"); } } while (!aStudent.setLastName(last)); // prompt for G-Number until valid entry is made do { GNum = (JOptionPane.showInputDialog("Please enter your G-number")); if (!aStudent.setgNumber(GNum)) { JOptionPane.showMessageDialog( null, "Invalid entry! Please write your GNumber in this format 00XXXXXX \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setgNumber(GNum)); // prompt for phone number until valid entry is made do { phoneNum = (JOptionPane.showInputDialog("Please enter your phone number")); if (!aStudent.setPhoneNumber(phoneNum)) { JOptionPane.showMessageDialog( null, "Invalid entry. Please write your phone number in XXXXXXXXXX format \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setPhoneNumber(phoneNum)); // prompt for email until valid entry is made do { email = (JOptionPane.showInputDialog("Please enter your Email address")); if (!aStudent.setEmail(email)) { JOptionPane.showMessageDialog( null, "Invalid entry, correct format: [email protected] \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setEmail(email)); // prompt for address until valid entry is made Student nStudent = (Student) aStudent; do { address = (JOptionPane.showInputDialog("Please enter your shipping address")); if (!nStudent.setShippingAddress(address)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again"); } } while (!nStudent.setShippingAddress(address)); JOptionPane.showMessageDialog(null, "Your account has been created"); try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("accounts.txt", true))); pw.println( "\r\n-" + aStudent.getFirstName() + "," + aStudent.getLastName() + "," + aStudent.getgNumber() + "," + aStudent.getPassword() + "," + aStudent.getPhoneNumber() + "," + aStudent.getEmail() + "," + aStudent.getUsername() + "," + nStudent.getShippingAddress()); pw.close(); } catch (IOException e) { e.printStackTrace(); } userList.add(aStudent); return nStudent; }
/** @param userList Populate system with list of students accounts from text file */ public static void populateStudentAccounts(LinkedList<User> userList) { Scanner inputStream = null; // open text file of accounts try { inputStream = new Scanner(new FileInputStream("accounts.txt")); // System.out.println("accounts file read"); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "The file \"accounts.txt\" could not be found"); JOptionPane.showMessageDialog(null, "The system will now exit"); System.exit(0); } // Pull line of text to generate a student while (inputStream.hasNextLine()) { String s1 = inputStream.nextLine(); // locate first name int fNsameStart = (s1.indexOf("-") + 1); int fNameEnd = (s1.indexOf(",")); String fName = s1.substring(fNsameStart, fNameEnd); // locate Last name int lNameStart = (s1.indexOf(",", fNameEnd) + 1); int lNameEnd = (s1.indexOf(",", lNameStart)); String lName = s1.substring(lNameStart, lNameEnd); // locate gNumber int gNumberStart = (s1.indexOf(",", lNameEnd) + 1); int gNumberEnd = (s1.indexOf(",", gNumberStart)); String gNumber = s1.substring(gNumberStart, gNumberEnd); // locate password int passwordStart = (s1.indexOf(",", gNumberEnd) + 1); int passwordEnd = (s1.indexOf(",", passwordStart)); String password = s1.substring(passwordStart, passwordEnd); // locate phone number int phoneNumberStart = (s1.indexOf(",", passwordEnd + 1)); int phoneNumberEnd = (s1.indexOf(",", phoneNumberStart)); String phoneNumber = s1.substring(phoneNumberStart, phoneNumberEnd); // locate email int emailStart = (s1.indexOf(",", phoneNumberEnd) + 1); int emailEnd = (s1.indexOf(",", emailStart)); String email = s1.substring(emailStart, emailEnd); // locate username int usernameStart = (s1.indexOf(",", emailEnd) + 1); int usernameEnd = (s1.indexOf(",", usernameStart)); String username = s1.substring(usernameStart, usernameEnd); // locate address int addressStart = (s1.indexOf(",", usernameEnd) + 1); String address = s1.substring(addressStart); // create student object and populate info User aStudent = new Student(username); aStudent.setFirstName(fName); aStudent.setLastName(lName); aStudent.setgNumber(gNumber); aStudent.setPassword(password); aStudent.setPhoneNumber(phoneNumber); aStudent.setEmail(email); if (aStudent instanceof Student) { ((Student) aStudent).setShippingAddress(address); } // add Student to list userList.add(aStudent); System.out.println(userList.size()); } }
public static void main(String[] args) { // you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream // according to your real file location // вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в // соответствии с путем к вашему реальному файлу try { File your_file_name = new File("/home/alexey/pro/out"); OutputStream outputStream = new FileOutputStream(your_file_name); InputStream inputStream = new FileInputStream(your_file_name); User u1 = new User(); User u2 = new User(); u1.setCountry(User.Country.UKRAINE); u2.setCountry(User.Country.OTHER); u1.setBirthDate(new GregorianCalendar(1991, 2, 21).getTime()); u2.setBirthDate(new GregorianCalendar(1990, 11, 7).getTime()); u1.setFirstName("Antonio"); u2.setFirstName("Joni"); u1.setLastName("Kokovetzh"); u2.setLastName("Pakolonetzh"); u1.setMale(true); u2.setMale(true); JavaRush javaRush = new JavaRush(); javaRush.users.add(u1); javaRush.users.add(u2); javaRush.save(outputStream); outputStream.flush(); JavaRush loadedObject = new JavaRush(); loadedObject.load(inputStream); Comparator<List<User>> c = new Comparator<List<User>>() { @Override public int compare(List<User> u1, List<User> u2) { int c = 0; if (u1.size() == u2.size()) for (int i = 0; i < u1.size(); i++) { if (u1.get(i).getBirthDate().equals(u2.get(i).getBirthDate())) c++; if (u1.get(i).getCountry().equals(u2.get(i).getCountry())) c++; if (u1.get(i).getFirstName().equals(u2.get(i).getFirstName())) c++; if (u1.get(i).getLastName().equals(u2.get(i).getLastName())) c++; if (u1.get(i).isMale() == u2.get(i).isMale()) c++; } return c; } }; if (c.compare(javaRush.users, loadedObject.users) == javaRush.users.size() * 5) System.out.println("OK"); else System.out.println("NOT"); outputStream.close(); inputStream.close(); } catch (IOException e) { // e.printStackTrace(); System.out.println("Oops, something wrong with my file"); } catch (Exception e) { // e.printStackTrace(); System.out.println("Oops, something wrong with save/load method"); } }