Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import com.opencsv.CSVReader;
- import com.opencsv.CSVWriter;
- public class Buyer{
- private Scanner input;
- private CSVReader reader;
- private String currentUsername = "";
- private Scanner updater;
- private Scanner passGetter;
- private Scanner emailGetter;
- private CSVReader reader3;
- public Buyer(){
- try {
- login();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public Buyer(String username){
- try {
- login();
- currentUsername = username;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private void register(){
- }
- private void login() throws IOException{
- System.out.println("\nLogged in as Buyer\n\n");
- String help = "Enter a command below (without the quotes) to continue.\n\n";
- help += "\"view\" to view items for sale\n";
- help += "\"cart\" to view your cart\n";
- help += "\"search\" to search for items\n";
- help += "\"buy\" to buy items in cart\n";
- help += "\"history\" to view purchase history\n";
- help += "\"update\" to update credentials\n";
- help += "\"logout\" to logout\n\n";
- help += "\"help\" anytime to see this message again\n";
- System.out.println(help);
- Scanner buyInput = new Scanner(System.in);
- String input = buyInput.next().toLowerCase();
- if(input.equals("view")){
- viewMarketPlaceItems();
- }else if(input.equals("cart")){
- //viewCart();
- }else if(input.equals("search")){
- search();
- }else if(input.equals("buy")){
- buy();
- login();
- }else if(input.equals("history")){
- //history();
- }else if(input.equals("update")){
- updateCreds();
- }else if(input.equals("logout")){
- }else if (input.equals("help")){
- login();
- }else{
- System.out.println("Sorry, I cannot recognize your command, please try again\n");
- login();
- }
- }
- private void buy() throws IOException {
- System.out.println("What do you want to buy?");
- Scanner buying = new Scanner(System.in);
- String itemToBuy = buying.next();
- int quantity = 0;
- String sellerOfItem = "";
- int amountToBuy = 0;
- reader = new CSVReader(new FileReader("bin/items.csv"), ',');
- List<String[]> csvBody = reader.readAll();
- for (String[] items : csvBody) {
- if (itemToBuy.equals(items[1])) {
- quantity = Integer.parseInt(items[4]);
- sellerOfItem = items[0];
- if (quantity > 0) {
- while (true) {
- System.out.println("How many would you like to buy?");
- amountToBuy = buying.nextInt();
- if (amountToBuy <= quantity) {
- items[4] = String.valueOf(quantity - amountToBuy);
- System.out.println("You bought " + amountToBuy + " " + itemToBuy);
- break;
- }
- }
- }
- }
- }
- CSVWriter writer = new CSVWriter(new FileWriter("bin/items.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER);
- writer.writeAll(csvBody);
- writer.flush();
- quantity = quantity - amountToBuy;
- notifySeller(sellerOfItem, itemToBuy, quantity, amountToBuy);
- }
- private String viewMarketPlaceItems(){
- return null;
- }
- public void updateCreds() {
- String userResponse;
- String username;
- boolean pass = false;
- boolean email = true;
- CSVReader reader = null;
- try {
- reader = new CSVReader(new FileReader("bin/creds.csv"), ',');
- List<String[]> csvBody = reader.readAll();
- updater = new Scanner(System.in);
- while (true) {
- System.out.println("What would you like to update, Password or Email?");
- userResponse = updater.next();
- userResponse = toTitleCase(userResponse);
- if (userResponse.equals("Password")) {
- pass = true;
- break;
- } else if (userResponse.equals("Email")) {
- email = true;
- break;
- } else {
- System.out.println("Invalid Response");
- }
- }
- System.out.println("Current Username: " + currentUsername);
- for (String[] creds : csvBody) {
- if (currentUsername.equals(creds[1])) {
- if (pass) {
- System.out.println("Changing Password here");
- creds[2] = newPassword();
- } else if (email) {
- System.out.println("Changing Email here");
- creds[3] = newEmail();
- }
- }
- }
- CSVWriter writer = new CSVWriter(new FileWriter("bin/creds.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER);
- writer.writeAll(csvBody);
- writer.flush();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static String toTitleCase(String input) {
- StringBuilder titleCase = new StringBuilder();
- boolean nextTitleCase = true;
- for (char c : input.toCharArray()) {
- if (Character.isSpaceChar(c)) {
- nextTitleCase = true;
- } else if (nextTitleCase) {
- c = Character.toTitleCase(c);
- nextTitleCase = false;
- }
- titleCase.append(c);
- }
- return titleCase.toString();
- }
- private String newPassword(){
- String newPassword;
- String confimationPassword;
- passGetter = new Scanner(System.in);
- while(true){
- System.out.print("\nPlease enter a password: ");
- newPassword = passGetter.next();
- System.out.print("Please enter your password again: ");
- confimationPassword = passGetter.next();
- if (newPassword.equals(confimationPassword)) break;
- else System.out.print("Passwords did not match, try again!\n");
- }
- return newPassword;
- }
- private String newEmail(){
- String newEmail;
- String confimationEmail;
- emailGetter = new Scanner(System.in);
- //RFC822 compliant regex for email
- Pattern ptr = Pattern.compile("(?:(?:\\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|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\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\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*)");
- while(true){
- System.out.print("\nPlease enter an email: ");
- newEmail = emailGetter.next();
- System.out.print("Please enter your email again: ");
- confimationEmail = emailGetter.next();
- if (newEmail.equals(confimationEmail) && ptr.matcher(newEmail).matches()) break;
- else if (!(ptr.matcher(newEmail).matches())) System.out.print("Not a legal email!\n");
- else System.out.print("Emails did not match, try again!\n");
- }
- return newEmail;
- }
- private String search(){
- return null;
- }
- private void notifySeller(String seller, String item, int quantity, int amountToBuy) throws IOException{
- try {
- reader3 = new CSVReader(new FileReader("bin/creds.csv"), ',');
- List<String[]> csvBody = reader3.readAll();
- for (String[] cred : csvBody){
- if(cred[1].equals(seller)){
- cred[5] += (" " + amountToBuy + " " + item + "s has been sold! "+ quantity + " " + item + "s remain! ");
- }
- }
- CSVWriter writer = new CSVWriter(new FileWriter("bin/creds.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER);
- writer.writeAll(csvBody);
- writer.flush();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement