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.
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces of information: Enlistee name Enlistee number Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information: Platoon number (an integer, such as 1, 2, or 3) Years of service (also an integer) Write the appropriate accessor and mutator methods for each class. Once you have written the classes, write...
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...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
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 {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT