Frage

I'm try to create one simple reservation system, we'll read a file, then we'll add Train, Bus, etc., then we'll writer everything to output.

import java.io.*;
import java.util.*;
public class Company
{

private static ArrayList<Bus> bus = new ArrayList<Bus>();
static int buscount = 0, traincount = 0;
public static void main (String[] args) throws IOException
{

    FileParser();
}
public Company()
{

}

public static void FileParser()
{

     try { 
          File file = new File(); //i fill this later
          File file2 = new File(); // i fill this later
          FileInputStream fis = new FileInputStream(file); 
          FileOutputStream fos = new FileOutputStream(file2); 
          BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); 
          String line; 
          while ((line = br.readLine()) != null) 
        { 
            String[] splitted = line.split(",");
            if(splitted[0].equals("ADDBUS"))
            {
                bus.add(buscount) = Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]);
            }

        }
     }
        catch (FileNotFoundException fnfe) { 

        } 
        catch (IOException ioe) { 

        } 
     }
}

I try to read the file line by line. For example one of the line is "ADDBUS,78KL311,10,140,54" I split the line for "," then i try to add every pieces of array to Bus' class' constructor but i couldn't figured it out.

My Bus Class is like ` public class Bus extends Vehicle{

private String command;
private String busName;
private String busPlate;
private String busAge;
private String busSpeed;
private String busSeat;

public Bus(String command, String busname, String busplate, String busage, String busspeed, String busseat)
{
    this.command = command;
    this.busName = busname;
    this.busPlate = busplate;
    this.busAge = busage;
    this.busSpeed = busspeed;
    this.busSeat = busseat;
}

public String getBusName() {
    return busName;
}

public void setBusName(String busName) {
    this.busName = busName;
}

public String getBusPlate() {
    return busPlate;
}

public void setBusPlate(String busPlate) {
    this.busPlate = busPlate;
}

public String getBusAge() {
    return busAge;
}

public void setBusAge(String busAge) {
    this.busAge = busAge;
}

public String getBusSpeed() {
    return busSpeed;
}

public void setBusSpeed(String busSpeed) {
    this.busSpeed = busSpeed;
}

public String getBusSeat() {
    return busSeat;
}

public void setBusSeat(String busSeat) {
    this.busSeat = busSeat;
}

public String getCommand() {
    return command;
}

public void setCommand(String command) {
    this.command = command;
}
   } 

can someone show me a way to solve this problem?

Thank you,

War es hilfreich?

Lösung

You are missing the keyword new to create a new instance of the class:

bus.add(new Bus(...));

Andere Tipps

You can add items to ArrayList like this

bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));

you were missing new keyword before Bus constructor call. Then you can increment the counter (or do whatever)

bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));
buscount++;

try to add new Bus(...)

bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));

As I understand if you want to call constructor you need to call new Bus(parms). when you say new it will call constructor of your class


when you say this() again it going to call enclosing class' constructor


if you say super() it will call super class' constructor.

if you want it into a map order by counter you can use this:

Map(Integer, Bus) busPosition = new HashMap<>();

busPosition.put(buscount, new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top