Beginner stuck on coursework with HASHMAP's!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bumrag
    New Member
    • Apr 2007
    • 2

    Beginner stuck on coursework with HASHMAP's!

    This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be greatly appreciated.

    /** A CarDealer object represents a business that buys and resells used cars */
    import java.util.Array List;
    import java.util.HashM ap;
    public class CarDealer
    {
    private int dayNumber; // Day number from start of operation of business

    // HashMap objects are used to provided easy lookup of cars using their registrations
    // Each HashMap entry comprises a car registration as key, and the corresponding Car object
    private HashMap<String, Car> prepStock; // To hold cars being prepared for sale
    private HashMap<String, Car> saleStock; // To hold cars on sale
    private ArrayList<Sale> Sold; // An ArrayList field is needed, to hold the Sale objects created as sales are made

    /** Constructor */
    public CarDealer()
    {
    dayNumber = 1;
    Sold = new ArrayList<Sale> ();
    prepStock = new HashMap<String, Car>();
    saleStock = new HashMap<String, Car>();
    }

    public void advanceDate()
    {
    dayNumber++;
    System.out.prin tln("Day is now: " + dayNumber);
    }

    /** Note: Methods addCar, putOnSale, reducePrice and sellCar should print a confirmation
    * message, or an error report if the operation cannot sensibly be performed.
    */

    /** Method to be called when the dealer has acquired a car.
    * A Car object representing the car is added to the preparation stock.
    */
    public void addCar(String reg, String model, int paid)
    { // Replace this comment with your code
    prepStock.put(r eg, model, paid);
    }

    CAR OBJECT

    /** An object of class Car represents a car that the dealer has bought
    * and is preparing for sale, or has on sale, or has sold.
    */
    public class Car
    {
    private String registration; // Unique UK vehicle identification
    private String model; // What kind of car
    private int paid; // How much the dealer paid
    private int askingPrice; // "Windscreen " price

    /** Constructor - assumes that when a Car object is created, the
    * asking price is not decided and will be set later
    */
    public Car(String reg, String model, int paid)
    {
    registration = reg;
    this.model = model;
    this.paid = paid;
    }

    public String getRegistration ()
    {
    return registration;
    }

    public String getModel()
    {
    return model;
    }

    public int getPaid()
    {
    return paid;
    }

    /** To set a car's asking price to a specified sum */
    public void setAskingPrice( int price)
    {
    askingPrice = price;
    }

    public int getAskingPrice( )
    {
    return askingPrice;
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bumrag
    This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be greatly appreciated.

    /** A CarDealer object represents a business that buys and resells used cars */
    import java.util.Array List;
    import java.util.HashM ap;
    public class CarDealer
    {
    private int dayNumber; // Day number from start of operation of business

    // HashMap objects are used to provided easy lookup of cars using their registrations
    // Each HashMap entry comprises a car registration as key, and the corresponding Car object
    private HashMap<String, Car> prepStock; // To hold cars being prepared for sale
    private HashMap<String, Car> saleStock; // To hold cars on sale
    private ArrayList<Sale> Sold; // An ArrayList field is needed, to hold the Sale objects created as sales are made

    /** Constructor */
    public CarDealer()
    {
    dayNumber = 1;
    Sold = new ArrayList<Sale> ();
    prepStock = new HashMap<String, Car>();
    saleStock = new HashMap<String, Car>();
    }

    public void advanceDate()
    {
    dayNumber++;
    System.out.prin tln("Day is now: " + dayNumber);
    }

    /** Note: Methods addCar, putOnSale, reducePrice and sellCar should print a confirmation
    * message, or an error report if the operation cannot sensibly be performed.
    */

    /** Method to be called when the dealer has acquired a car.
    * A Car object representing the car is added to the preparation stock.
    */
    public void addCar(String reg, String model, int paid)
    { // Replace this comment with your code
    prepStock.put(r eg, model, paid);
    }

    CAR OBJECT

    /** An object of class Car represents a car that the dealer has bought
    * and is preparing for sale, or has on sale, or has sold.
    */
    public class Car
    {
    private String registration; // Unique UK vehicle identification
    private String model; // What kind of car
    private int paid; // How much the dealer paid
    private int askingPrice; // "Windscreen " price

    /** Constructor - assumes that when a Car object is created, the
    * asking price is not decided and will be set later
    */
    public Car(String reg, String model, int paid)
    {
    registration = reg;
    this.model = model;
    this.paid = paid;
    }

    public String getRegistration ()
    {
    return registration;
    }

    public String getModel()
    {
    return model;
    }

    public int getPaid()
    {
    return paid;
    }

    /** To set a car's asking price to a specified sum */
    public void setAskingPrice( int price)
    {
    askingPrice = price;
    }

    public int getAskingPrice( )
    {
    return askingPrice;
    }
    }
    1.) Use code tags next time when posting code.
    2.)What problems are you having with the addCar method and what ideas do you have for it?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by bumrag
      Code:
      public void addCar(String reg, String model, int paid)
          { // Replace this comment with your code
             prepStock.put(reg, model, paid);
          }
      Your HashMaps take a String (the registration code for the car) and a Car.
      Your method supplies you with the registration code, but not with a Car object.
      You have to create a new Car first given the other parameter values.

      kind regards,

      Jos

      Comment

      • bumrag
        New Member
        • Apr 2007
        • 2

        #4
        Thankyou for your help guys, Ive tried to create a new Car object but at the moment all i seem to be doing is sitting here staring at a blank screen, Any help would be greatly APPRECIATED, i know i have to initialise a new Car object, i have tried using,

        Car C = new Car();

        However, it says it cannot find symbol constructor. Has anyone ideas of how to resolve this?

        martin

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by bumrag
          Thankyou for your help guys, Ive tried to create a new Car object but at the moment all i seem to be doing is sitting here staring at a blank screen, Any help would be greatly APPRECIATED, i know i have to initialise a new Car object, i have tried using,

          Car C = new Car();

          However, it says it cannot find symbol constructor. Has anyone ideas of how to resolve this?

          martin
          But you wrote this constructor yourself didn't you?
          Code:
          public Car(String reg, String model, int paid)
          As far as I can see it's the only constructor defined in your class. Hint: compare
          its parameters to the parameters of the addCar( ... ) method.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by bumrag
            Thankyou for your help guys, Ive tried to create a new Car object but at the moment all i seem to be doing is sitting here staring at a blank screen, Any help would be greatly APPRECIATED, i know i have to initialise a new Car object, i have tried using,

            Car C = new Car();

            However, it says it cannot find symbol constructor. Has anyone ideas of how to resolve this?

            martin
            You create the Car using a constructor in the Car class. You do not have one that takes no arguments (Car()), so use the one you have. You can read this for more details on constructors.

            Comment

            • sateesht
              New Member
              • Apr 2007
              • 41

              #7
              As your Car class don't have a no argument Constructor, so by using this statement Car c=new Car(), obviously you'll get the Compilation Error, so you better to Create the Car Object by using Constructor you have i.e. a three argument constructor.

              Comment

              Working...