Question

In: Computer Science

Please implement Sample string toString()method for each class and return itself a string, not the output....

 Please implement Sample string toString()method for each class and return itself a string, not the output. 
import java.util.ArrayList;

public class Customer extends User{
    private ArrayList orders;

    public Customer(String display_name, String password, String email) {
        super(display_name, password, email);
    }

    @Override
    public String getPermissionLevel() {
        return "CUSTOMER";
    }

    public void addOrder(Order order){
        this.orders.add(order);
    }

    public ArrayList listOrders(){
        return this.orders;
    };
}

----------------

public class ElectronicProduct extends Product{
    private long SNo;
    private String warranty_period;

    public ElectronicProduct(long SNo, String warranty_period, String productId, String productName, String brandName, String Description) {
        super(productId, productName, brandName, Description);
        this.SNo = SNo;
        this.warranty_period = warranty_period;
    }

}

--------------

public class HomeProduct extends Product{

    String location;
    public HomeProduct(String location, String productId, String productName, String brandName, String Description) {
        super(productId, productName, brandName, Description);
        this.location = location;
    }
}

----------------------

import java.util.ArrayList;
import java.util.Date;
public class Order {
    private int order_no;
    private ArrayList products;
    private String order_Status;
    private Date final_date;

    public Order(int order_no, String order_Status, Date final_date) {
        this.order_no = order_no;
        this.order_Status = order_Status;
        this.final_date = final_date;
        this.products = new ArrayList<>();
    }

    public Date getFinal_date() {
        return final_date;
    }

    public String getOrder_Status() {
        return order_Status;
    }

    public int getOrder_no() {
        return order_no;
    }

    public void setFinal_date(Date final_date) {
        this.final_date = final_date;
    }

    public void setOrder_Status(String order_Status) {
        this.order_Status = order_Status;
    }

    public void setOrder_no(int order_no) {
        this.order_no = order_no;
    }

    public ArrayList getProducts(){
        return this.products;
    }

    public void addProduct(Product product){
        this.products.add(product);
    }

}

------------------------------------------------

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;

public class Product {

    private String productId;
    private String productName;
    private String brandName;
    private String description;
    private ArrayList categories;
    private LocalDate dateCatalog;

    public Product(String productId, String productName, String brandName, String description) {
        this.productId = productId;
        this.productName = productName;
        this.brandName = brandName;
        this.description = description;
        this.dateCatalog = LocalDate.now();
        this.categories = new ArrayList<>();
    }

    public String getProductId() {
        return productId;
    }

    public String getDescription() {
        return description;
    }

    public String getProductName() {
        return productName;
    }

    public void addCategory(Category category){
        this.categories.add(category);
    }

    public ArrayList listCategories(){
        return this.categories;
    }
}

--------

 

-----------------------------------------

 

Solutions

Expert Solution

I have added toString method for each class.

Full Java Code :

import java.util.ArrayList;

public class Customer extends User{

    private ArrayList orders;

    public Customer(String display_name, String password, String email) {

        super(display_name, password, email);

    }

//sample toString method for Customer class

public String toString()

    {

        return "this is toString method of Customer class & " +display_name + " " + password + " " + email;

    }  

    @Override

    public String getPermissionLevel() {

        return "CUSTOMER";

    }

    public void addOrder(Order order){

        this.orders.add(order);

    }

    public ArrayList listOrders(){

        return this.orders;

    };

}




public class ElectronicProduct extends Product{

    private long SNo;

    private String warranty_period;

    public ElectronicProduct(long SNo, String warranty_period, String productId, String productName, String brandName, String Description) {

        super(productId, productName, brandName, Description);

        this.SNo = SNo;

        this.warranty_period = warranty_period;

    }





//tostring method for ElectronicProduct Class

public String toString()

    {

        return productId + " " + productName + " "+ brandName + " " + description + " " +SNo + " " +warranty_period;

    }

}

--------------

public class HomeProduct extends Product{

    String location;

    public HomeProduct(String location, String productId, String productName, String brandName, String Description) {

        super(productId, productName, brandName, Description);

        this.location = location;

    }

//tostring method for HomeProduct Class

public String toString()

    {

        return productId + " " + productName + " "+ brandName + " " + description + " " +location;

    }


}

----------------------

import java.util.ArrayList;

import java.util.Date;

public class Order {

    private int order_no;

    private ArrayList products;

    private String order_Status;

    private Date final_date;

    public Order(int order_no, String order_Status, Date final_date) {

        this.order_no = order_no;

        this.order_Status = order_Status;

        this.final_date = final_date;

        this.products = new ArrayList<>();

    }

//sample toString method for Oder class

public String toString()

    {

        return order_no + " " + products + " " + order_Status + " " + final_date ;

    }

    public Date getFinal_date() {

        return final_date;

    }

    public String getOrder_Status() {

        return order_Status;

    }

    public int getOrder_no() {

        return order_no;

    }

    public void setFinal_date(Date final_date) {

        this.final_date = final_date;

    }

    public void setOrder_Status(String order_Status) {

        this.order_Status = order_Status;

    }

    public void setOrder_no(int order_no) {

        this.order_no = order_no;

    }

    public ArrayList getProducts(){

        return this.products;

    }

    public void addProduct(Product product){

        this.products.add(product);

    }

}

------------------------------------------------

import java.time.LocalDate;

import java.util.ArrayList;

import java.util.Date;

public class Product {

    private String productId;

    private String productName;

    private String brandName;

    private String description;

    private ArrayList categories;

    private LocalDate dateCatalog;

    public Product(String productId, String productName, String brandName, String description) {

        this.productId = productId;

        this.productName = productName;

        this.brandName = brandName;

        this.description = description;

        this.dateCatalog = LocalDate.now();

        this.categories = new ArrayList<>();

    }

//tostring method for Product Class

public String toString()

    {

        return productId + " " + productName + " "+ brandName + " " + description + " " + dateCatalog + " " +categories;

    }


    public String getProductId() {

        return productId;

    }

    public String getDescription() {

        return description;

    }

    public String getProductName() {

        return productName;

    }

    public void addCategory(Category category){

        this.categories.add(category);

    }

    public ArrayList listCategories(){

        return this.categories;

    }

}

--------


Related Solutions

Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method 2: badFriend - return string value, "I don't like you!" Main Method: - create new Friend object - call goodFriend and badFriend using Friend object /* class header */ { /* constructor method header */ { } public static void main (String [] args) { Friend f = /* new Friend object */ // call goodFriend using Friend object // call badFriend using Friend...
Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. IT IS C++
Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. c++
Please write a main method that creates 2 pizzas and uses the toString() method to test...
Please write a main method that creates 2 pizzas and uses the toString() method to test this program. public class PizzaOrder { private Pizza[] order; private int numPizzas; public PizzaOrder() { order = new Pizza[1]; order[0] = new Pizza("Cheese", 0); numPizzas = 1; } public PizzaOrder(int count) { order = new Pizza[count]; this.numPizzas = 0; } public void addPizza(Pizza pizza) { for (int i = 0; i < order.length; i++) { if (order[i] == null) { order[i] = pizza; System.out.println("Pizza...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing a letter // grade (e.g., "A", "A-", "B+", etc.). // The letter grade is determined by the given percentage, according // to the scale specified on page 2 of the class syllabus (available // here: https://kyledewey.github.io/comp110-spring18/syllabus.pdf). // You may assume that the given percentage is between 0.0 and 100.0 // TODO - write your code below this comment.    public static String letterGrade(double percentage){...
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
Which of this method of class String is used to obtain a length of String object?...
Which of this method of class String is used to obtain a length of String object? What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4)    break;    if(cnt==0) {     cnt++; continue; }   System.out.print(cnt + ",");   cnt++; } 1,2,3,4 Compiler error 0,1,2,3,4, 1,2,3,4,
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT