-2

I'm currently trying to make an Object with Java Scanner input.

This object CashCard contains the fields amount, name & amountcardisused.

But i'm not sure how i can get inputs from java Scanner and than move it inside my ArrayList CashCard.

This is mycode so far, i'm not sure how to continue, hopefully you can help me out.

Class Cashcard

package cashcard;

import java.util.ArrayList;

public class CashCard {

    private double amount;
    private String name;
    private int amountcardisused;
    private ArrayList<CashCard> listofcashcards;

    public CashCard(String name, double amount, int amountcardisused){
        this.amount = amount;
        this.name = name;
        this.amountcardisused = amountcardisused;
        listofcashcards = new ArrayList<CashCard>();
}

    public String setName(String name){
        return name;
    }
    public double setAmount(double amount){
        return amount;
    }
    public int setTimesCardIsUsed(int amountcardisused){
        return amountcardisused;
    }
}

Class TestCashCard

package cashcard;

import java.util.*;

public class TestCashCard {

    ArrayList<CashCard> listofcashcards = new ArrayList<CashCard>();

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String input;

        // Stop console when entering 0 in console
        System.out.println("0: Stop Program");
        System.out.println("1: Add CashCard");

        // Execute this code when entering 1 in console
        System.out.println();
        System.out.print("Enter 0 or 1: ");
        input = in.nextLine();

        if(input.equals("0")){
        System.exit(0);
        }

        else if(input.equals("1")){

        System.out.print("Enter name of the card owner: ");
        String yourname = in.nextLine();

        System.out.println("Enter the amount you want to set on your cashcard: ");
        double youramount = in.nextDouble();

        System.out.print("Enter The Amount This Card Is Already Used: ");
        int youramountcardisused = in.nextInt();

        // Do Something to create an CashCard object by using the scanner its input 
        // and than put the object inside the ArrayList CashCard.
        // Need help on this part

    }

    else{
        System.out.print("No choice is made, close program");
        System.exit(0);
    }
  }
}
2
  • you have all three values that you need to call CashCard constructor. just call contructor to create object and then add it in list.
    – Adi
    Sep 3, 2014 at 13:17
  • @Caleryn - He has list of CashCard in TestCashCard class as well.
    – Adi
    Sep 3, 2014 at 13:22

3 Answers 3

1

Your CashCard shouldn't contain an ArrayList of CashCard objects. You can remove that and keep your ArrayList in your main class.

So you could do this in your main:

ArrayList<CashCard> myCashCards = new ArrayList<CashCard>();

//Scanner input here.

CashCard cashCard = new CashCard(yourname, youramount, youramountcardisused);

myCashCards.add(cashCard);
0

Only thing you have to do is

public CashCard(String name, double amount, int amountcardisused){
    this.amount = amount;
    this.name = name;
    this.amountcardisused = amountcardisused;
    //remove the below line from here
    listofcashcards = new ArrayList<CashCard>();// dont include this here
  }

and in your testcashcard class

after

  System.out.print("Enter The Amount This Card Is Already Used: ");
        int youramountcardisused = in.nextInt();

add

CashCard cashcard1 = new CashCard("abc",1000,2);

listofcashcards.add(cashcard1);
1
  • class name is CashCard
    – Adi
    Sep 3, 2014 at 13:22
0

If your desire is to keep a list of all created CashCards inside your CashCard class (rather than your test class), make your ArrayList field static, like so:

private double amount;
private String name;
private int amountcardisused;
// change your line below to this
private static ArrayList<CashCard> listofcashcards = new ArrayList<CashCard>();

Write two access methods for your ArrayList field (again in your CashCard class):

public static void addCashCard(CashCard cc) {
    listofcashcards.add(cc);
}

public static ArrayList<CashCard> getAllCashCards() {
    // Return new list copied from field (for field protection)
    return new ArrayList<CashCard>(listofcashcards);
}

Then, when you want to add a new card using your test class, use your constructor and the addCashCard method to store the new card in your shared field:

...

System.out.print("Enter The Amount This Card Is Already Used: ");
int youramountcardisused = in.nextInt();

// Create your object using the constructor, and add it to your list,
// which lives inside your CashCard class
CashCard.addCashCard(new CashCard(yourname, youramount, youramountcardisused));

...

Voila! If you want to access all your cards from your test class simply call CashCard.getAllCashCards()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.