help with hashmaps and arraylist


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: help with hashmaps and arraylist

  1. #1
    Join Date
    Feb 2007
    Posts
    3

    help with hashmaps and arraylist

    hey i need some help on some of the question, if your able to help please reply!

    thankYou.




    /** A CarDealer object represents a business that buys and resells used cars */
    import java.util.ArrayList;
    import java.util.HashMap;
    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 int[] sales;
    // An ArrayList field is needed, to hold the Sale objects created as sales are made

    /** Constructor */
    public CarDealer()
    {
    dayNumber = 1;

    prepStock = new HashMap<String, Car>();
    saleStock = new HashMap<String, Car>();
    sales = new int[100];// to hold sales


    }

    public void advanceDate()
    {
    dayNumber++;
    System.out.println("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)
    {
    System.out.println("addCar");
    System.out.println("putOnSale");
    System.out.println("reducePrice");
    System.out.println("sellCar");

    {
    }

    prepStock.put(reg, new Car(reg, model, paid));


    }

    /** Method to be called when a car is put on sale. The asking price must be specified.
    * The Car object with the specified registration is moved to the sale stock.
    */
    public void putOnSale(String reg, int ask)
    {

    Car c = prepStock.get(reg);
    c.setPrice();



    }

    /** Method to reduce the asking price of a car by a specified amount */
    public void reducePrice(String reg, int amount)
    {


    Car c = saleStock.get(reg);


    }

    /** Method to be called when a car has been sold. The price received must be specified.
    * The Car object with the specified registration is removed from the sale stock
    * and an appropriate Sale object created and added to the collection of sales.
    */
    public void sellCar(String reg, int received)
    {
    Car c = saleStock.get(reg);
    reg = reg + received;

    saleStock.remove(reg);



    }

    /** Method to print a report giving:
    * - The number of cars sold so far
    * - The total profit made on those sales
    * - The average discount per sale.
    *
    * Notes:
    * - The profit on a sale is the difference between the price obtained
    * and the price the dealer paid for the car.
    * - The discount on a sale is the difference between the asking price
    * and the price obtained.
    */
    public void printOverallSummary()
    {


    }

    /** Method to print a report giving:
    * - The number of cars currently in preparation
    * - The number of cars currently for sale
    * - The number of cars sold on the current day
    * - The total amount received for those sales
    */
    public void printDaySummary()
    {
    System.out.println(prepStock.get("numCars"));
    System.out.println(saleStock.get("numCars"));
    if (dayNumber==dayNumber)
    {

    }
    }

    /** Method to print a report giving details of the single most profitable sale
    * so far achieved.
    */
    public void showBestSale()
    {

    }

    /** Method to print a report giving details of every sale starting from a
    * specified day and continuing up to another specified day.
    */
    public void listDaySales(int dayFrom, int dayTo)
    {

    while(dayFrom < dayTo) {


    }
    }

    /** Method to print a report giving details of all the sales achieved for a
    * specified model.
    */
    public void listModelSales(String model)
    {
    if(reg.equals(model))
    System.out.prinltn("sales");
    index++;
    }

    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What is your question?

  3. #3
    Join Date
    Feb 2007
    Posts
    3
    /** Method to be called when a car is put on sale. The asking price must be specified.
    * The Car object with the specified registration is moved to the sale stock.
    */
    public void putOnSale(String reg, int ask)
    {

    }

    ** Method to reduce the asking price of a car by a specified amount */
    public void reducePrice(String reg, int amount)
    {

    }

    /** Method to print a report giving details of the single most profitable sale
    * so far achieved.
    */
    public void showBestSale()
    {

    }

    /** Method to print a report giving details of every sale starting from a
    * specified day and continuing up to another specified day.
    */
    public void listDaySales(int dayFrom, int dayTo)
    {

    while(dayFrom < dayTo) {


    }
    }


    if you can help me with any of theses questions, that would be great. ThankYou!

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Some hints

    It would have been nice if you supplied this question w. the code for the Car class....

    Your code below invokes a Car method called setPrice, without supplying any price at all.
    Code:
    public void putOnSale(String reg, int ask) {
    
      Car c = prepStock.get(reg);
      c.setPrice();
    }
    I recon the Car class will have at least three private price members: Price paid for the car, the car's estimated salesprice and perhaps the latest (highest ?) bid for the car.
    The code below assumes that the Car class has an int variable for the sales price: askPrice:
    Code:
    public void setPrice(int askPrice) {
      // perhaps some additional "sanity checking"  here..
      this.askPrice=askPrice;
    }
    Then your code would be like:
    Code:
    public void putOnSale(String reg, int ask) {
    
      Car c = prepStock.get(reg);
    
      if (c !=null) {
        c.setPrice(ask);
      } else {
        System.err.println("Car reg: "+reg+" not found");
      }
    
    }
    eschew obfuscation

Similar Threads

  1. Alternative data structures to ArrayList
    By jamiecottonuk in forum Java
    Replies: 1
    Last Post: 04-03-2005, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center