Question

In: Computer Science

Using Netbeans update the Sales project so the input and the output are done through a...

Using Netbeans update the Sales project so the input and the output are done through a GUI of your choice.

The classes design should not be changed, only the code in the test class.

public abstract class Account {

private int accountId;

public Account(int id){
this.accountId=id;
}
//getters
public int getAccountId() {
return accountId;
}

//setters
public void setAccountId(int accountId) {
}

//abstract method to calculate sales
public abstract double calculateSales();
  
//to string method
@Override
public String toString() {
return("Account ID:"+getAccountId());
}
  
}

public class Services extends Account {

private int numberOfHours;
private double ratePerHour;

public Services(int id, int numberOfHours, double ratePerHours){
super(id);
this.numberOfHours=numberOfHours;
this.ratePerHour=ratePerHours;
}
  
//getters
public int getNumberOfHours() {
return numberOfHours;
}

public double getRatePerHour() {
return ratePerHour;
}

//setters
public void setNumberOfHours() {
this.numberOfHours=numberOfHours;
}

public void setRatePerHour() {
this.ratePerHour=ratePerHour;
}

//using the abstract method from Account
public double calculateSales() {
return getNumberOfHours() * getRatePerHour();
}

//toString method to put info in a readable format
@Override
public String toString() {
return super.toString()+", Rate per Hour: $"+getRatePerHour()+", Hours worked: "+ getNumberOfHours()+", Total Sales: $"+calculateSales();
}
}

public class Supplies extends Account {

private int numberOfItems;
private double pricePerItem;

public Supplies(int id, int numberOfItems, double pricePerItem){
super(id);
this.numberOfItems=numberOfItems;
this.pricePerItem=pricePerItem;
}
  
  
//getters
public int getNumberOfItems() {
return numberOfItems;
}

public double getPricePerItem() {
return pricePerItem;
}

//setters
public void setNumberOfItems() {
this.numberOfItems=numberOfItems;
}

public void setPricePerItem() {
this.pricePerItem=pricePerItem;
}

//using the abstract method from Account
public double calculateSales() {
return getNumberOfItems() * getPricePerItem();
}

//toString method to put info in a readable format
@Override
public String toString() {
return super.toString()+", Item Price: $"+getPricePerItem()+", Number of Items: "+ getNumberOfItems()+", Total Sales: $"+calculateSales();
}
}

TEST CLASS

public class companySales {
  
public static void main(String[] args) {
  
Account[] accounts;
accounts = new Account[100];
  
accounts[0] = new Supplies(1, 50, 4.50);
accounts[1] = new Services(2, 5, 25.50);

for(int x=0;x<2;++x){
System.out.println(accounts[x].toString());
}
}
}

Solutions

Expert Solution

Account.java

public abstract class Account {
   private int accountId;
   public Account(int id){
       this.accountId=id;
   }
   //getters
   public int getAccountId() {
       return accountId;
   }
   //setters
   public void setAccountId(int accountId) {
   }
   //abstract method to calculate sales
   public abstract double calculateSales();
   //to string method
   @Override
   public String toString() {
       return("Account ID:"+getAccountId());
   }

}

Supplies.java

public class Supplies extends Account {
   // instance variables
   private int numberOfItems;
   private double pricePerItem;
   // parameterized constructor
   public Supplies(int id, int numberOfItems, double pricePerItem){
       super(id);
       this.numberOfItems=numberOfItems;
       this.pricePerItem=pricePerItem;
   }
   //getters
   public int getNumberOfItems() {
       return numberOfItems;
   }
   public double getPricePerItem() {
       return pricePerItem;
   }
   //setters
   public void setNumberOfItems(int numberOfItems) {
       this.numberOfItems=numberOfItems;
   }
   public void setPricePerItem(double pricePerItem) {
       this.pricePerItem=pricePerItem;
   }
   //using the abstract method from Account
   public double calculateSales() {
       return getNumberOfItems() * getPricePerItem();
   }
   //toString method to put info in a readable format
   @Override
   public String toString() {
       return super.toString()+", Item Price: $"+getPricePerItem()+", Number of Items: "+ getNumberOfItems()+", Total Sales: $"+calculateSales();
   }
}

Services.java

public class Services extends Account {
   // instance variables
   private int numberOfHours;
   private double ratePerHour;
   // parameterized constructor
   public Services(int id, int numberOfHours, double ratePerHours){
       super(id);
       this.numberOfHours=numberOfHours;
       this.ratePerHour=ratePerHours;
   }
   //getters
   public int getNumberOfHours() {
       return numberOfHours;
   }
   public double getRatePerHour() {
       return ratePerHour;
   }
   //setters
   public void setNumberOfHours(int numberOfHours) {
       this.numberOfHours=numberOfHours;
   }
   public void setRatePerHour(double ratePerHour) {
       this.ratePerHour=ratePerHour;
   }
   //using the abstract method from Account
   public double calculateSales() {
       return getNumberOfHours() * getRatePerHour();
   }
   //toString method to put info in a readable format
   @Override
   public String toString() {
       return super.toString()+", Rate per Hour: $"+getRatePerHour()+", Hours worked: "+ getNumberOfHours()+", Total Sales: $"+calculateSales();
   }
}

CompanySales.java

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
// create class with JFrame
public class CompanySales extends JFrame implements ActionListener {

   private static final long serialVersionUID = 1L;
   // declare all required controls
   private JRadioButton supplies;
   private JRadioButton services;
   private ButtonGroup group;
   private JLabel label1;
   private JLabel label2;
   private JLabel label3;
   private JTextField accountId;
   private JTextField number;
   private JTextField rate;
   private JButton calculate;
   // Account class reference
   private Account account;
   // default constructor
   public CompanySales() throws HeadlessException {
       super();
       setSize(300,400); // set size of frame
       setTitle("Sales Project"); // set title to frame
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setLayout(null); // set layout as no layout
       // instantiate all declared controls
       supplies=new JRadioButton("Supplies",true);
       services=new JRadioButton("Services");
       group=new ButtonGroup();
       group.add(supplies);
       group.add(services);
       label1=new JLabel("Account ID: ");
       label2=new JLabel("Number Of Items: ");
       label3=new JLabel("Price per Item: ");
       accountId=new JTextField();
       number=new JTextField();
       rate=new JTextField();
       calculate=new JButton("Calculate Sales");
       // set bounds to all controls
       supplies.setBounds(50,30,100,40);
       services.setBounds(150,30,100,40);
       label1.setBounds(40,100,120,40);
       label2.setBounds(40,140,120,40);
       label3.setBounds(40,180,120,40);
       accountId.setBounds(140,100,125,30);
       number.setBounds(140,140,125,30);
       rate.setBounds(140,180,125,30);
       calculate.setBounds(80,250,150,40);
       // add all controls to the frame
       add(supplies);
       add(services);
       add(label1);
       add(label2);
       add(label3);
       add(accountId);
       add(number);
       add(rate);
       add(calculate);
       // all action listener on radio buttons and bauuton
       supplies.addActionListener(this);
       services.addActionListener(this);
       calculate.addActionListener(this);
       setVisible(true); // make frame visible
   }
  
   @Override
   public void actionPerformed(ActionEvent e) {
       try {
           int id,num;
           double price;
           // if supplies radio button is selected
           if(e.getSource()==supplies) {
               label2.setText("Number Of Items: ");
               label3.setText("Price per Item: ");
           }else
               // if services radio button is selected
               if(e.getSource()==services) {
                   label2.setText("Number Of Hours: ");
                   label3.setText("Rate Per Hour");
               }
           // accept input from text fields
           id=Integer.parseInt(accountId.getText());
           num=Integer.parseInt(number.getText());
           price=Double.parseDouble(rate.getText());
           if(supplies.isSelected()) {
               // create Supplies class object
               account=new Supplies(id, num, price);
               // calculate and print total sales
               JOptionPane.showMessageDialog(null, account.toString());
               clear(); // clear text fields
           }else
               if(services.isSelected()) {
                   // create Services class object
                   account=new Services(id, num, price);
                   // calculate and print total sales
                   JOptionPane.showMessageDialog(null, account.toString());
                   clear();
               }
       }catch(Exception e1) {
           if(e.getSource()==calculate)
           JOptionPane.showMessageDialog(null, "Invalid input...!!!");
       }
   }
   // method to clear text fields
   private void clear() {
       accountId.setText("");
       number.setText("");
       rate.setText("");
   }
   public static void main(String[] args) {
       new CompanySales(); // anonymous object of CompanySales class
   }
}

Output


Related Solutions

Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are...
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are created. Complete the following:     Create a 2-item array of type Account.     Store each account object created into the array.     For each element in this array, call the calculateSales() method, and use the toString() method to display the results.     Code should be fully commented.     Program flow should be logical. Code before revision: /* * To change this license header, choose...
In this project students will write a very simply program using Java’s input and output methods....
In this project students will write a very simply program using Java’s input and output methods. The program will prompt the user to enter responses to several questions, displaying the answer each time. The project is designed to give students an opportunity to practice very basic input and output via a simple project. Specification When the Java program starts it should prompt the user for input and print a response (including that input) as follows: Hello. What is your name?...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Create a Maven project in NetBeans using the maven-archetype-quickstart template, with Group Id: com.lastname.firstname and Artifact...
Create a Maven project in NetBeans using the maven-archetype-quickstart template, with Group Id: com.lastname.firstname and Artifact Id: exam2p1. 2. Use search.maven.org and/or mvnrepository.com to find three different artifacts that you will demonstrate in your project. a. Each artifact you use should be in a different category (e.g. cloud, JSON, regex, etc.). Hint: You can search by category at mvnrepository.com. DO NOT USE any artifact we have already used in a project in-class assignments or demos. b. Make sure you can...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT