CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2016
    Posts
    1

    Unhappy Object oriented program in Java

    Please help me with the following task:
    "Imagine that you must create an information system of a store.You must create the Product class,which will represent the base for the inheritance(extend) that will follow and which will never instantiate.This class must be inherited by two classes which represent specific groups of products:Wine and Chocolate.This hierarchy,represented by a diagram that looks like this Name:  diagrama 1.jpg
Views: 2342
Size:  29.7 KB


    Each product must possess some features like:
    -the name of the product;
    -the barcode;
    -the price without VAT;
    -the tax;

    Also, each product possesses a method for calculating the price,the method according to which it calculates when the price without VAT increases with the tax amount.The tax(VAT) for each product is 20% and this constant data remains unchanged.
    The products from the beer group have an additional tax,because they belong to the category of alcoholic beverages,so that this additional tax represents 10% of the price with VAT.And this constant data remains unchanged.
    From the specified reasons,is necessary to redefine the method for calculating the prices within the Wine Class.
    Apart from that,the Wine Class must possess an attribute which will define the volume of the bottle, and the Chocolate Class must have the attribute which will define the weight.
    In the classes Chocolate and Wine, must be created the parameterized constructors for creating the objects.
    Also,in the classes must be redefined the method "to String" for displaying the information about the object.
    Finally,an object must be created for each of these classes, and at output must be displayed the information about the products,as well as the final price,defined by the price calculation method.As a price without VAT we can sider any arbitrary amount.
    Hint:The Chocolate Class must contain a private member/component for weight, and Class Wine a private member/component for volume.
    It is recommended that the class members(barcode, name, price) should be private members,and for their use the methods getter/setter should be used accessed(in order to respect the principle of encapsulation).The primary and additional taxes should be defined for instance as "private final static float".
    It is recommended that the parameterized constructors of the derived class to call via super parameterized constructor of the primary class.
    At the same time it is recommended that overriding the method toString() from the classes Wine and Chocolate yo use overriding method from the class Product via super.toString().
    It would be recommended to achieve validation of the values to be alloted to the members price,weight and volume,verifying if these values are strictly positive(the value zero and the negative values having no sense)."

    Please anyone help me with this assignment,I've posted below but it's incomplete.
    Thanks in advance!

    public abstract class Product {


    private String barcode;
    private String name;
    private float Price;
    private static float DEFAULT_VALUE_PRICE;

    public abstract double countPrice();

    public Product(String barcode, String name, float Price) {
    this.barcode = barcode;
    this.name = name;
    this.Price = Price;
    Product.DEFAULT_VALUE_PRICE = DEFAULT_VALUE_PRICE;
    }

    public void show() {
    System.out.println("Product: " + barcode + " " + name + Price + DEFAULT_VALUE_PRICE);
    }

    public String getBarcode() {
    return barcode;
    }

    public String getName() {
    return name;
    }

    public float getPrice() {
    return Price;
    }

    public float getDEFAULT_VALUE_PRICE() {
    return DEFAULT_VALUE_PRICE;
    }

    public void setBarcode(String barcode) {
    this.barcode = barcode;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void setPrice(float price) {

    if (!Validate.verify(Price)) {

    this.Price = DEFAULT_VALUE_PRICE;

    }

    this.Price = price;

    }


    @Override
    public String toString() {
    StringBuilder output = new StringBuilder();

    output.append("Produs: ");
    output.append(this.barcode).append(", ");
    output.append(this.name).append(", ");
    output.append("price: ").append(countPrice()).append(", ");
    output.append(Product.DEFAULT_VALUE_PRICE);

    return output.toString();
    }

    }



    public class Wine extends Product {

    private float volumeWine;
    private final static float DEFAULT_VALUE_VOLUME = 5.5f;
    private final static float TAXES_VAT = 0.2f;
    private final static float TAXES_ADD = 0.1f;

    public Wine(String barcode, String name, float Price, float volumeWine) {

    super(barcode, name, Price);
    this.volumeWine = volumeWine;
    }

    @Override
    public final double countPrice() {
    //Se calculează preţul produsului cu taxa TVA de 20% plus taxa suplimentara de 10% din TVA
    return super.getPrice() + super.getPrice() * 0.2 + 0.1 * (super.getPrice() * 0.2 + super.getPrice());
    }

    @Override
    public String toString() {

    return super.toString() + ", volumeWine: " + this.volumeWine;
    }

    public float getVolumeWine() {
    return volumeWine;
    }

    public static float getDEFAULT_VALUE_VOLUME() {

    return DEFAULT_VALUE_VOLUME;
    }

    public static float getTAXES_VAT() {

    return TAXES_VAT;

    }

    public static float getTAXES_ADD() {

    return TAXES_ADD;

    }

    public void setVolumeWine(float volumeWine) {

    if (!Validate.verify(volumeWine)) {

    this.volumeWine = DEFAULT_VALUE_VOLUME;

    }

    this.volumeWine = volumeWine;

    }
    }



    public class Chocolate extends Product {

    private float weightChocolate;
    private final static float DEFAULT_VALUE_WEIGHT = 3.1f;
    private final static float TAXES_VAT = 0.2f;

    public Chocolate(String barcode, String name, float Price, float weightChocolate) {

    super(barcode, name, Price);
    this.weightChocolate = weightChocolate;
    }

    @Override
    public final double countPrice() {

    return 0.2 * super.getPrice() + super.getPrice();
    }

    @Override
    public String toString() {
    return super.toString() + ", weightChocolate: " + this.weightChocolate;
    }

    public float getWeightChocolate() {
    return weightChocolate;
    }

    public static float getDEFAULT_VALUE_WEIGHT() {

    return DEFAULT_VALUE_WEIGHT;
    }

    public static float getTAXES_VAT() {

    return TAXES_VAT;

    }

    public void setWeightChocolate(float weightChocolate) {

    if (!Validate.verify(weightChocolate)) {

    this.weightChocolate = DEFAULT_VALUE_WEIGHT;

    }

    this.weightChocolate = weightChocolate;

    }
    }

    package aldoileaassignment;

    public class Validate {

    static boolean verify(float attribute) {
    return attribute > 0;
    }

    }


    package aldoileaassignment;

    //Programul principal
    public class AlDoileaAssignment {

    public static void main(String[] args) {

    Wine w = new Wine("8410261271224", "Don Simon Red Dry Wine", 50, 5.5f);


    Chocolate c = new Chocolate("4025700001023", "Milka Nuts", 40, 3.1f);

    //Afişarea claselor Wine şi Chocolate
    System.out.println("Name of the product:" + w.getName());
    System.out.println("Barcode:" + w.getBarcode());
    System.out.println("Price with VAT and additional TAXES:" + w.countPrice());
    System.out.println("Volume:" + w.getDEFAULT_VALUE_VOLUME());
    System.out.println("Name of the product:" + c.getName());
    System.out.println("Barcode:" + c.getBarcode());
    System.out.println("Price with VAT:" + c.countPrice());
    System.out.println("Weight:" + c.getDEFAULT_VALUE_WEIGHT());

    }

    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Object oriented program in Java

    Please use code tags. If you want help with homework, you need to be very specific about the problem (and where the code is failing).

    No one here is going to simply fix the code but we will help on specific areas you are having trouble with.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured