Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.81 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. import java.util.regex.Pattern;
  12.  
  13. import com.opencsv.CSVReader;
  14. import com.opencsv.CSVWriter;
  15.  
  16. public class MarketPlace {
  17.  
  18. private boolean loggedin = false;
  19. private Scanner userGetter;
  20. private Scanner passGetter;
  21. private Scanner emailGetter;
  22. private Scanner typeGetter;
  23. private Scanner updater;
  24. private String currentUsername = "";
  25.  
  26. public static void main(String[] args) throws FileNotFoundException {
  27. MarketPlace testMarketPlace = new MarketPlace();
  28.  
  29. }
  30.  
  31. public MarketPlace() throws FileNotFoundException {
  32.  
  33. Scanner reader1 = new Scanner(System.in);
  34. System.out.println("Welcome!\n");
  35. System.out.println(helpMenu());
  36.  
  37. while (true) {
  38. String input = reader1.next();
  39.  
  40. if (input.equals("login")) {
  41. System.out.println("Enter your username");
  42. String userSelect = reader1.next();
  43. System.out.println("Enter your password");
  44. String passSelect = reader1.next();
  45.  
  46. userSelect = userSelect.toLowerCase();
  47.  
  48. // this is where I validate the username and password
  49. if (login(userSelect, passSelect).equals("Buyer")) {
  50. Buyer buyer = new Buyer();
  51.  
  52. } else if (login(userSelect, passSelect).equals("MarketPlaceAdmin")) {
  53. MarketPlaceAdmin admin = new MarketPlaceAdmin();
  54. } else if (login(userSelect, passSelect).equals("Seller")) {
  55. Seller seller = new Seller();
  56. }
  57.  
  58. else {
  59. System.out.println("incorrect combination, please try again add something here");
  60. }
  61. } else if (input.equals("logout")) {
  62. loggedin = false;
  63. currentUsername = "";
  64. System.out.println("logged out");
  65. System.out.println("\n" + helpMenu());
  66. } else if (input.equals("search")) {
  67. System.out.println("searching...");
  68. System.out.println("\n" + helpMenu());
  69. } else if (input.equals("help")) {
  70. System.out.println("\n" + helpMenu());
  71. } else if (input.equals("register")) {
  72. register();
  73. } else if(input.equals("update")){
  74. updateCreds();
  75. }
  76. else {
  77. System.out.println("Sorry, I cannot recognize your command, please try again");
  78. System.out.println("\n" + helpMenu());
  79. }
  80. }
  81.  
  82. }
  83.  
  84. // we may need to move this method to each individual class to customize it
  85. // for the type of user
  86. private String helpMenu() {
  87. String help = "Enter a command below (without the quotes) to continue.\n\n";
  88.  
  89. if (loggedin) {
  90. help += "\"logout\" to logout\n";
  91. help += "\"update credentials\" to update credentials\n";
  92. } else {
  93. help += "\"register\" to register\n";
  94. help += "\"login\" to login\n";
  95. }
  96. help += "\"search\" to search for items\n";
  97. help += "\"help\" anytime to see this message again\n";
  98. return help;
  99. }
  100.  
  101. private void register() throws FileNotFoundException {
  102.  
  103. String newUsername = uniqueUsername();
  104.  
  105. String newPassword = newPassword();
  106.  
  107. String newEmail = newEmail();
  108.  
  109. String accountType = accountType();
  110.  
  111. int newID = makeID();
  112.  
  113. try {
  114. FileWriter pw = new FileWriter("bin/creds.csv", true);
  115. StringBuilder sb = new StringBuilder();
  116.  
  117. sb.append(Integer.toString(newID));
  118. sb.append(",");
  119. sb.append(newUsername);
  120. sb.append(",");
  121. sb.append(newPassword);
  122. sb.append(",");
  123. sb.append(newEmail);
  124. sb.append(",");
  125. sb.append(accountType);
  126. sb.append("\n");
  127.  
  128. pw.write(sb.toString());
  129.  
  130. pw.close();
  131.  
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135.  
  136. System.out.println("\nNew Account has been created!\n");
  137.  
  138. System.out.print(helpMenu());
  139. }
  140.  
  141. private int makeID() throws FileNotFoundException {
  142. BufferedReader reader = new BufferedReader(new FileReader("bin/creds.csv"));
  143. String thisLine;
  144. int max = 0;
  145. try {
  146. thisLine = reader.readLine();
  147. while ((thisLine = reader.readLine()) != null) {
  148. thisLine = thisLine.trim();
  149. String[] items = thisLine.split(",");
  150. String id = items[0];
  151. int idNum = Integer.parseInt(id);
  152. if (idNum > max) {
  153. max = idNum;
  154. }
  155. }
  156. } catch (IOException e) {
  157. System.out.print("\nUser does not exist\n");
  158. e.printStackTrace();
  159. } finally {
  160. try {
  161. reader.close();
  162. } catch (IOException e) {
  163. /* ignore */ }
  164. }
  165.  
  166. return (max + 1);
  167. }
  168.  
  169. private String uniqueUsername() throws FileNotFoundException {
  170. boolean unique;
  171.  
  172. String newUsername;
  173.  
  174. userGetter = new Scanner(System.in);
  175.  
  176. do {
  177.  
  178. System.out.print("Please enter a Username: ");
  179. unique = true;
  180.  
  181. newUsername = userGetter.next();
  182. newUsername = newUsername.toLowerCase();
  183.  
  184. BufferedReader reader = new BufferedReader(new FileReader("bin/creds.csv"));
  185. String thisLine;
  186. try {
  187. thisLine = reader.readLine();
  188. while ((thisLine = reader.readLine()) != null) {
  189. thisLine = thisLine.trim();
  190. String[] items = thisLine.split(",");
  191. String user = items[1];
  192. user = user.toLowerCase();
  193.  
  194. if (newUsername.equals(user)) {
  195. unique = false;
  196. System.out.println("Not a unique Username. Please try again");
  197. }
  198. }
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. } finally {
  202. try {
  203. reader.close();
  204. } catch (IOException e) {
  205. /* ignore */ }
  206. }
  207.  
  208. try {
  209. reader.close();
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. }
  213. } while (!unique);
  214.  
  215. return (toTitleCase(newUsername));
  216. }
  217.  
  218. private String newPassword() {
  219. String newPassword;
  220. String confimationPassword;
  221.  
  222. passGetter = new Scanner(System.in);
  223.  
  224. while (true) {
  225. System.out.print("\nPlease enter a password: ");
  226. newPassword = passGetter.next();
  227.  
  228. System.out.print("Please enter your password again: ");
  229. confimationPassword = passGetter.next();
  230.  
  231. if (newPassword.equals(confimationPassword))
  232. break;
  233. else
  234. System.out.print("Passwords did not match, try again!\n");
  235. }
  236.  
  237. return newPassword;
  238. }
  239.  
  240. private String accountType() {
  241. String type;
  242.  
  243. typeGetter = new Scanner(System.in);
  244.  
  245. while (true) {
  246. System.out.print("\nWill you be a Buyer or Seller?: ");
  247. type = typeGetter.next();
  248.  
  249. type = toTitleCase(type);
  250.  
  251. if (type.equals("Buyer") || type.equals("Seller"))
  252. break;
  253. else
  254. System.out.print("Not a valid input, try again!");
  255. }
  256.  
  257. return type;
  258. }
  259.  
  260. private static String toTitleCase(String input) {
  261. StringBuilder titleCase = new StringBuilder();
  262. boolean nextTitleCase = true;
  263.  
  264. for (char c : input.toCharArray()) {
  265. if (Character.isSpaceChar(c)) {
  266. nextTitleCase = true;
  267. } else if (nextTitleCase) {
  268. c = Character.toTitleCase(c);
  269. nextTitleCase = false;
  270. }
  271.  
  272. titleCase.append(c);
  273. }
  274.  
  275. return titleCase.toString();
  276. }
  277.  
  278. private String newEmail() {
  279. String newEmail;
  280. String confimationEmail;
  281.  
  282. emailGetter = new Scanner(System.in);
  283.  
  284. // RFC822 compliant regex for email
  285. Pattern ptr = Pattern.compile(
  286. "(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\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@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\"(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)");
  287.  
  288. while (true) {
  289. System.out.print("\nPlease enter an email: ");
  290. newEmail = emailGetter.next();
  291.  
  292. System.out.print("Please enter your email again: ");
  293. confimationEmail = emailGetter.next();
  294.  
  295. if (newEmail.equals(confimationEmail) && ptr.matcher(newEmail).matches())
  296. break;
  297. else if (!(ptr.matcher(newEmail).matches()))
  298. System.out.print("Not a legal email!\n");
  299. else
  300. System.out.print("Emails did not match, try again!\n");
  301. }
  302.  
  303. return newEmail;
  304. }
  305.  
  306. private String viewStock() {
  307. return null;
  308.  
  309. }
  310.  
  311. private void updateCreds() {
  312. String userResponse;
  313.  
  314. String username;
  315.  
  316. boolean pass = false;
  317. boolean email = true;
  318. CSVReader reader = null;
  319. if (currentUsername != "") {
  320. try {
  321. reader = new CSVReader(new FileReader("bin/creds.csv"), ',');
  322. List<String[]> csvBody = reader.readAll();
  323. updater = new Scanner(System.in);
  324.  
  325. while (true) {
  326. System.out.println("What would you like to update, Password or Email?");
  327. userResponse = updater.next();
  328. userResponse = toTitleCase(userResponse);
  329. if (userResponse.equals("Password")) {
  330. pass = true;
  331. break;
  332. } else if (userResponse.equals("Email")) {
  333. email = true;
  334. break;
  335. } else {
  336. System.out.println("Invalid Response");
  337. }
  338. }
  339. System.out.println("Current Username: " + currentUsername);
  340. for (String[] creds : csvBody) {
  341. if (currentUsername.equals(creds[1])) {
  342. if (pass) {
  343. System.out.println("Changing Password here");
  344. creds[2] = newPassword();
  345. } else if (email) {
  346. System.out.println("Changing Email here");
  347. creds[3] = newEmail();
  348. }
  349. }
  350. }
  351.  
  352.  
  353.  
  354. CSVWriter writer = new CSVWriter(new FileWriter("bin/creds.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER);
  355. writer.writeAll(csvBody);
  356. writer.flush();
  357.  
  358.  
  359. } catch (FileNotFoundException e) {
  360. e.printStackTrace();
  361. } catch (IOException e) {
  362. e.printStackTrace();
  363. }
  364. }
  365. else{
  366. System.out.println("Verify you are logged in first!");
  367. }
  368. }
  369.  
  370. private String search() {
  371. return null;
  372.  
  373. }
  374.  
  375. private String login(String username, String password) throws FileNotFoundException {
  376. String type = "bad input";
  377. BufferedReader reader = new BufferedReader(new FileReader("bin/creds.csv"));
  378. String thisLine;
  379. try {
  380. thisLine = reader.readLine();
  381. while ((thisLine = reader.readLine()) != null) {
  382. thisLine = thisLine.trim();
  383. String[] items = thisLine.split(",");
  384. String user = items[1];
  385. String pass = items[2];
  386. String userType = items[4];
  387. user = user.toLowerCase();
  388. if (user.equals(username)) {
  389. if (pass.equals(password)) {
  390.  
  391. if (userType.equals("Buyer")) {
  392. type = "Buyer";
  393. } else if (userType.equals("Seller")) {
  394. type = "Seller";
  395. } else if (userType.equals("MarketPlaceAdmin")) {
  396. type = "MarketPlaceAdmin";
  397. }
  398. currentUsername = toTitleCase(user);
  399. loggedin = true;
  400. break;
  401. }
  402. }
  403. }
  404.  
  405. } catch (IOException e) {
  406. System.out.print("\nUser does not exist\n");
  407. e.printStackTrace();
  408. } finally {
  409. try {
  410. reader.close();
  411. } catch (IOException e) {
  412. /* ignore */ }
  413. }
  414. return type;
  415.  
  416. }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement