将新元素添加到我的arraylist中会显示错误

时间:2019-01-13 12:24:42

标签: java arraylist

我正在尝试将新元素添加到我的arraylist中,但是显示er * or,我不知道为什么。 我试图更改它,但错误不会消失。我想不出其他方法。如果您要修复它,请帮助我。 这是我的代码:

//main class
import java.util.ArrayList;

public class Ticket {

    static ArrayList<T> arrayticket = new ArrayList <T>();   //T for Ticket( Generic Type)
public static void main(String[] args) {

 BuyTicket ticket1 = new BuyTicket();  
                ticket1.BuyaTicket();
}

class T {
    // instance variables
    private String type;
    private int RoutesOrDays;

    private String kind;
    private String name;
    private String email;

    private int TicketCode;
    private String ExpirationDate;




    //constructor
    public T(String type, int RoutesOrDays, String kind, String name, String email, int TicketCode, String ExpirationDate) {
        this.type = type;
        this.RoutesOrDays = RoutesOrDays;
        this.kind = kind;
        name = " ";
        email = " ";
        TicketCode = 0;
        ExpirationDate = " ";
    }
//++setters aand getters

}

public class BuyTicket extends MyInterface {

//METHOD
    int BuyaTicket() {
Save save_dataA = new Save();
                            save_dataA.Person3(type, hostOfRoutes, kind); 
     }
} 


public class SaveData extends T {
private String name;
    private String email;
    private int code;

    static AtomicInteger codeSequence = new AtomicInteger();  //Creates a new AtomicInteger with initial value 0

    //contructor
    public SaveData(String name, String email, int code ) {

        name =" ";
        email =" ";
        this.code = codeSequence.incrementAndGet();  //Atomically increments by one the current value.

    }

    //contructor of super 
    public SaveData(String type, int RoutesOrDays, String kind, String name, String email, int TicketCode, String ExpirationDate) {
        super(type, RoutesOrDays, kind, name, email, TicketCode, ExpirationDate);
    }

    void Person3(String type, int RoutesOrDays, String kind){    
        System.out.println("Type your full name.");
        String name = input.nextLine();
        System.out.println("Please type your email. ");
        String email = input.nextLine();
        code = codeSequence.incrementAndGet();

        arrayticket.add(new T(type, RoutesOrDays, kind, code,email, name));
//here is the er*or
    }

我对Java还是很陌生,所以我可以更改些什么来修复它??

1 个答案:

答案 0 :(得分:0)

您说您是Java的新手,但您仍然决定使用许多类,接口和对Java泛型的一些误解。我决定尽可能简化您的代码,因为保持简单是编程的很好规则。

由于我不了解您的类BuyTickets,因此我将T移到了Ticket类中,并将其放入了数据类中,然后只让SaveData类负责收集数据。

还有一些奇怪的地方,例如我必须进行硬编码的某些参数,您只能添加一张票证,但我希望这会帮助您前进

SaveData类

public class SaveData {
    Scanner input;

    public SaveData() {
        input = new Scanner(System.in);
    }

    public Ticket buyTicket(String type, int routesOrDays, String kind) {
        System.out.println("Type your full name.");
        String name = input.nextLine();
        System.out.println("Please type your email. ");
        String email = input.nextLine();

        return new Ticket(type, routesOrDays, kind, name, email);
    }
}

和主要的机票类

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class Ticket {
    static ArrayList<Ticket> arrayTicket = new ArrayList<>();
    private String type;
    private int routesOrDays;

    private String kind;
    private String name;
    private String email;
    private int ticketCode;
    private String expirationDate;
    static AtomicInteger codeSequence = new AtomicInteger();

    public Ticket(String type, int routesOrDays, String kind, String name, String email) {
        this.type = type;
        this.routesOrDays = routesOrDays;
        this.kind = kind;
        this.ticketCode = codeSequence.incrementAndGet();
        this.email = email;
        this.name = name;
        expirationDate = " ";
    }

    public static void main(String[] args) {
        SaveData save = new SaveData();
        String someType = "type";
        int someRoute = -1;
        String someKind = "kind";
        arrayTicket.add(save.buyTicket(someType, someRoute, someKind));

        for (Ticket ticket: arrayTicket) {
            System.out.printf("Ticket [%d] for %s, %s\n", ticket.ticketCode,ticket.name,ticket.email);
        }        
    }
}