I need help with the beginning of my code. I need add a JFrame or a JPanel in th
ID: 3697537 • Letter: I
Question
I need help with the beginning of my code. I need add a JFrame or a JPanel in the main method of TestOrder that does the same thing as my JOptionpane. In other words, I want the program to open up open up a window where i can input values such as Client name, ID, Order value, and interest all at once instead of seperately.
TestOrder.java (the part of the code i need help with)
//TestOrder can only contain Main method
import javax.swing.JOptionPane;
class TestOrders {
public static void main(String[] args) throws Exception {
//initiate order input values for first two objects
String name1 = JOptionPane.showInputDialog("Input Client Name:");
String sID1 = JOptionPane.showInputDialog("Input Order ID:");
String sValue1 = JOptionPane.showInputDialog("Input Order Value:");
int id1 = Integer.parseInt(sID1);
int value1 = Integer.parseInt(sValue1);
//convert input strings to int and double variables
//instantiate order 1 and credit order 1
Order ord1 = new Order(name1, id1, value1);
//output toString values of newly instantiated objects
System.out.println(ord1.toString());
//create order value from ord1 and cord1
int orderAmt1 = ord1.getOrderValue(value1);
//initiate order input values for new object
String name2 = JOptionPane.showInputDialog("Input Client Name:");
String sID2 = JOptionPane.showInputDialog("Input Order ID:");
String sValue2 = JOptionPane.showInputDialog("Input Order Value:");
String sInterest2 = JOptionPane.showInputDialog("Input Credit Order Interest Value:");
//convert input strings to int and double variables
int id2 = Integer.parseInt(sID2);
int value2 = Integer.parseInt(sValue2);
double interest = Double.parseDouble(sInterest2);
if(interest < 8 || interest > 25) {
String intresterror = JOptionPane.showInputDialog("Input Credit Order Interest Value must be between 8 and 25 percent:");
double intreste1 = Double.parseDouble(intresterror);
interest = intreste1;
}
//instantiate new credit order object from new values
CreditOrder creditOrder1 = new CreditOrder(name2, id2, value2, interest);
//output toString values of new credit order object
System.out.println(creditOrder1.toString());
//get order value of creditOrder1
double corderAmt2 = creditOrder1.getCreditOrderTotalValue(value2, interest);
//print order value of creditOrder1
int Totalorder;
Totalorder = (int) (corderAmt2 + orderAmt1);
System.out.println("Amount earned from Order both orders: " + Totalorder);
//logic check of whether the clients are the same or it creditOrder1 is a new client
if (ord1.getClientName().equals(creditOrder1.getClientName())) {
System.out.println("Existing Credit Customer: " + creditOrder1.getClientName());
} else {
System.out.println("New Credit Customer " + creditOrder1.getClientName());
}
//determine the greatest profit to be earned between the two credit orders
if (ord1.getOrderValue(value1) > creditOrder1.getCreditOrderTotalValue(value2, interest)) {
System.out.println("Greatest income from client " + name1 + " Order ID: " + ord1.getOrderID(id1) + " with a value of $" + orderAmt1);
} else {
System.out.println("Greatest income is from client " + name2 + " Order ID: " + creditOrder1.getOrderID(id2) + " with a value of $" + corderAmt2);
}
}
}
Order.java
//orderId, clientName and orderValue
public class Order {
private int orderID = 0;
private String clientName = " ";
private int orderValue = 0;
public Order(String clientName, int orderID, int orderValue) {
this.orderID = orderID;
this.clientName = clientName;
this.orderValue = orderValue;
}
// get and set methods for Order ID.
public int getOrderID(int Id) throws Exception{
orderID = Id;
return orderID;
}
public void setOrderID(int orderID) throws Exception{
if(orderID < 0) {
throw new Exception("Exception: Order ID cannot be less than 0!");
}
this.orderID = orderID;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
// get and set methods for Order Value
public int getOrderValue(int Value){
orderValue = Value;
return orderValue;
}
public void setOrderValue(int orderValue) throws Exception{
if(orderValue < 0) {
throw new Exception("Exception: Order Value cannot be less than 0!");
}
this.orderValue = orderValue;
}
public String toString() {
return "Client: " + clientName + " Order ID#: " + orderID + " Order Value: " + orderValue;
}
}
CreditOrder.java
public class CreditOrder extends Order {
private double interest = 0;
private double creditOrderTotal;
/**
* @param args
* @throws Exception
*/
public CreditOrder(String clientName, int orderID, int orderValue, double interest) {
super(clientName,orderID,orderValue);
this.interest = interest;
}
public double getInterest(double interestRate) {
interest = interestRate;
return interest;
}
public void setInterest(double interest) throws Exception{
this.interest = interest;
}
public String toString() {
return super.toString() + " Interest Percentage: " + interest;
}
/**
* @param int order value, double interest value
*
* @return credit order plus 1 year worth of interest
*
*/
public double getCreditOrderTotalValue(int value, double interest){
double newValue = value * (interest/100) + value;
creditOrderTotal = newValue;
return creditOrderTotal;
}
}
Explanation / Answer
some changes made in program are in bold :
import javax.swing.JOptionPane;
class TestOrders {
public static void main(String[] args) throws Exception {
JFrame f=new JFrame("this is JFrame");
JPanel p=new JPanel();
p.setSize(100,100);
p.add(new JButton("Button"));
p.add(new JLabel("this is JLabel"));
p.setBackground(Color.RED);
f.add(p);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//initiate order input values for first two objects
String name1 = JOptionPane.showInputDialog("Input Client Name:");
String sID1 = JOptionPane.showInputDialog("Input Order ID:");
String sValue1 = JOptionPane.showInputDialog("Input Order Value:");
String sInterest1 = JOptionPane.showInputDialog("Input Credit Order Interest Value:");
int id1 = Integer.parseInt(sID1);
int value1 = Integer.parseInt(sValue1);
//convert input strings to int and double variables
//instantiate order 1 and credit order 1
Order ord1 = new Order(name1, id1, value1);
CreditOrder cord1 = new CreditOrder(name1, id1, value1, interest1);
//output toString values of newly instantiated objects
System.out.println(ord1.toString());
System.out.println(cord1.toString());
//create order value from ord1 and cord1
int orderAmt1 = ord1.getOrderValue(value1);
double corderAmt1 = cord1.getCreditOrderTotalValue(value1, interest1);
//print amounts gathered above
System.out.println("Amount earned from Order: " + orderAmt1);
System.out.println("Amount earned from Order: " + corderAmt1);
//initiate order input values for new object
String name2 = JOptionPane.showInputDialog("Input Client Name:");
String sID2 = JOptionPane.showInputDialog("Input Order ID:");
String sValue2 = JOptionPane.showInputDialog("Input Order Value:");
String sInterest2 = JOptionPane.showInputDialog("Input Credit Order Interest Value:");
//convert input strings to int and double variables
int id2 = Integer.parseInt(sID2);
int value2 = Integer.parseInt(sValue2);
double interest2 = Double.parseDouble(sInterest2);
if(interest < 8 || interest > 25) {
String interesterror = JOptionPane.showInputDialog("Input Credit Order Interest Value must be between 8 and 25 percent:");
double interest1 = Double.parseDouble(interestterror);
interest = interest1;
}
//instantiate new credit order object from new values
CreditOrder creditOrder1 = new CreditOrder(name2, id2, value2, interest);
//output toString values of new credit order object
System.out.println(creditOrder1.toString());
//get order value of creditOrder1
double corderAmt2 = creditOrder1.getCreditOrderTotalValue(value2, interest);
//print order value of creditOrder1
int Totalorder;
Totalorder = (int) (corderAmt2 + orderAmt1);
System.out.println("Amount earned from Order both orders: " + Totalorder);
//logic check of whether the clients are the same or it creditOrder1 is a new client
if (ord1.getClientName().equals(creditOrder1.getClientName())) {
System.out.println("Existing Credit Customer: " + creditOrder1.getClientName());
} else {
System.out.println("New Credit Customer " + creditOrder1.getClientName());
}
//determine the greatest profit to be earned between the two credit orders
if (ord1.getOrderValue(value1) > creditOrder1.getCreditOrderTotalValue(value2, interest)) {
System.out.println("Greatest income from client " + name1 + " Order ID: " + ord1.getOrderID(id1) + " with a value of $" + orderAmt1);
} else {
System.out.println("Greatest income is from client " + name2 + " Order ID: " + creditOrder1.getOrderID(id2) + " with a value of $" + corderAmt2);
}
}
}
Order.java
//orderId, clientName and orderValue
public class Order {
private int orderID = 0;
private String clientName = " ";
private int orderValue = 0;
public Order(String clientName, int orderID, int orderValue) {
this.orderID = orderID;
this.clientName = clientName;
this.orderValue = orderValue;
}
// get and set methods for Order ID.
public int getOrderID(int Id) throws Exception{
orderID = Id;
return orderID;
}
public void setOrderID(int orderID) throws Exception{
if(orderID < 0) {
throw new Exception("Exception: Order ID cannot be less than 0!");
}
this.orderID = orderID;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
// get and set methods for Order Value
public int getOrderValue(int Value){
orderValue = Value;
return orderValue;
}
public void setOrderValue(int orderValue) throws Exception{
if(orderValue < 0) {
throw new Exception("Exception: Order Value cannot be less than 0!");
}
this.orderValue = orderValue;
}
public String toString() {
return "Client: " + clientName + " Order ID#: " + orderID + " Order Value: " + orderValue;
}
}
CreditOrder.java
public class CreditOrder extends Order {
private double interest = 0;
private double creditOrderTotal;
/**
* @param args
* @throws Exception
*/
public CreditOrder(String clientName, int orderID, int orderValue, double interest) {
super(clientName,orderID,orderValue);
this.interest = interest;
}
public double getInterest(double interestRate) {
interest = interestRate;
return interest;
}
public void setInterest(double interest) throws Exception{
if(interest < 0) {
throw new Exception("Exception: Interest cannot be less than 0!");
}
this.interest = interest;
}
public String toString() {
return super.toString() + " Interest Percentage: " + interest;
}
/**
* @param int order value, double interest value
*
* @return credit order plus 1 year worth of interest
*
*/
public double getCreditOrderTotalValue(int value, double interest){
double newValue = value * (interest/100) + value;
creditOrderTotal = newValue;
return creditOrderTotal;
}
}