【问题标题】:Not sure how to create an object with Java Scanner and storing it inside an ArrayList不确定如何使用 Java Scanner 创建对象并将其存储在 ArrayList 中
【发布时间】:2014-09-03 13:14:03
【问题描述】:

我目前正在尝试使用 Java Scanner 输入创建一个对象。

此对象CashCard 包含字段amountnameamountcardisused

但我不确定如何从 java Scanner 获取输入并将其移动到我的 ArrayList CashCard 中。

到目前为止,这是我的代码,我不知道如何继续,希望你能帮助我。

班级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;
    }
}

班级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);
    }
  }
}

【问题讨论】:

  • 您拥有调用CashCard 构造函数所需的所有三个值。只需调用构造函数创建对象,然后将其添加到列表中。
  • @Caleryn - 他在TestCashCard 类中也有CashCard 的列表。

标签: java object arraylist java.util.scanner


【解决方案1】:

您的CashCard 不应包含ArrayListCashCard 对象。您可以删除它并将您的 ArrayList 保留在您的主课程中。

所以你可以在你的 main 中这样做:

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

//Scanner input here.

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

myCashCards.add(cashCard);

【讨论】:

    【解决方案2】:

    你唯一需要做的就是

    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
      }
    

    在你的 testcashcard 类中

    之后

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

    添加

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

    【讨论】:

    • 类名是CashCard
    【解决方案3】:

    如果您希望在 CashCard 类(而不是测试类)中保留所有已创建 CashCard 的列表,请将 ArrayList 字段设为静态,如下所示:

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

    为您的 ArrayList 字段编写两个访问方法(同样在您的 CashCard 类中):

    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);
    }
    

    然后,当您想使用测试类添加新卡时,请使用构造函数和 addCashCard 方法将新卡存储在共享字段中:

    ...
    
    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));
    
    ...
    

    瞧!如果你想从你的测试类中访问你所有的卡片,只需调用 CashCard.getAllCashCards()

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2012-09-20
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2020-03-21
      • 2017-08-31
      相关资源
      最近更新 更多