Reputation: 65
I am a beginner java programmer and I am encountering a problem.
I have marked key areas of code with '// !!', I've included the rest of the program to assist any trouble shooting.
I am trying to store an instance of Person stored in the linked list clients in class Clients into a linked list called passengers in an instance of the class Car. However I'm constantly encountering the error needing to provide a string and int, two values stored in Person objects. I thought however that because I have already selected the Person class, this would be transfered? Does anyone know where I am going wrong?
To sum up, I am trying to copy one instance of an object(Person) in a linked list(In clients) into another linked list of the same objects in another class(Car) that itself an instance(there are 3 car objects)
Any assistance is greatly appreciated.
Here is the class Car that contains the passengers linked list
import java.io.*;
import java.util.*;
public class Car implements Serializable
{ private int id;
private String pilot;
private int stops;
private LinkedList<Person> passengers = new LinkedList<Person>(); // !! `Here is the linked list passengers`
private double rate = 10.00;
public int scannableId = this.id;
//
public Car(int id, String pilot, int stops)
{ this.id = id;
this.pilot = pilot;
this.stops = stops; }
public void go()
{ System.out.println("CarManager" + id + " with " + pilot);
for (int stop = 0; stop < stops; stop++)
{ charge(stop);
if (stopAt(stop))
{ showStop(stop);
pay(stop); }}}
private boolean stopAt(int i)
{ for (Person person: passengers)
if (person.uses(i))
return true;
return false; }
private void showStop(int i)
{ String s = " Stop " + i + "\n";
String on = on(i);
if (!on.isEmpty())
s += " On: " + on + "\n";
System.out.print(s); }
private String on(int i)
{ String s = "";
for (Person person: passengers)
if (person.getsOn(i))
s += person.handle() + " ";
return s; }
public int getId() {
return this.id;
}
public int printId() {
System.out.print(this.id);
return this.id;
}
public int getStops() {
return this.stops;
}
public Person AddPassenger(Person newpassenger) {
newpassenger = passengers.add(new Person(newpassenger)); // !! This is where I try add a new passenger, however I'm getting an error stating that I need to provide a String and int, the parameters of the Person class.
}
Here is the class Person
import java.io.*;
import java.text.*;
public class Person implements Serializable
{ private String name;
private int id;
private double cash = 100.00;
private int start = 0;
private int end = 0;
private double charge = 0;
public Person(String name, int id)
{ this.name = name;
this.id = id + 100; }
public boolean uses(int stop)
{ return getsOn(stop) || getsOff(stop); }
public boolean getsOn(int stop)
{ return start == stop; }
public int giveStops() {
System.out.println(this.start);
System.out.println(this.end);
return start;
}
public void setStart (int start) {
this.start = start;
}
public void setOff (int stop) {
this.end = stop;
}
public Person getPerson () {
Person person = this.Person;
return person;
}
public boolean getsOff(int stop)
{ return end == stop; }
public String handle()
{ return name + id; }
public double readCash() {
return this.cash;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
String result = " " + this.name + " (Scuba" + this.id + ") has " + formatted(this.cash);
return result;
}
private String formatted(double amount)
{ DecimalFormat twoD = new DecimalFormat("$###,##0.00");
return twoD.format(amount); }
public boolean matches(String name)
{ return this.name.equals(name) || name.equals(this.name + this.id); }
}
Here is the carManager class, it contains the linked list of Car objects.
import java.io.*;
import java.util.*;
public class CarManager implements Serializable
{ private LinkedList<Car> cars = new LinkedList<Car>();
private Clients clients = new Clients();
public CarManager(){
setup();
menu();
}
// !! Here is where the cars are created
public void setup()
{ cars.add(new Car(1, "Ed", 2));
cars.add(new Car(2, "Fred", 7));
cars.add(new Car(3, "Freda", 5)); }
public void menu() {
char choice;
while ((choice = readChoice()) !='x' ) {
switch(choice) {
case 'a': clients.makePerson(); break;
case 's': clients.showClients(); break;
case 'r':clients.removeClient(); break;
case 'b': findCar(); break;
case 'c': clients.getClient(); break;
default: showMenu();
}
}
}
private int nextInt() {
return In.nextInt();
}
public void findCar() {
System.out.print("Car: ");
int searchid = In.nextInt();
boolean carfound = false;
for (Car i: cars)
{
if (searchid == i.getId())
{
carfound = true;
System.out.println(" Stops 0-" + i.getStops());
getClientId();
Car thecar = i;
}
if (carfound == false)
System.out.println(" No such car");
}
}
public void getClientId() {
clients.theSiv();
}
private void showMenu() {
System.out.println("The menu choices are ");
System.out.println(" a: add a client");
System.out.println(" r: remove a client ");
System.out.println(" b: make a booking");
System.out.println(" g: go (run the s)");
System.out.println(" s: show the clients");
System.out.println(" f: store and exit");
System.out.println(" x: exit");
}
private char readChoice() {
System.out.print("Your choice: ");
return In.nextChar();
}
public void exit() {
System.exit(0);
}
}
Here is the Clients class, this contains the linked list clients that contains the instances of Person that I want to add to an instance of Car
import java.io.*;
import java.util.*;
public class Clients implements Serializable
{ private LinkedList<Person> clients = new LinkedList<Person>();
private int id = 1;
public Clients()
{ clients.add(new Person("Homer", id++));
clients.add(new Person("Marge", id++));
}
public void makePerson(){
clients.add(createPerson());
}
public void removeClient() {
String searchname = readName();
boolean removed = false;
for (Person i:clients)
if (i.matches(searchname)){
clients.remove(i);
removed = true;
}
if (removed == false)
System.out.println(" No such client");
}
public void theSiv() {
System.out.println(" CarManager id: ");
String s = In.nextLine();
boolean isValidInteger = false;
char choice = 'p';
int foundid = 0;
boolean exists = false;
int searchid = 0;
try
{
int i = Integer.parseInt(s);
isValidInteger = true;
// if (isValidInteger = true)
searchid = i;
}
catch (NumberFormatException ex)
{
choice = s.charAt(0);
}
if (choice != 'x') {
for (Person b:clients)
if (b.getId() == searchid){
exists = true;
giveStops(b);
}
if(exists == false) {
System.out.println(" No such client");
resartTheSiv();
}
}
}
public void resartTheSiv(){
theSiv();
}
public int giveStops(Person i) {
String input;
System.out.println(" Trip: ");
input = In.nextLine();
String[] split = input.split(" +");
int start = Integer.parseInt(split[0]);
int stop = Integer.parseInt(split[1]);
i.setStart(start);
i.setOff(stop);
i.addPassenger(i); `// !! Here is where I try to add the selected instance of Person to car by first calling a method in Person to send itself to the passenger linked list.`
resartTheSiv();
return start;
}
public void getClient() {
System.out.print(" CarManager id: ");
char choice;
int searchid = In.nextInt();
boolean exists = false;
while ((choice = In.nextChar()) !='x' ){
for (Person i:clients)
if (i.getId() == searchid){
exists = true;
System.out.println("found client");
}
if (exists == false)
System.out.println(" No such client");
}
}
public Person createPerson()
{
String name = readName();
Person p = new Person(name, id++);
System.out.println(" Hi " + p.getName() + ", " + "you are CarManager" + p.getId());
return p;
}
private String readName() {
System.out.print(" Name: ");
return In.nextLine();
}
public void showClients() {
for (Person i: clients)
System.out.println(i);
}
}
And here is the Root class, there is where the program is run from
import java.io.*;
public class Root
{ public Root() {
new CarManager();
}
public static void main(String[] args)
{ new Root(); }
private CarManager carManager;
}
Here is where input is processed if that helps also
import java.util.*;
public class In
{ private static Scanner in = new Scanner(System.in);
public static String nextLine()
{ return in.nextLine(); }
public static char nextChar()
{ return in.nextLine().charAt(0); }
public static int nextInt()
{ int i = in.nextInt();
in.nextLine();
return i; }
public static double nextDouble()
{ double d = in.nextDouble();
in.nextLine();
return d; }
}
Upvotes: 0
Views: 3208
Reputation: 1
Is this for Application Programming?
If so your doing it completely wrong as you need to use a boat NOT A CAR!
Upvotes: 0
Reputation: 7032
How about just calling .add
on passengers? No need to construct a new Person object or set the value of passengers in any way.
public void AddPassenger(Person newpassenger) {
passengers.add(newpassenger);
}
Also a small note - usually method names are camel case, so it'd be better to call the method addPassenger
instead of AddPassenger
Upvotes: 1