0

I need to create a program that can add, edit, delete, search, and sort the list of customer's points. And I couldn't figure out how to sort, delete, and search the list. Here is my code right now:

/*
RewardPoints.java
Description: This program add, edit, delete, search, and sort customer's reward points.
*/

import java.util.*;

public class Main {

  public static void main(String[] args) {
    ArrayList <Customers> inventory = new ArrayList <Customers>();
    mainMenu(inventory);

  }

  public static void mainMenu(ArrayList<Customers> inventory) {
    Scanner sin = new Scanner(System.in);
    int choice = 0;

    while (true) {
      System.out.println("Grocery Store Cutomer's Points System");
      System.out.println("---------------------------------------------------------");
      System.out.println("Enter what you would like to do: ");
      System.out.println("1 - Print the List of Customers");
      System.out.println("2 - Add New Customer");
      System.out.println("3 - Delete Customer's Reward Points");
      System.out.println("99 - Exit");
      System.out.print("Choice? ");
      choice = sin.nextInt();
      sin.nextLine();
      System.out.println();

      switch (choice) {
        case 1:
          selectionSort(inventory);
          System.out.println(printListOfCustomers(inventory));
          System.out.println();
          break;
        case 2:
          addNewCustomers(inventory);
          System.out.println();
          break;
        case 3:
          
          System.out.println();
          break;
        case 4:
          System.out.println();
          break;
        case 99:
          System.out.println("Exit.");
          sin.close();
          System.exit(0);
        default:
          System.out.println("Illegal choice.");
          System.out.println();
      }
    }
  }

  public static String printListOfCustomers (ArrayList<Customers> inventory) {
    String temp = "";
    for (int x = 0; x <= inventory.size() - 1; x++) {
      temp = temp + inventory.get(x).nlast + " "
                  + inventory.get(x).nfirst + " "
                  + inventory.get(x).wfirst + " "
                  + inventory.get(x).wsecond + " "
                  + inventory.get(x).wthird + " "
                  + inventory.get(x).wfourth 
                  + " Bonus Points: " 
                  + inventory.get(x).bonus 
                  + " Total Points: "
                  + inventory.get(x).Final + "\n";
    }
    return temp;
  }

  public static void addNewCustomers (ArrayList<Customers> inventory) {
    Customers c;
    String nlast, nfirst; 
    int wfirst, wsecond, wthird, wfourth, total, bonus, Final;
    Scanner sin = new Scanner(System.in);
    System.out.print("Enter Last Name: ");
    nlast = sin.nextLine();
    System.out.print("Enter First Name: ");
    nfirst = sin.nextLine();
    System.out.print("Enter Reward Points for the First Week: ");
    wfirst = sin.nextInt();
    System.out.print("Enter Reward Points for the Second Week: ");
    wsecond = sin.nextInt();
    System.out.print("Enter Reward Points for the Third Week: ");
    wthird = sin.nextInt();
    System.out.print("Enter Reward Points for the fourth Week: ");
    wfourth = sin.nextInt();
    total = 0;
    bonus = 0;
    Final = 0;
    c = new Customers (nlast, nfirst, wfirst, wsecond, wthird, wfourth, total, bonus, Final);
    inventory.add(c);
  }

  public static void selectionSort (ArrayList<Customers> inventory) {
    int i, j, minValue, minIndex, temp = 0;
    for (i = 0; i < inventory.length; i++) {
      minValue = inventory[i];
      minIndex = i;
      for (j = i; j < inventory.length; j++) {
        if (inventory[j] < minValue) {
          minValue = inventory[j];
          minIndex = j;
        }
      }
      if (minValue < inventory[i]) {
        temp = inventory[i];
        inventory[i] = inventory[minIndex];
        inventory[minIndex] = temp;
      }
    }
    return list;
  }
}

And this is my ArrayList:

// ArrayList <Customers> Inventory
public class Customers {
  String nlast;
  String nfirst;
  int wfirst;
  int wsecond;
  int wthird;
  int wfourth;
  int total;
  int bonus;
  int Final;

  public Customers (String _nlast, String _nfirst, int _wfirst, int _wsecond, int _wthird, int _wfourth, int _total, int _bonus, int _Final) {
    this.nlast = _nlast;
    this.nfirst = _nfirst;
    this.wfirst = _wfirst;
    this.wsecond = _wsecond;
    this.wthird = _wthird;
    this.wfourth = _wfourth;
    this.total = _wfirst + _wsecond + _wthird + _wfourth;
    this.bonus = 1000;
    this.Final = _wfirst + _wsecond + _wthird + _wfourth + _bonus;

    if (total >= 5000){
      this.bonus = 1000;
    }
    else {
      this.bonus = 0;
    }
  }
}

My first problem is sorting. I tried to use selection sort but I kept getting error that cannot find symbol.

After sorting, I have to make system that can delete and edit the information. But I have no clue..

Also, I need 10 customers' information that are already initialized. I tried like this:

public static void main(String[] args) {
    ArrayList <Customers> inventory = new ArrayList <Customers>();
    mainMenu(inventory);

    ArrayList <String> row1 = new ArrayList <String> ();
    ArrayList <String> row2 = new ArrayList <String> ();
    ArrayList <String> row3 = new ArrayList <String> ();
    ArrayList <String> row4 = new ArrayList <String> ();
    ArrayList <String> row5 = new ArrayList <String> ();
    ArrayList <String> row6 = new ArrayList <String> ();
    ArrayList <String> row7 = new ArrayList <String> ();
    ArrayList <String> row8 = new ArrayList <String> ();
    ArrayList <String> row9 = new ArrayList <String> ();
    ArrayList <String> row10 = new ArrayList <String> ();

    // Information of 10 Customers
    row1.add("Huang");
    row1.add("Renjun");
    row1.add("300");
    row1.add("200");
    row1.add("300");
    row1.add("2000");
    row2.add("Lee");
    row2.add("Jeno");
    row2.add("400");
    row2.add("230");
    row2.add("2000");
    row2.add("700");
    row3.add("Kim");
    row3.add("Doyoung");
    row3.add("2000");
    row3.add("100");
    row3.add("9600");
    row3.add("7000");
    row4.add("Jeong");
    row4.add("Jay");
    row4.add("200");
    row4.add("100");
    row4.add("400");
    row4.add("970");
    row5.add("Dong");
    row5.add("Sicheng");
    row5.add("1000");
    row5.add("20");
    row5.add("8");
    row5.add("1997");
    row6.add("Wong");
    row6.add("Yuk-hei");
    row6.add("100");
    row6.add("200");
    row6.add("5000");
    row6.add("1830");
    row7.add("Suh");
    row7.add("John");
    row7.add("2000");
    row7.add("900");
    row7.add("8400");
    row7.add("950");
    row8.add("Park");
    row8.add("Jisung");
    row8.add("2000");
    row8.add("500");
    row8.add("1800");
    row8.add("20");
    row9.add("Zhong");
    row9.add("Chenle");
    row9.add("1100");
    row9.add("2200");
    row9.add("3300");
    row9.add("4400");
    row10.add("Na");
    row10.add("Jaemin");
    row10.add("800");
    row10.add("1300");
    row10.add("2000");
    row10.add("2016");

    // Add rows to the inventory
    inventory.add(row1);
    inventory.add(row2);
    inventory.add(row3);
    inventory.add(row4);
    inventory.add(row5);
    inventory.add(row6);
    inventory.add(row7);
    inventory.add(row8);
    inventory.add(row9);
    inventory.add(row10);
  }

But it didn't work. And I don't know why..

Please help me.. I'm very new to computer science..

Sarah Choi
  • 1
  • 1
  • 3
  • what are row1-10 ? – Akash Jan 29 '21 at 04:57
  • It is the information of Customers that I have to initialize – Sarah Choi Jan 29 '21 at 04:58
  • You should be creating instances of Customer class and then add it to List . I dont think you need List of Strings. For example :- Customer customer1 = new Customer(params...) ; Customer customer2 = new Customer(params...) ; Inventory.add(customer1); Inventory.add(customer2); – Akash Jan 29 '21 at 05:03
  • What is the sorting criteria ? – once Jan 29 '21 at 05:17
  • Sorting customer's last name alphabetically – Sarah Choi Jan 29 '21 at 05:34
  • May consider using Java Stream API. It has sorting function to sort the elements based on the provided comparator. Similar post about sorting using stream - https://stackoverflow.com/questions/40517977/sorting-a-list-with-stream-sorted-in-java – once Jan 29 '21 at 11:01

1 Answers1

0

I've not read the actual logic that you are trying to do but there are a lot of problems with your code first and foremost accessing ArrayList by an index is wrong.

 minValue = inventory[i]; // is wrong... 

ArrayList elements are stored in the object and can be accessed by the get(index) method provided by the ArrayList.

example:

inventory.get(j) //is right

in the same way.

inventory.size() //is right.
inventory.length //is wrong and is no method(Reason for getting cannot find symbol.)

For more details: https://www.w3schools.com/java/java_arraylist.asp

Zahid Khan
  • 2,130
  • 2
  • 18
  • 31