Question

Implement the AutoShop class described by this API. You do not have to include javadoc comments....

Implement the AutoShop class described by this API. You do not have to include javadoc comments. The programming question asks you to implement a class that represents a auto shop that has an owner. The auto shop has a collection of vehicles, but the choice of which collection to use is left up to the student. In fact, students might choose to use more than one collection to implement the class. Students should analyze the API of the AutoShop class before choosing a collection. Also, inside this project you will have other classes that can help you to complete the implementation of the AutoShop class. ShopOwner class, the API of the ShopOwner class. Vehicle class, the API of the Vehicle class. SortVehiclebyYear class, the API of the SortVehiclebyYear class. SortVehiclebyMake class, the API of the SortVehiclebyMake class. Run the JUnit tester AutoShopTest after you complete each method to check your work.

https://www.eecs.yorku.ca/course_archive/2018-19/W/2030/labs/lab4/index.html

I need an answer ASAP

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package eecs2030.lab4;

import java.util.*;

/**
 * A class representing a <strong> auto shop </strong> that has an <strong>
 * owner</strong> . A auto shop <strong> <em> owns a collection (or possibly
 * collections) of vehicles, but does not own the vehicles themselves</em>
 * </strong>. In other words, <strong> <em>the auto shop and its collection of
 * vehicles form a composition</em> </strong>.
 * 
 * <p>
 * Only the owner of the auto shop is able to sell vehicles from the auto shop.
 * <strong> <em> The auto shop does NOT own its owner</em> </strong>. In other
 * words, <strong> <em>the auto shop and its owner form an aggregation</em>
 * </strong>.
 * </p>
 */

public class AutoShop {

    /*
     * YOU NEED A FIELD HERE TO HOLD THE Vehicle OF THIS AutoShop
     */
    private ShopOwner owner;
    private ArrayList<Vehicle> vehicles;

    /**
     * Initializes this auto shop so that it has the specified owner and no
     * vehicles.
     * 
     * @param owner the owner of this auto shop
     */
    public AutoShop(ShopOwner owner) {
        this.owner = owner;
        this.vehicles = new ArrayList<>();

    }

    /**
     * Initializes this auto shop by copying another auto shop. This auto shop will
     * have the same owner and the same number and type of vehicles as the other
     * auto shop.
     * 
     * @param other the auto shop to copy
     */
    public AutoShop(AutoShop other) {
        this.owner = other.owner;
        this.vehicles = new ArrayList<>();
        for (Vehicle v : other.vehicles) {
            this.vehicles.add(v);
        }

    }

    /**
     * Returns the owner of this auto shop.
     * 
     * <p>
     * This method is present only for testing purposes. Returning the owner of this
     * auto shop allows any user to sell vehicles from the auto shop (because any
     * user can get the owner of this auto shop)!
     * 
     * @return the owner of this auto shop
     */
    public ShopOwner getOwner() {
        // ALREADY IMPLEMENTED; DO NOT MODIFY
        return this.owner;
    }

    /**
     * Allows the current owner of this auto shop to give this auto shop to a new
     * owner.
     * 
     * @param currentOwner the current owner of this auto shop
     * @param newOwner     the new owner of this auto shop
     * @throws IllegalArgumentException if currentOwner is not the current owner of
     *                                  this auto shop
     */
    public void changeOwner(ShopOwner currentOwner, ShopOwner newOwner) {

        if (!currentOwner.equals(owner)) {
            throw new IllegalArgumentException("Owner name incorrect");
        }
        owner = newOwner;

    }

    /**
     * Adds the specified vehicles to this auto shop.
     * 
     * @param vehicles a list of vehicles to add to this auto shop
     */
    public void add(List<Vehicle> vehicles) {
        this.vehicles.addAll(vehicles);
    }

    /**
     * Returns true if this auto shop contains the specified vehicle, and false
     * otherwise.
     * 
     * @param vehicle a vehicle
     * @return true if this auto shop contains the specified vehicle, and false
     *         otherwise
     */
    public boolean contains(Vehicle vehicle) {
        return this.vehicles.contains(vehicle);
    }

    /**
     * Allows the owner of this auto shop to sell a single vehicle equal to the
     * specified vehicle from this auto shop.
     * 
     * <p>
     * If the specified user is not equal to the owner of this auto shop, then the
     * vehicle is not sold from this auto shop, and null is returned.
     * 
     * @param user    the person trying to sell the vehicle
     * @param vehicle a vehicle
     * @return a vehicle equal to the specified vehicle from this auto shop, or null
     *         if user is not the owner of this auto shop @pre. the auto shop
     *         contains a vehicle equal to the specified vehicle
     */
    public Vehicle sellingsingleVehicle(ShopOwner user, Vehicle vehicle) {
        if (!user.equals(owner) || !contains(vehicle)) {
            return null;
        }
        vehicles.remove(vehicle);
        return vehicle;
    }

    /**
     * Allows the owner of this auto shop to sell the smallest number of vehicles
     * whose total price value in dollars is equal or less than to the specified
     * price value in dollars.
     * 
     * <p>
     * Returns the empty list if the specified user is not equal to the owner of
     * this auto shop.
     * </p>
     * 
     * @param user       the person trying to sell vehicles from this auto shop
     * @param pricevalue a value in dollars
     * @return the smallest number of vehicles whose total price value in dollars is
     *         equal to the specified value in dollars from this auto shop 
     * @pre. the auto shop contains a group of vehicles whose total price value is
     *         equal to specified value
     */
    public List<Vehicle> sellingVehicles(ShopOwner user, int pricevalue) {

        if (!user.equals(owner)) {
            return new ArrayList<>();
        }

        List<Vehicle> removedVehicles = new ArrayList<>();

        while(true) {

            List<Vehicle> vehicles = new ArrayList<>(this.vehicles);

            vehicles.sort(new Comparator<Vehicle>() {

                @Override
                public int compare(Vehicle o1, Vehicle o2) {
                    if (o1.getPrice() > o2.getPrice()) {
                        return -1;
                    } else if (o1.getPrice() < o2.getPrice()) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
            });

            Vehicle closestVehicle = null;

            // Now pick vehicles one by one.
            for (Vehicle v : vehicles) {
                if(closestVehicle == null) {
                    closestVehicle = v;
                } else if(v.getPrice() >= pricevalue) {
                    closestVehicle = v;
                }
            }

            sellingsingleVehicle(user, closestVehicle);
            pricevalue -= closestVehicle.getPrice();
            removedVehicles.add(closestVehicle);

            if (pricevalue <= 0) {
                break;
            }
        }

        return removedVehicles;

    }

    /**
     * Returns a deep copy of the vehicles in this auto shop. The returned list has
     * its vehicles in sorted order (from smallest price value to largest price
     * value).
     * <p>
     * Remember that <strong> <em>the auto shop and its collection of vehicles form
     * a composition.</em> </strong>
     * </p>
     * 
     * @return a deep copy of the vehicles in this auto shop sorted by price value
     */
    public List<Vehicle> deepCopy() {

        List<Vehicle> vehicles = new ArrayList<>();
        for (Vehicle v : this.vehicles) {
            vehicles.add(new Vehicle(v));
        }
        Collections.sort(vehicles);
        return vehicles;

    }

    /**
     * Returns a Shallow copy of the vehicles in this auto shop. The returned list
     * has its vehicles in sorted order (from smallest year of make value to largest
     * year of make value).
     * <p>
     * Remember that <strong> <em>the auto shop and its collection of vehicles form
     * a composition.</em> </strong>
     * </p>
     * 
     * @return a show copy of the vehicles in this auto shop sorted by year of make
     */

    public List<Vehicle> shallowCopySortedbyYear() {
        List<Vehicle> vehicles = new ArrayList<>(this.vehicles);
        vehicles.sort(new Comparator<Vehicle>() {

            @Override
            public int compare(Vehicle o1, Vehicle o2) {
                return o1.getYearMake() - o2.getYearMake();
            }
        });
        return vehicles;

    }

    /**
     * Returns a deep copy of the vehicles in this auto shop. The returned list has
     * its vehicles in sorted based on make and then ordered based on price value.
     * 
     * (i.e., from smallest price value to largest price value).
     * <p>
     * Remember that <strong> <em>the auto shop and its collection of vehicles form
     * a composition.</em> </strong>
     * </p>
     * 
     * @return a deep copy of the vehicles in this auto shop sorted based on make
     *         and then ordered based on price value.
     */
    public List<Vehicle> deepCopySortedbyMakePrice() {
        List<Vehicle> vehicles = deepCopy();
        vehicles.sort(new Comparator<Vehicle>() {

            @Override
            public int compare(Vehicle o1, Vehicle o2) {
                if (o1.getMake().equals(o2.getMake())) {
                    if (o1.getPrice() > o2.getPrice()) {
                        return 1;
                    } else if (o1.getPrice() < o2.getPrice()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } else {
                    return o1.getMake().compareTo(o2.getMake());
                }
            }
        });
        return vehicles;
    }

}

I could not get one test passed, because i do not understand that case..

test08d_sellingVehicles expects
Vehicle vc5= new Vehicle("Audi",2017,4, 34000);
       expvehicles.add(vc5);
       expvehicles.add(vc5);
       Vehicle vc6= new Vehicle("Toyota",2018,4, 3000);
       expvehicles.add(vc6);
      
2 Audis and 1 toyota, which sums up to 71000 only, while we were passing min price as 72000, so i do not understand the expectation here.

Add a comment
Know the answer?
Add Answer to:
Implement the AutoShop class described by this API. You do not have to include javadoc comments....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the class described by the following API. You do not have to include javadoc comments....

    Implement the class described by the following API. You do not have to include javadoc comments. API - https://www.eecs.yorku.ca/course_archive/2018-19/W/2030/labs/lab6/doc/eecs2030/lab6/package-summary.html The programming question asks you to implement each method in the RecursiveMethods class recursively. Any use of loops is forbidden. You receive a zero if there is any occurrence of a loop (e.g., for, while).

  • Design and implement a class for a one person guessing game as described on page 30,...

    Design and implement a class for a one person guessing game as described on page 30, Chapter 1, Programming Problem 7 of the textbook. CSCI 2421 HW1.jpg Submit header file guess.h, implementation file guess.cpp, main function file main.cpp that asks initial seed number, and prints three sequential numbers generated by the program. You need to include a makefile, and Readme file pseudocode version UF comments in your code. 6. Exercises 6, 7, and 8 ask you to specify methods for...

  • C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement...

    C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement some code that could be part of a program to play a popular card game called “Crazy 8’s”, which is the basis for the game “Uno”. You will start to implement the rules, as described at: https://www.pagat.com/eights/crazy8s.html . Note that the inheritance relationship below is also based on the organization of card games described on that website. Requirements: your work will be split into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT