1

package chapter10;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Customer {
    private String name;
    private  String streetAddress;   
    private  String phoneNumber;
    private  int total;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }      

    public  String getStreetAddress(){
        return streetAddress;
    }

    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }



    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public int getTotal(){  
        return total;       
    }

    public void setTotal(int total){
        this.total = total; 
    }

    public static void assign(){
        int a = (int) (Math.random() + 10); 
        int r = (int) (Math.random() + 10); 
        int f = (int) (Math.random() + 10); 
        System.out.println(a + r + f + "x" + "x" + "x");
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        ArrayList <Customer > customerList = new ArrayList <Customer>();
        char ans;
        do
        {
            Customer customer = new Customer(); 
            System.out.print("Customer name ");
            customer.setName(in.next());
int i = 0;
++i;
            System.out.print("Street Address ");
            customer.setStreetAddress(in.next());

            System.out.print("Phone Number ");
            customer.setPhoneNumber(in.next());

            customerList.add(customer);
            System.out.println("Enter total sales ");
            customer.setTotal(in.nextInt());

            System.out.println("Would you like to enter in a new customer       (    y/n)? ");
            String answer = in.next();
            ans = answer.charAt(0);
           ((String) customerList).concat("")

        } while(ans == 'y');

        for(Customer c: customerList){
            System.out.print(c.getName() + "\n" + "Phone number is         "            +c    .getPhoneNumber() +"\n" + "Total sales is "+ c.getTotal() + "\n" + "Address       is"+ c.getStreetAddress());

        }

        for(int i=0; i<customerList.size(); i++){
            //System.out.print(customerList.get(i).getName());
        }
    }
}

I need to assign a number to each value in the arraylist but i am getting an error that says that I have to convert to string (arraylist). How do I add it?

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
  • Read this: http://stackoverflow.com/help/how-to-ask Don't just dump your entire code here and expect us to find the error for you. – almightyGOSU Jun 03 '15 at 03:41
  • 2
    You sending me this message because you are looking for a generalized theme of people throwing their entire project up here. This is different because I know what is correct and incorrect in my code and i know that me adding a feature caused a problem. My question requires the entire code because it provides background on why I need to avoid simply switching the arraytype to String which would solve my problem. I would appreciate it if you could provide a hint in the right direction instead giving the same canned answer that you give to everyone else. – Andrew Montez Jun 03 '15 at 03:51
  • What do you mean by "assign a number"? You are already assigning a phone number and total in the main method. Your question in the current form is way too vague to answer... – James S Jun 03 '15 at 03:52
  • Add 3 random numbers one through 10 and three XXX's – Andrew Montez Jun 03 '15 at 03:53
  • 678xxx = it is a customernumber – Andrew Montez Jun 03 '15 at 03:54
  • So you want a field that's not a phoneNumber but is a customerNumber? – James S Jun 03 '15 at 03:56

4 Answers4

1

If what I gather from the comments is correct then I believe this is what you want:

Your current assign() is incorrect if you want random values 1-10, it should look like this instead:

public String assign(){
    Random rand = new Random();
    int a = rand.nextInt(10) + 1;
    int r = rand.nextInt(10) + 1;
    int f = rand.nextInt(10) + 1;
    return a+r+f+"xxx";
}

Customer will look like this:

public class Customer {
    private String name;
    private String customerNumber;
    private  String streetAddress;   
    private  String phoneNumber;
    private  int total;
    ...
    ...
    ...
    public String getCustomerNumber() { return this.customerNumber; }
    public void setCustomerNumber(String cNumber) { this.customerNumber = cNumber; }

And assigning the numbers should look like this:

    for(Customer c : customerList) {
        c.setCustomerNumber(assign());
    }

Also avoid this line of code, it's a really bad idea:

    ((String) customerList).concat("")

You should probably rename the customerNumber to customerID.

James S
  • 308
  • 1
  • 7
1

Hiiii

As i understand you are trying to to add number to each value to arrayList ,And same time you are creating arrayList of customer object, So first understand about arrayList of object,

Customer c1 = new Customer();
Customer c2 = new Customer();
ArrayList<Customer> al = new ArrayList();

al.add(c1);
al.add(c2);

Here this ArrayList object save only address of customer object so how can you change the address of Customer object ; You can not add number in Customer type ArrayList Object, There is another way typecast your ArrayList to Object type and then no need to typecast again , you can add any type of object in ArrayList like this,

Customer c1 = new Customer();
    Customer c2 = new Customer();
    ArrayList<Object> al = new ArrayList();

    al.add(c1);
    al.add(c2);
Abhay Singh
  • 152
  • 2
  • 12
0

In your code there's the following line:

((String) customerList).concat("")

Trying to cast a List to a String is doomed to failure - I'm not sure why do you think it should work.

If you want a String representation of the list, you should implement a toString() method in class Customer and then you can do something like:

System.out.println(Arrays.toString(customerList.toArray()));
Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Instead of using ArrayList, you can use Map. In which you can have the number as key and value as Customer.

http://examples.javacodegeeks.com/java-basics/java-map-example/ Contains the example of using Map

Answer in Storing a new object as the value of a hashmap? contains info about how to use Object as value in HashMap

Community
  • 1
  • 1
sag
  • 5,333
  • 8
  • 54
  • 91