Answer to Question #238500 in Java | JSP | JSF for Mikz

Question #238500

1. Write a class called Item. It should have the following attributes (fields): a) a String name to store the name of the item. b) a String comment to store a comment about the item. c) a double value to store how much the item cost 

2.  Write a CD class that extends the Item class. CD’s have a String artist field and an int playingTime field.  Write a DVD class that extends the Item class. The DVD class has a String director and an int runningTime

3. Write a Database class that creates and maintain a list of multimedia items. Your class must use the subclasses defined above to provide the following minimum functionality: a) add an item to the list b) remove an item to the list c) print out a list of items (to the screen) d) depreciates each item 4. Write a test program that creates some items of different types, adds them to the database, removes one of them and then prints out a list.


1
Expert's answer
2021-09-17T15:01:10-0400
public class Item {
    private String name;
    private String comment;
    private double cost;

    public Item(String name, String comment, double cost) {
        this.name = name;
        this.comment = comment;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public double getCost() {
        return cost;
    }

    public String getComment() {
        return comment;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    @Override
    public String toString() {
        return "Name: " + name + " Comment: " + comment + " Cost: " + cost;
    }
}


public class CD extends Item {
    private String artist;
    private int playingTime;

    public CD(String name, String comment, double cost, String artist, int playingTime) {
        super(name, comment, cost);
        this.artist = artist;
        this.playingTime = playingTime;
    }

    @Override
    public String toString() {
        return super.toString() + " Artist: " + artist + " Playing time: " + playingTime;
    }
}


public class DVD extends Item{
    private String director;
    private int runningTime;

    public DVD(String name, String comment, double cost,String director, int runningTime) {
        super(name, comment, cost);
        this.director = director;
        this.runningTime = runningTime;
    }
    @Override
    public String toString() {
        return super.toString() + " Director: " + director + " Running time: " + runningTime;
    }
}


import java.util.ArrayList;

public class Database {
    private ArrayList<Item>items;

    public Database(){
        items = new ArrayList<>();
    }

    public void addItem(Item item){
        items.add(item);
    }
    
    public void removeItem(String name){
        for (int i = 0; i < items.size(); i++) {
            if(items.get(i).getName().equals(name)){
                items.remove(i);
                break;
            }
        }
    }
    
    public void printList(){
        for (Item item:items) {
            System.out.println(item);
        }
    }
    
    public void depreciateItems(){
        for (Item item:items) {
            item.setCost(0);
        }
    }
}


public class Main {
    public static void main(String[] args) {
        Database database = new Database();
        database.addItem(new CD("Night","Good",25.2,"Tarja",20));
        database.addItem(new CD("Twenty","Nice",20,"Tom",10));
        database.addItem(new DVD("Ram","Excellent",300.5,"Till",18));
        database.addItem(new DVD("Holly","Good",15.5,"Charlie",21));
        database.printList();
        database.removeItem("LOUNA");
        System.out.println();
        database.printList();
        database.depreciateItems();
        System.out.println();
        database.printList();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS