创建一个打印对象,在java中存储字符串和整数



我有以下问题。五个班级正在互相交流。两个主题做得很好。但是随着在我的主类Event中创建一个类(Ticket)的对象(从另一个类(UserInput)获取用户输入,在构造函数中处理这个),我现在有显示结果的问题。

带有主方法的主类Event

import java.util.ArrayList;
public class Event {

private static String artistName;
private static int artistSalary;
private Language language;
private static ArrayList<String> trackList;
private InputReader inputReader;
private Ticket ticket;
private int amountOfTicketCategories;
private static Object[] ticketList;
private static int index;



public Event() {

}

public Event(Artist artist, Ticket ticket) {
artistName = artist.getArtistName();
artistSalary = artist.getArtistSalary();
trackList = artist.getArrayList();
for (index = 0; index < amountOfTicketCategories; index++) {
ticketList[index] = ticket.getTicket();
ticketList[index].toString();
}
}

public void chooseWhatToDefine() {
language = new Language();
language.whatToSpecify();
}

public void setTicketPrice(String ticketCategory, int ticketPrice) {

}

public void displayArtist(String artistName, int artistSalary) {
language = new Language();
language.displayArtistAndSalary(artistName, artistSalary);
}

public void displayTracklist(ArrayList<String> trackList) {
language = new Language();
language.displayTrackList(trackList);
}

public void displayTickets(Object[] ticketList) {
language = new Language();
language.displayTicket(ticketList);
}           

public static void main(String[] args) {
Event event1 = new Event(new Artist(), new Ticket());

event1.displayArtist(artistName, artistSalary);
event1.displayTracklist(trackList);
event1.displayTickets(ticketList);

}
}

票据类,其构造函数使用来自InputReader类的用户输入初始化该类,并创建string和integer类型的对象。

import java.util.Arrays;
public class Ticket {

private static String ticketCategory;
private static int ticketAmount;
private static int ticketPrice;
private InputReader inputReader;
private int amountOfTicketCategories;
private int index;
private Ticket[] ticketList;

public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {

}

public Ticket() {
inputReader = new InputReader();
inputReader.specifyTicketCategories();
ticketList = new Ticket[amountOfTicketCategories];
for (index = 0; index < amountOfTicketCategories; index++) {
inputReader.specifyTicket(ticketCategory, ticketAmount, ticketPrice);
ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);


}
}

public String toString() {
return("TicketCategory: " + ticketCategory + "Amount of Tickets: " + ticketAmount + "Ticket Price: " +ticketPrice);
}
public Object getTicket() {
return ticketList[index];
}
public int getAmountOfTicketCategories() {
amountOfTicketCategories = inputReader.specifyTicketCategories();
return amountOfTicketCategories;


}
}
处理用户输入的InptReader类:

import java.util.ArrayList;
import java.util.Scanner;
public class InputReader {
private  Scanner sc;
private  Language language;
private  ArrayList <String> tracks;





public InputReader() {
tracks = new ArrayList<String>();
language = new Language();
sc = new Scanner(System.in);


}
public int specifyTicketCategories() {
language.defineAmountOfTicketCategories();
return sc.nextInt();


}

public void specifyTicket(String ticketCategory, int ticketAmount, int ticketPrice) {
language.specifyTicketCategory();
ticketCategory = sc.next();
language.specifyTicketAmount();
ticketAmount = sc.nextInt();
language.specifyTicketPrice();
ticketPrice = sc.nextInt();

}

public int amountOfTickets() {
return sc.nextInt();
}

public int ticketPrice() {
return sc.nextInt();
}


public String readName() {
language.specifyArtist();
return sc.nextLine();
}
public int readInteger() {
language.specifyArtistSalary();
return sc.nextInt();
}
public void addTitle() {
int anzahlSongs = 3;
int index = 0;
while (index < anzahlSongs) {
language.specifyTrackList();
tracks.add(sc.nextLine());
index++;
}

}
public  ArrayList<String> getArray() {
return tracks;
}
}

由语言语句

组成的语言类
import java.util.ArrayList;
public class Language {

public Language () {


}
public void whatToSpecify() {
System.out.println("What would you like to specify fist? For Artist press 1, for Ticket press 2");
}

public void specifyArtist() {
System.out.println("Who is the artist? ");
}

public void specifyArtistSalary() {
System.out.println("How much does the artist earn? ");
}
public void displayTicket(Object[] ticketList) {
System.out.println("Ticketlist: " + ticketList);
}

public void displayArtistAndSalary(String artistName, int artistSalary) {
System.out.println("Artist: " + artistName + " " + "Salary: " + artistSalary);
}
public void displayTrackList(ArrayList<String> trackList) {
System.out.println("Tracklist: " + trackList);
}
public void specifyTicketCategory() {
System.out.println("What is the ticket category? ");
}
public void specifyTicketAmount() {
System.out.println("What ist the amount of tickets? ");
}
public void specifyTicketPrice() {
System.out.println("What is the price for your ticket?");
}
public void specifyTrackList() {
System.out.println("Add title: ");
}
public void defineAmountOfTicketCategories() {
System.out.println("How many ticket categories you like to set up?");
}
public void line() {
System.out.println("***********************************");
}
}

艺术家类,它在主类中创建了一个艺术家的实例(与ticket的想法相同),但带有其他变量和参数。

import java.util.ArrayList;
public class Artist {
private int artistSalary;
private String artistName;
private InputReader inputReader;
ArrayList <String> trackList;



public Artist() {


inputReader = new InputReader();
artistName =    inputReader.readName();
artistSalary = inputReader.readInteger();
inputReader.addTitle();
trackList = inputReader.getArray();
trackList.remove(2);


}
public String getArtistName() {
return artistName;
}
public int getArtistSalary() {
return artistSalary;
}


public ArrayList<String> getArrayList(){
return trackList;
}
}

控制台中输出:添加标题:你好添加标题:你好您想设置多少种机票类别?5艺术家:大卫薪水:5000歌曲列表:[,你好]Ticketlist:空

首先,在Ticket类的构造函数中,您使用另一个构造函数(具有3个参数的构造函数),该构造函数的主体为空。

ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);

public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
//this is empty
}

这意味着你正在创建一个对象…

试试这个:

public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
this.ticketCategory = ticketCategory;
this.ticketAmount = ticketAmount;
this.ticketPrice = ticketPrice;
}

那么,你的getTicket方法是错误的。永远不要定义"索引"。

public Object getTicket() {
return ticketList[index];
}

,"index"是未定义的。

ticketList不应该在Ticket class =>每次创建Ticket实例时,它都可能与前一个不一样。

相关内容

  • 没有找到相关文章

最新更新