I can't figure out why it is only printing the last line.
I also can't seem to get the count of items to work either.
Any help is greatly appreciated.
Test File
Bike,Schwinn,100,Candy Apple Red
Car,Ford,5000,Light Blue,150,4
MotorCycle,Honda,2000, Gold,75
MotorCycle,Harley,20000,Black,100
Bike,Specialized,500,White
Bike,Kiddy,50,Pink
currently printing
Tara
CMPSC 22 Final 2012
A 2 Wheeled, Pink Kiddy bicycle.
Should Print
Tara
CMPSC 22 Final 2012
Tara is a homeowner with 6 items in the garage:
A 2 wheeled, Candy Apple Red Schwinn bicycle
A Light Blue Ford Car with 4 doors and 150 horsepower.
A 2 wheeled, Gold Honda Motor Cycle with 75 horsepower.
A 2 wheeled, Black Harley Motor Cycle with 100 horsepower.
A 2 wheeled, White Specialized bicycle
A 2 wheeled, Pink Kiddy bicycle
Tara has a total value of $27650.0 in the garage.
I also can't seem to get the count of items to work either.
Any help is greatly appreciated.
Test File
Bike,Schwinn,100,Candy Apple Red
Car,Ford,5000,Light Blue,150,4
MotorCycle,Honda,2000, Gold,75
MotorCycle,Harley,20000,Black,100
Bike,Specialized,500,White
Bike,Kiddy,50,Pink
currently printing
Tara
CMPSC 22 Final 2012
A 2 Wheeled, Pink Kiddy bicycle.
Should Print
Tara
CMPSC 22 Final 2012
Tara is a homeowner with 6 items in the garage:
A 2 wheeled, Candy Apple Red Schwinn bicycle
A Light Blue Ford Car with 4 doors and 150 horsepower.
A 2 wheeled, Gold Honda Motor Cycle with 75 horsepower.
A 2 wheeled, Black Harley Motor Cycle with 100 horsepower.
A 2 wheeled, White Specialized bicycle
A 2 wheeled, Pink Kiddy bicycle
Tara has a total value of $27650.0 in the garage.
/** * Main class: Driver * // so far prints last item on list * @author Tara * @version 12/8/2012 */ public class Driver { private static final String name = "Tara"; private static final String title = "CMPSC 22 Final 2012"; public static void main(String[] args) { // prints out name and title of program System.out.println(name + "\n" + title); // instantiates a garage Garage g1 = new Garage(); // instantiates a homeowner and passes it name and the garage HomeOwner h1 = new HomeOwner(name, g1); // prints out list of items in garage System.out.println(g1); // prints out total value of items in garage //System.out.println(h1.showValue()); // not the correct value either //System.out.println(name + " is a homeowner with " + /*count+*/ " items in the garage:"); //count not working. } } /** * Write a description of class Garage here. * * @author Tara Sturgis * @version 12/8/2012 */ public class Garage { private int count; private double val; private Vehicle[] vehicleList; String x = ""; // default constructor public Garage() { count = 0; vehicleList = new Vehicle[10]; } //adds vehicles to the list of vehicles public void addVehicle(Vehicle addVehicle) { count ++; for (int i = 0; i < count; i++) { vehicleList[i] = addVehicle; //may cause count to be to high } } // rethink, got idea for this online public double getValue() { for (int i = 0; i < count; i++) { double curValue = vehicleList[i].getValue(); val = val + curValue; } return val; } public String toString() { String foo; //could be problamatic due to length longer then actual length for(int i = 0; i < count; i++) { x = vehicleList[i].toString(); } foo = x; return foo; } } /** * Write a description of class HomeOwner here. * * @author Tara * @version 12/8/2012 */ import java.util.*; // For Scanner import java.io.*; public class HomeOwner extends Person { // Instance variables // instantiates a garage private Garage garage; public String owner = "Tara"; String [] itemList = new String [10]; // Default constructor public HomeOwner() { super(); garage = null; } //Constructor public HomeOwner(String ownerName, Garage garage) { super(ownerName); //calls superClass this.garage = garage; try { readItems(); } catch (FileNotFoundException e) { e.printStackTrace(); } } // method to read file and creates vehicles private void readItems() throws FileNotFoundException { File inFile = new File("items.txt"); Scanner in = new Scanner(inFile); int i = 0; while (in.hasNextLine()) { itemList [i] = in.nextLine(); Scanner line = new Scanner(itemList[i]); line.useDelimiter(","); if (itemList[i].contains("Car")) { Car newCar; String type = line.next(); String maker = line.next(); double value = line.nextDouble(); String color = line.next(); int hP = line.nextInt(); int doors = line.nextInt(); newCar = new Car(maker, value, color, hP, doors, owner); garage.addVehicle(newCar); } else if (itemList[i].contains("Bike")) { Bike newBike; String type = line.next(); String maker = line.next(); double value = line.nextDouble(); String color = line.next(); newBike = new Bike(maker, value, color, owner); garage.addVehicle(newBike); } else if (itemList[i].contains("MotorCycle")) { Motorcycle newMotorcycle; String type = line.next(); String maker = line.next(); double value = line.nextDouble(); String color = line.next(); int hP = line.nextInt(); newMotorcycle = new Motorcycle(maker, value, color, hP, owner); garage.addVehicle(newMotorcycle); } i++; } //Instantiate a vehicle based on each line read, passing all parameters to the constructor. //Then add the vehicle to the Garage using a Garage method } public double showValue() { double value = garage.getValue(); return value; } } /** * Write a description of class Person here. * * @author Tara * @version 12/8/2012 */ public class Person { private String name; public Person() { name = "No name yet."; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public String toString() { return name; } public boolean sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.getName())); } } /** * Abstract class Vehicle - write a description of the class here * * @author Tara * @version 12/8/2012 */ public abstract class Vehicle { // instance variables - replace the example below with your own protected double value; protected String maker; protected String owner; protected String color; public Vehicle () { value = 0; maker = "Unknown maker"; owner = "Unknown owner"; color = "Unknown color"; } public Vehicle(String m, double v, String c, String o) { // sets values for: maker, value, color, owner setMaker(m); setValue(v); setColor(c); setOwner(o); } public String getMaker() { return maker; } // returns value of vehicle public double getValue() { return value; } // returns color of vehicle public String getColor() { return color; } // returns owner of vehicle public String getOwner() { return owner; } // sets instance maker to param maker public void setMaker(String m) { maker = m; } // sets instance value to param value public void setValue(double v) { value = v; } // sets instance color to param color public void setColor(String c) { color = c; } // sets instance owner to param owner public void setOwner(String o) { owner = o; } // vehicle tostring to be overwritten public abstract String toString(); } /** * Write a description of class Bike here. * * @author Tara * @version 12/8/2012 */ public class Bike extends Vehicle { // instance variables - replace the example below with your own protected int wheels; // default constructor public Bike() { super(); wheels = 2; color = "Red"; } //constructor public Bike(String m, double v, String c, String o) { super(m, v, c, o); wheels = 2; } public int getWheels() { return wheels; } public String toString() { String bike = "A " + wheels + " Wheeled, " + super.getColor() + " " + super.getMaker() + " bicycle.\n"; return bike; } } /** * Write a description of class Motorcycle here. * * @author Tara * @version 12/8/2012 */ public class Motorcycle extends Bike { // instance variables - replace the example below with your own private int horsepower; public Motorcycle() { super(); horsepower = 50; setColor("Black"); } public Motorcycle(String m, double v, String c, int hP, String o) { super(m, v, c, o); horsepower = hP; } public String toString() { String motorcycle = "A " + super.getWheels() + " wheeled, " + super.getColor() + " " + super.getMaker() + " Motor Cycle with " + horsepower + " horsepower.\n"; return motorcycle; } } /** * Write a description of class Car here. * * @author Tara * @version 12/8/2012 */ public class Car extends Vehicle //subclass of the vehicle class { protected int doors; protected int wheels; protected int horsepower; //default constructor public Car() { super(); doors = 2; wheels = 4; setColor("Silver"); } //constructor with params, sends to vehicle public Car(String m, double v, String c, int hP, int d, String o) { super(m, v, c, o); wheels = 4; horsepower = hP; doors = d; } // method where a string is formated and returned public String toString() { String car = "A " + super.getColor() + " " + super.getMaker() + " with " + doors + " doors and " + horsepower + "horsepower.\n"; return car; } }