In: Computer Science
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());
}
}
}
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