0

I have an ArrayList of Customers, and I want to add some elements of this list but not all of the Customers into another class called PayingCustomers . How would I be able to add specific values of an ArrayList into my class constructor? Kindly ignore some of the commented lines I have which is just me working on other elements of my project.

Customer.java:

//
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : Customer.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //

    package javaapplication1;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    public class Customer {

        private String name;
        private String email;
            private List <Customer> list;
        private Magazine magazines;


            public void SetName(String name){

                this.name = name;

            }
        public void SetEmail(String email){

                this.email = email;

            }
        public String GetName(){

                return name;

            }
        public String GetEmail(){

                return email;

            }

            public List<Customer> getList() {

                return list;

            }

            public void setList(List<Customer> list) {

                this.list = list;

            }

            public Customer(String name, String email, Magazine magazines){

                this.name = name;
                this.email = email;
                this.magazines = magazines;


            }

            public String toString() {

            return "\nName: " + name + " Email: " + email + "\nMagazine Details: " + magazines + "\n";


            }

    }

PayingCustomer.java:

//
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : PayingCustomer.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //

    package javaapplication1;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    public class PayingCustomer{
        private String paymentmethod;
        private List <Customer> list;
            //private List <Associate> list2;

        public void SetPaymentMethod(String paymentmethod){

                this.paymentmethod = paymentmethod;

            };
        public String GetPaymentMethod(){

                return paymentmethod;

            };


            public PayingCustomer(List<Customer> list, String paymentmethod){

                this.list = list;
                this.paymentmethod = paymentmethod;
                //this.list2 = list2;

            }

            public String toString() {

                return "List of Paying Customers: " + list;

            }
    }

Main:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaApplication1 {
    public static void main(String[] args) {


        List<Supplement> supplements = new ArrayList<Supplement>();

        supplements.add(new Supplement("Sports Illustrated Special", 4));
        supplements.add(new Supplement("Health and Nutrition", 2));
        supplements.add(new Supplement("Lifestyled", 5));
        supplements.add(new Supplement("Gamer's Update", 3));

        /*supplements2[0] = new Supplement("Horse Racing", 4);
        supplements2[1] = new Supplement("7 Seven", 2);
        supplements2[2] = new Supplement("Guru of the North", 5);
        supplements2[3] = new Supplement("Electrify", 3);*/


        Magazine magazineobj3 = new Magazine("The Wheels Special", 35, supplements);



        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        Magazine magazineobj2 = new Magazine("Free your think", 15, supplements);

        List <Customer> customers = new ArrayList<Customer>();
        customers.add(new Customer("Morgan Freeman","[email protected]", magazineobj3));
        customers.add(new Customer("George Bush","[email protected]", magazineobj3));
        customers.add(new Customer("Jamie Carragher","[email protected]", magazineobj3));
        customers.add(new Customer("Sarah Williams","[email protected]", magazineobj3));
        customers.add(new Customer("Nathan Bledsoe","[email protected]", magazineobj3));
        customers.add(new Customer("Phillip Franklin","[email protected]", magazineobj3));

        List <PayingCustomer> payingCustomers = new ArrayList<PayingCustomer>();



        //System.out.println(magazineobj3);
        //System.out.println(magazineobj2);
        System.out.println(customers);

    }

}

1 Answer 1

2

Why not add a field payingCustomer in the Customer class? And set that to true only for paying customers. Or make PayingCustomer to extend Customer class and add for paying customer extra functionalities.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a good idea, thank you for that. I've already now managed to add 2 Customers as objects to my Paying Customers ArrayList using the method get from ArrayList. I have another question I hope you can answer. I've overrided my toString method in Customer already to display the details of all Customers. Now I need a method that prints out an email to all of the customers in that list, but I can't use toString because it is already overriden. When I create a normal method, it won't let me use it in main with the error, non-static method cannot be referenced from a static context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.