Question

In: Computer Science

Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours...

Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type double. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type double. For each class, provide its getter and setter functions, a default constructor, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class is computed as numberOfHours times ratePerHour, and for the Supplies class the cost will be numberOfItems times pricePerItem. Each class should have a function toString() that will return all the required information. Next, create a class called Account with two protected attributes fullName of type String, and accountNum of type integer. Provide appropriate constructors, getter, setters, and toString methods for the class. Make classes Services and Supplies inherit class Account. Write a main() program that uses Java ArrayList to create an array of Account that will store objects of Services and objects of Supplies. Your program should use the loop structure that will ask the user which object to input into the array. The program will then ask the user to enter all the required values. Make up any values when creating each object. The user should have the options to enter one or more objects of Services or Supplies.

Solutions

Expert Solution

Account.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Account {
   protected String fullName;
   protected int accountNum;
   public Account(String fullName, int accountNum) {
       super();
       this.fullName = fullName;
       this.accountNum = accountNum;
   }
   public String getFullName() {
       return fullName;
   }
   public void setFullName(String fullName) {
       this.fullName = fullName;
   }
   public int getAccountNum() {
       return accountNum;
   }
   public void setAccountNum(int accountNum) {
       this.accountNum = accountNum;
   }
   @Override
   public String toString() {
       return "Account [fullName=" + fullName + ", accountNum=" + accountNum + "]";
   }
  
  
   //Main method for testing of the three classes
   public static void main(String[] args) {
       ArrayList<Account> accounts = new ArrayList<Account>();
       String choice="";
       do {
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           try {
               System.out.println("What do you want to add?");
               System.out.println("1. Services.");
               System.out.println("2. Supplies.");
               int option = Integer.parseInt(br.readLine());
               String fullname;
               int ac_no;
               System.out.println("Enter full name: ");
               fullname = br.readLine();
               System.out.println("Enter account number: ");
               ac_no = Integer.parseInt(br.readLine());
               switch(option) {
               case 1:
                   double numHours, rate;
                   System.out.println("Enter number of hours: ");
                   numHours = Double.parseDouble(br.readLine());
                   System.out.println("Enter rate: ");
                   rate = Double.parseDouble(br.readLine());
                   Services s = new Services(fullname, ac_no, numHours, rate);
                   accounts.add(s);
                   break;
               case 2:
                   double numItems, price;
                   System.out.println("Enter number of items: ");
                   numItems = Double.parseDouble(br.readLine());
                   System.out.println("Enter price per item : ");
                   price = Double.parseDouble(br.readLine());
                   Supplies sp = new Supplies(fullname, ac_no, numItems, price);
                   accounts.add(sp);
                   break;
               default:
                   System.exit(-1);
               }
              
               System.out.println("Do you want to add more? yes/no");
               choice = br.readLine();
           }catch(IOException ioe) {
               ioe.printStackTrace();
           }
       }while(choice.contentEquals("yes"));
      
   }
}

Supplies.java

public class Supplies extends Account{
   private double numberOfItems;
   private double pricePerItem;
   public Supplies(String fullName, int accountNum, double numberOfItems, double pricePerItem) {
       super(fullName, accountNum);
       this.numberOfItems = numberOfItems;
       this.pricePerItem = pricePerItem;
   }
   public Supplies(String fullName, int accountNum) {
       super(fullName, accountNum);
   }
   public double getNumberOfItems() {
       return numberOfItems;
   }
   public void setNumberOfItems(double numberOfItems) {
       this.numberOfItems = numberOfItems;
   }
   public double getPricePerItem() {
       return pricePerItem;
   }
   public void setPricePerItem(double pricePerItem) {
       this.pricePerItem = pricePerItem;
   }
   @Override
   public String toString() {
       return "Supplies [numberOfItems=" + numberOfItems + ", pricePerItem=" + pricePerItem + "]";
   }
  
   public double calculateSales() {
       return numberOfItems * pricePerItem;
   }
}

Services.java

public class Services extends Account{
   private double numOfHours;
   private double ratePerHour;
   public Services(String fullName, int accountNum, double numOfHours, double ratePerHour) {
       super(fullName, accountNum);
       this.numOfHours = numOfHours;
       this.ratePerHour = ratePerHour;
   }
   public Services(String fullName, int accountNum) {
       super(fullName, accountNum);
   }
   @Override
   public String toString() {
       return "Services [numOfHours=" + numOfHours + ", ratePerHour=" + ratePerHour + "]";
   }
  
   public double calculateSales() {
       return numOfHours*ratePerHour;
   }
}


Related Solutions

Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT