-3

So i have 4 classes of a car The Engine, The transmission, The Chassis and the Car itself each are coded as follows:

public class Chassis {
    public double m_Cprice;

    public Chassis(double m_Cprice) {
        this.m_Cprice = m_Cprice;
    }

    public double getChassisPrice() {
        return m_Cprice;
    }

    public void setChassisPrice(double m_Cprice){
        this.m_Cprice = m_Cprice;
    }
}

For the Chassis:

public class Transmission {
    public double m_Tprice;

    public Transmission(double m_Tprice){
        this.m_Tprice = m_Tprice;
    }

    public double getTransmissionPrice() {
        return m_Tprice;
    }

    public void setTransmissionPrice(double m_Tprice){
        this.m_Tprice = m_Tprice;
    }
}

For the Engine:

public class Engine {
    public double m_Eprice;

    public Engine(double m_Eprice){
        this.m_Eprice = m_Eprice;
    }

    public double getEnginePrice() {
        return m_Eprice;
    }

    public void setEnginePrice(double m_Eprice){
        this.m_Eprice = m_Eprice;
    }
}

And finally for the Car:

public final  class Car {
    public double m_baseprice;
    public Chassis m_Cprice;
    public Engine m_Eprice;   
    public Transmission m_Tprice;

    public double getBaseprice() {
        return m_baseprice;
    }

    public void setBasePrice(double m_baseprice){
        this.m_baseprice = m_baseprice;
    }

    public Car(double Baseprice,double Chassis, double Engine, double Transmission) {
        setBasePrice(Baseprice);
        m_Cprice = new Chassis(Chassis);
        m_Eprice = new Engine(Engine);
        m_Tprice = new Transmission(Transmission); 
    }

    public Chassis getM_Cprice() {
        return m_Cprice;
    }


    public void setM_Cprice(Chassis m_Cprice) {
        this.m_Cprice = m_Cprice;
    }

    public Engine getM_Eprice() {
        return m_Eprice;
    }

    public void setM_Eprice(Engine m_Eprice) {
        this.m_Eprice = m_Eprice;
    }


    public Transmission getM_Tprice() {
        return m_Tprice;
    }


    public void setM_Tprice(Transmission m_Tprice) {
        this.m_Tprice = m_Tprice;
    }

When i try to call the constructor for this app, everything works fine with no errors in my code, however when i ask to output anything nothing is output at all meaning i cannot properly construct my car using the boundaries within the constructor which are new Car(3000, 500, 1000, 2000) making the app not work correctly although no errors are there to point me in the correct direction.

Smajl
  • 7,555
  • 29
  • 108
  • 179
  • 2
    You'll need to show us the code where you construct the car and perform your output. A cursory glance indicates everything is ok with the code above – tddmonkey Feb 04 '16 at 10:26
  • ...other than naming, which is wildly at odds with Java conventions. – T.J. Crowder Feb 04 '16 at 10:27
  • What you tried? Where it is not working? – Samurai Feb 04 '16 at 10:28
  • I've actually got it working a little now public class MyApp { public static void main(String args[]){ Car C1 = new Car(3000, 500, 1000, 2000); C1.DisplayTotalPrice(); } is my main app that now displays values however it displays the wrong ones. Base Price 3000.0 Chassis CarPackage.Chassis@15db9742 Engine CarPackage.Engine@6d06d69cTransmissionCarPackage.Transmission@7852e922 Total where as it should display the values i input in the constructor – KAI ATTEWELL Feb 04 '16 at 10:29
  • Don't comment your code. Update it in question. – Samurai Feb 04 '16 at 11:18
  • Also you are printing the objects not values. Try obj.getChassisPrice() – Samurai Feb 04 '16 at 11:24

2 Answers2

0

Please leave off that awful "m_" prefix for member variables. It is horrible to read.

You need to learn more Java. First step: override toString() in all your classes to output the price. What you're seeing is the default output: the reference value for each class.

Double is not a good idea for money. Since you're dealing with big ticket items here, I'd recommend leaving off the cents and sticking to integers and whole dollars.

public class Engine {

    private int price;

    public Engine(int price) {
        this.setPrice(price);
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price){
        if (price <= 0) throw new IllegalArgumentException("price must be positive");
        this.price = price;
    }

    public String toString() {
        return String.format("price: %d", this.price);
    }
}

I know you're a beginner, but this design is crying out for an interface and a Composite pattern:

public interface Sellable {
    int getPrice();
    void setPrice(int price);
}

Now your Car can be considered a collection of Sellable instances. Its price will be a sum of all the Sellables.

duffymo
  • 305,152
  • 44
  • 369
  • 561
-1

The overall structure of your code is not very good. I suggest the following:

public class CarPart {

    private double price;

    public Part(final double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(final double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Price = " + price;
    }
}

public class Engine extends CarPart {

    public Engine(final double price) {
        super(price);
    }
}

public class Chassis extends CarPart {

    public Chassis(final double price) {
        super(price);
    }
}

public class Transmission extends CarPart {

    public Transmission(final double price) {
        super(price);
    }
}

public class Car {

    private double totalPrice;
    private Chassis chassis;
    private Engine engine;
    private PTransmission transmission;

    public Car(final double totalPrice, final Chassis chassis, final Engine engine, final Transmission transmission) {
        this.totalPrice = totalPrice;
        this.chassis = chassis;
        this.engine = engine;
        this.transmission = transmission;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(final double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Chassis getChassis() {
        return chassis;
    }

    public void setChassis(final Chassis chassis) {
        this.chassis = chassis;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(final Engine engine) {
        this.engine = engine;
    }

    public Transmission getTransmission() {
        return transmission;
    }

    public void setTransmission(final Transmission transmission) {
        this.transmission = transmission;
    }
}

Now you'll be able to use the application:

public class Main {

    public static void main(String[] args) {
        final Chassis chassis = new Chassis(2000);
        final Engine engine = new Engine(3000);
        final Transmission transmission = new Transmission(4000);

        final Car car = new Car(1000, chassis, engine, transmission);

        System.out.println(chassis.toString());
    }
}
Christiaan Janssen
  • 1,003
  • 1
  • 9
  • 14