Home > Software design >  Is there a way to implement generics to have an ArrayList take in a boolean?
Is there a way to implement generics to have an ArrayList take in a boolean?

Time:10-07

I have the following code, but the ArrayList is not taking in 'Date' type. The only thing I can think of is to incorporate generics. However, I am running into problems incorporating it into the arraylist. I did some search online and have not been able to find a good source for it.

Main:

public class Main {

    public static void main(String[] args) {

        while(true)
        {
            ATM atm = new ATM();
            atm.init();
            atm.run();
        }
    }
}

class:

import java.util.Scanner;

class ATM{

    private LCD lcd;
    private CardReader cardReader;
    private Keypad keypad;
    private CashDispenser cashDispenser;

    public void init(){

        System.out.println("ATM.init()");


        lcd = new LCD();
        lcd.init();

        cardReader = new CardReader();
        cardReader.init();



        cashDispenser = new CashDispenser();
        cashDispenser.init();
    }

    public void run(){
        int count = 0;
        boolean correct;
        int tries = 0;
        System.out.println("ATM.run()");

        System.out.println("Please select a four digit pin");
        Scanner pin = new Scanner(System.in);
        int pins = pin.nextInt();
        int pins2 = pins;

        keypad = new Keypad();
        while(pins != pins2 && tries <= 4)
        {
            pins2 = keypad.init();
            count  ;
            tries  ;

        }

        if(pins == pins2)
        {
            correct = false;
        }
        else
        {
            correct = true;
        }


        String BANK_NAME = "NewCo";

        System.out.println("ATM Version 0.1.0");
        System.out.println("Copyright (C) 2020"   BANK_NAME);
        System.out.println("Code by Julian Blanco");

        User user = new User(lcd, cardReader,keypad, cashDispenser, count, correct);
        while(true){
            user.io();
        }
    }
}

class:

import java.util.Date;
import java.util.ArrayList;

public class UserInfo<T>
{
    private ArrayList<T> values;

    public UserInfo(){
        this.values = new ArrayList<T>();
    }

    public void setValue(Date date,int count, boolean correct)
    {
        this.values.add(date, count, correct);

    }
}

Class:

import java.util.ArrayList;
import java.util.Date;


public class Logger
{
    ArrayList<UserInfo> obj = new ArrayList<UserInfo>();

    public Logger(Date date, int count, boolean correct){
        obj.get(0).setValue(date,count, correct);
    }

}

Error:

java: incompatible types: java.util.Date cannot be converted to T

CodePudding user response:

You cannot add Data the the List of T generic type. There is a problem in the setValue method.

public class UserInfo<T>
{
    private ArrayList<T> values;

    public UserInfo(){
        this.values = new ArrayList<T>();
    }

    public void setValue(T value)
    {
        this.values.add(value);

    }
}

Moreover, is it really necessary to use generics in the UserInfo class? If you want to store the date, count and correct values you should create a class for it.

In addition, I would get the depdendencies of the ATM object (lcd,...) in a Constructor and only initailize them in the init method. It facilitate the testing of your code.

class ATM{

    private LCD lcd;
    private CardReader cardReader;
    private Keypad keypad;
    private CashDispenser cashDispenser;

    public ATM(LCD lcd, CardReader cardReader, Keypad keypad, CashDispenser cashDispenser){
        this.lcd = lcd;
        this.cardReader = cardReader;
        this.keypad = keypad;
        this.cashDispenser = cashDispenser;
        }

    public void init(){
        System.out.println("ATM.init()");
        lcd.init();
        cardReader.init();
        cashDispenser.init();
    }

CodePudding user response:

I guess what you really would like to do is use UserInfo as a class to hold your data and save that data in a generic Java List.

So define UserInfo like this:

public class UserInfo {

  private final Date date;
  private final int count;
  private final boolean correct;

  public UserInfo(Date date, int count, boolean correct) {
    this.date = date;
    this.count = count;
    this.correct = correct;
  }

  public Date getDate() {
    return date;
  }

  public int getCount() {
    return count;
  }

  public boolean isCorrect() {
    return correct;
  }
}

And then you can use the normal List.add like this:

List<UserInfo> userInfo = new ArrayList<>();
userInfo.add(new UserInfo(someDate, 1, true));

CodePudding user response:

First thing we need to note is that your class UserInfo is a generic class

The second thing is that the ArrayList<T> is also a generic class; however, the generic type specified is whatever type is specified as the same as the type specified in for UserInfo

So if we do UserInfo<String> info = new UserInfo<String>(); the ArrayList<T> would effectively be ArrayList<String>.

In order to resolve your problem you should make your UserInfo something like

import java.util.Date;
import java.util.ArrayList;

public class UserInfo {
    private ArrayList<Date> values;

    public UserInfo(){
        this.values = new ArrayList<Date>();
    }

    public void setValue(Date date, int count, boolean correct){
        this.values.add(date, count, correct);
    }
}

Although you didn't ask about this, the I'm fairly certain that ArrayList does not have an overload for add that takes 3 arguments.

  • Related