public static void main(String[] args) { User person = new User(); person.setName("Name: "); person.setAge("Age: 1 - 100", 1, 100); person.setHeight("Height: 36 - 92", 36, 92); person.setWeight("Weight: 80 - 400", 80, 400); person.setFavNumber("Favorite Numner: 0 - 1", 0, 1); System.out.println("Name:\t" + person.getName()); System.out.println("Age:\t" + person.getAge()); System.out.println("Height:\t" + person.getHeight()); System.out.println("Weight:\t" + person.getWeight()); System.out.println("Number:\t" + person.getFavNumber()); // get a filename String filename = (System.getProperty("user.dir") + File.separatorChar + "file2015Oct.txt"); // write to a file PrintWriter writer = null; try { writer = new PrintWriter(new File(filename)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.println(person.getName()); writer.println(person.getAge()); writer.println(person.getHeight()); writer.println(person.getWeight()); writer.println(person.getFavNumber()); writer.close(); // read a file // This will reference one line at a time String line = null; try { // FileReader reads text files in the default encoding. FileReader fileReader = new FileReader(filename); // Always wrap FileReader in BufferedReader. BufferedReader bufferedReader = new BufferedReader(fileReader); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } // Always close files. bufferedReader.close(); } catch (FileNotFoundException ex) { System.out.println("Unable to open file '" + filename + "'"); } catch (IOException ex) { System.out.println("Error reading file '" + filename + "'"); // Or we could just do this: // ex.printStackTrace(); } }
public static void testUpdateAndFind() { User user = new User(); user = getSession().insert(user); user.setName("name12"); getSession().update(user); User user1 = getSession().find(User.class, user.getId()); System.out.println(user1.getId() == user.getId()); System.out.println(user1.getName().equals(user.getName())); System.out.println(user1.getAge() == user.getAge()); System.out.println(user.getId()); }
public void putAllDetails(User user) { SharedPreferences.Editor spEditor = sharedPreferences.edit(); spEditor.putString("username", user.getUsername()); spEditor.putString("name", user.getName()); spEditor.putString("password", user.getPassword()); spEditor.putString("age", user.getAge()); spEditor.putString("email", user.getEmail()); spEditor.putString("phone", user.getPhone()); spEditor.putString("position", user.getPosition()); spEditor.putString("experience", user.getExperience()); spEditor.putString("curloc", user.getCurloc()); spEditor.putString("desloc", user.getDesloc()); spEditor.putString("imageuri", user.getImageUri()); spEditor.putString("com1name", user.getCom1name()); spEditor.putString("com1pos", user.getCom1pos()); spEditor.putString("com1from", user.getCom1from()); spEditor.putString("com1to", user.getCom1to()); spEditor.putString("com1resp", user.getCom1resp()); spEditor.putString("com2name", user.getCom2name()); spEditor.putString("com2pos", user.getCom2pos()); spEditor.putString("com2from", user.getCom2from()); spEditor.putString("com2to", user.getCom2to()); spEditor.putString("com2resp", user.getCom2resp()); spEditor.putString("com3name", user.getCom3name()); spEditor.putString("com3pos", user.getCom3pos()); spEditor.putString("com3from", user.getCom3from()); spEditor.putString("com3to", user.getCom3to()); spEditor.putString("com3resp", user.getCom3resp()); spEditor.apply(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); view = (TextView) findViewById(R.id.textView2); Button button = (Button) findViewById(R.id.button2); editText = (EditText) findViewById(R.id.editText); // 获取传递的数据并显示出来 Intent i = getIntent(); // Bundle bundle = i.getBundleExtra("date"); // 设置显示数据,显示数据key,默认无数据是显示的参数 /*view.setText(String.format("data=%s,age=%d,name=%s", bundle.getString("data"), bundle.getInt("age"), bundle.getString("name","name")));*/ // // view.setText(bundle.getString("data")+"\n"+bundle.getInt("age")+"\n"+bundle.getString("name", // "name")); User user = i.getParcelableExtra("user"); // 使用Parcelable传递对象,自定义序列化 view.setText(String.format("Usrt info(name=%s,age=%d) ", user.getName(), user.getAge())); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.putExtra("date", editText.getText().toString()); setResult(1, i); finish(); } }); }
@Override public boolean addUser(User user) { try { DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL); Connection conn = myFactory.getConnection(); PreparedStatement pstmt = conn.prepareStatement( "insert into users(idproducts,username,userpassword,age,gender,location,birthday) values (?,?,?,?,?,?,?) "); pstmt.setInt(1, user.getId()); pstmt.setString(2, user.getUsername()); pstmt.setString(3, user.getUserpassword()); pstmt.setInt(4, user.getAge()); pstmt.setString(5, user.getGender()); pstmt.setString(6, user.getLocation()); pstmt.setString(7, user.getBirthday()); pstmt.executeUpdate(); conn.close(); return true; } catch (SQLException ex) { Logger.getLogger(UserDAO_MySQL.class.getName()).log(Level.SEVERE, null, ex); } return false; }
@Test public void testCreate() throws Exception { User.UserBuilder builder = BeanBuilder.create(new User(), User.UserBuilder.class); User user = builder.id("id").name("name").age(33).build(); Assert.assertThat(user.getId(), is("id")); Assert.assertThat(user.getName(), is("name")); Assert.assertThat(user.getAge(), is(33)); }
public void storePersonal(User user) { SharedPreferences.Editor spEditor = sharedPreferences.edit(); spEditor.putString("username", user.getUsername()); spEditor.putString("name", user.getName()); spEditor.putString("password", user.getPassword()); spEditor.putString("age", user.getAge()); spEditor.putString("email", user.getEmail()); spEditor.putString("phone", user.getPhone()); spEditor.apply(); }
public void storeUserData(User user) { SharedPreferences.Editor userLocalDatabaseEditor = userLocalDatabase.edit(); userLocalDatabaseEditor.putString("name", user.getName()); userLocalDatabaseEditor.putString("username", user.getUsername()); userLocalDatabaseEditor.putString("password", user.getPassword()); userLocalDatabaseEditor.putString("sex", user.getSex()); userLocalDatabaseEditor.putInt("age", user.getAge()); userLocalDatabaseEditor.putInt("height", user.getHeight()); userLocalDatabaseEditor.putInt("weight", user.getWeight()); userLocalDatabaseEditor.putInt("physAct", user.getPhysAct()); userLocalDatabaseEditor.commit(); }
@Override public void updateUser(User user) { try { DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL); Connection conn = myFactory.getConnection(); PreparedStatement pstmt = conn.prepareStatement( "update products SET username=?,userpassword=?,age=?,gender=?,location=?,birthday=? where productID=?"); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getUserpassword()); pstmt.setInt(3, user.getAge()); pstmt.setString(4, user.getGender()); pstmt.setString(5, user.getLocation()); pstmt.setString(6, user.getBirthday()); pstmt.setInt(7, user.getId()); pstmt.executeUpdate(); conn.close(); } catch (SQLException ex) { Logger.getLogger(ProductDAO_MySQL.class.getName()).log(Level.SEVERE, null, ex); } }
public void readFile() { char letter; ArrayList<String> line = new ArrayList<String>(); String s = new String(); try { while (input.available() != 0) { letter = (char) input.read(); if (letter != '\r' && letter != '\n') { s = s + letter; } if (letter == '\n') { line.add(s); s = new String(); } } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < line.size(); i++) { String[] parsed = line.get(i).split("-"); Product temp = new Product(); ArrayList<String> tags = new ArrayList<String>(); for (int j = 0; j < parsed.length; j++) { switch (j) { case 0: temp.setName(parsed[j]); break; case 1: temp.setCost(Double.parseDouble(parsed[j])); break; case 2: temp.setDescription(parsed[j]); break; case 3: temp.setImageName(parsed[j]); break; } if (j > 3) { tags.add(parsed[j]); } } temp.setTags(tags); temp.setQuantity(1); temp.setSelected(false); // This code takes each object as it is read and divides it into a tree of categories and // subcategories based on the tags belonging to the product // I can explain it all in the demo in detail if (categoryTable.containsKey(tags.get(0))) { if (subcategoryTable.get(categoryTable.get(tags.get(0))).containsKey(tags.get(1))) { totalItems .get(categoryTable.get(tags.get(0))) .get(subcategoryTable.get(categoryTable.get(tags.get(0))).get(tags.get(1))) .add(temp); } else { totalItems.get(categoryTable.get(tags.get(0))).add(new ArrayList<Product>()); subcategoryTable .get(categoryTable.get(tags.get(0))) .put(tags.get(1), totalItems.get(categoryTable.get(tags.get(0))).size() - 1); totalItems .get(categoryTable.get(tags.get(0))) .get(subcategoryTable.get(categoryTable.get(tags.get(0))).get(tags.get(1))) .add(temp); } } else { totalItems.add(new ArrayList<ArrayList<Product>>()); categoryTable.put(tags.get(0), totalItems.size() - 1); subcategoryTable.add(new Hashtable<String, Integer>()); totalItems.get(categoryTable.get(tags.get(0))).add(new ArrayList<Product>()); subcategoryTable .get(categoryTable.get(tags.get(0))) .put(tags.get(1), totalItems.get(categoryTable.get(tags.get(0))).size() - 1); totalItems .get(categoryTable.get(tags.get(0))) .get(subcategoryTable.get(categoryTable.get(tags.get(0))).get(tags.get(1))) .add(temp); } if (tags.size() > 2) // Extra tags for recommendations, can be explained in the demo. Checks extra tags // for each product and compares them to details given and provides a list of // products to recommend to the user { for (int k = 2; k < tags.size(); k++) { if (user.getLimit() > temp.getCost()) { Log.d( "TAG", "user.getJobTitle() = " + user.getJobTitle() + " tags.get(k) = " + tags.get(k) + " Equals: " + user.getJobTitle().equals(tags.get(k))); if (user.getJobTitle().equals(tags.get(k))) { if (!recommended.contains(temp)) { recommended.add(temp); } } if (user.getGender().equals(tags.get(k))) { Log.d( "TAG", "user.getGender() = " + user.getGender() + " tags.get(k) = " + tags.get(k) + " Equals: " + user.getGender().equals(tags.get(k))); if (!recommended.contains(temp)) { recommended.add(temp); } } if (user.getAge() < 10) { if (tags.get(k) == "Young") { if (!recommended.contains(temp)) { recommended.add(temp); } } } else if (user.getAge() < 25) { if (tags.get(k) == "Young Adult") { if (!recommended.contains(temp)) { recommended.add(temp); } } } else if (user.getAge() < 60) { if (tags.get(k) == "Adult") { if (!recommended.contains(temp)) { recommended.add(temp); } } } } } } // End of building recommendation list // Would have been used for filtering the recommendation list more, not enough time // for(Product p : recommended) // { // // } } }