Basically, it needs to be debugged, if possible, and made sure output is as requ
ID: 3701669 • Letter: B
Question
Basically, it needs to be debugged, if possible, and made sure output is as required. Please show the output.
//Order class
package superclass;
public class Order {
private int orderID;
private String customerID;
private int productID;
private int dateOfOrder;
private int monthOfOrder;
private int yearOfOrder;
private int orderAmount;
static int cnt;
// default Constructor
public Order()
{
this.customerID = " No Customer ID";
this.productID = 0;
this.dateOfOrder = 0;
this.monthOfOrder = 0;
this.yearOfOrder = 0;
this.orderAmount = 0;
this.orderID = 0;
}
// non default Constructor
public Order(String customerID, int productID, int dateOfOrder,
int monthOfOrder, int yearOfOrder, int orderAmount, int orderID)
{
this.customerID = customerID;
this.productID = productID;
this.dateOfOrder = dateOfOrder;
this.monthOfOrder = monthOfOrder;
this.yearOfOrder = yearOfOrder;
this.orderAmount = orderAmount;
this.orderID = orderID;
}
//Accessor Methods
public String getCustomerID()
{
return this.customerID;
}
public int getProductID()
{
return this.productID;
}
public int getDateOfOrder()
{
return this.dateOfOrder;
}
public int getMonthOfOrder()
{
return this.monthOfOrder;
}
public int getYearOfOrder()
{
return this.yearOfOrder;
}
public int getOrderAmount()
{
return this.orderAmount;
}
public int getOrderID()
{
return this.orderID;
}
//Mutator Method
public void setOrderID(int OrderID)
{
this.orderID = OrderID;
}
public void setCustomerID(String CustomerID)
{
this.customerID = CustomerID;
}
public void setProductID(int ProductID)
{
this.productID = ProductID;
}
public void setDateOfOrder(int DateOfOrder)
{
this.dateOfOrder = DateOfOrder;
}
public void setMonthOfOrder(int MonthOfOrder)
{
this.monthOfOrder = MonthOfOrder;
}
public void setYearOfOrder(int YearOfOrder)
{
this.yearOfOrder = YearOfOrder;
}
public void setOrderAmount( int OrderAmount)
{
this.orderAmount = OrderAmount;
}
public boolean isLessThan(Order other)
{
if (getYearOfOrder() < other.getYearOfOrder()) {
return true;
}
else if (getYearOfOrder() > other.getYearOfOrder()) {
return false;
}
else if (getMonthOfOrder() < other.getMonthOfOrder()) {
return true;
}
else if (getMonthOfOrder() > other.getMonthOfOrder()) {
return false;
}
else if (getDateOfOrder() < other.getDateOfOrder()) {
return true;
}
else if (getDateOfOrder() > other.getDateOfOrder()) {
return false;
}
return true;
}
//ToString Method
public String toString() {
String word = "";
word += "The Order ID is " + this.getOrderID() + " ";
word += "the Customer ID is " + this.getCustomerID() + " ";
word += "the Product ID is " + this.getProductID() + " ";
word += "The Date of Order is " + getMonthOfOrder() + "/" + getDateOfOrder() + "/" + getYearOfOrder() + " ";
word += "The Order Amount is " + this.getOrderAmount() + " ";
return word;
}
}
//reOrder class
package subclass;
import superclass.Order;
public class reOrder extends Order {
private int Period;
private int EndDate;
// default Constructor
public reOrder() {
this.Period = 0;
this.EndDate = 0;
}
// non-default Constructor
public reOrder(String customerID, int productID, int dateOfOrder, int monthOfOrder,
int yearOfOrder, int orderAmount, int orderID, int Period, int endDate) {
super (customerID, productID, dateOfOrder, monthOfOrder, yearOfOrder, orderAmount, orderID);
this.Period = Period;
this.EndDate = endDate;
}
//Accessor Method
public int getPeriod()
{
return this.Period;
}
public int getEndDate()
{
return this.EndDate;
}
//Mutator Method
public void setPeriod(int Period)
{
this.Period=Period;
}
public void setEndDate(int endDate)
{
this.EndDate = endDate;
}
//toString Method
public String toString() {
String word = new String();
word = super.toString();
word += " Period" + this.getPeriod() + " ";
word += " End Date" + this.getEndDate() + " ";
return word;
}
}
//Client class
package client;
import superclass.Order;
import subclass.reOrder;
import java.util.*;
import java.io.File;
import java.io.IOException;
public class Client {
Order [] order; // array of objects
int noRecords = 0; // no of records, not in the array
// add method to sort the array through increasing date order
ArrayList <Order> orders;
public void swap (ArrayList <Order> orders, int i, int j) {
Order temp = orders.get(i);
orders.set (i, orders.get(j));
orders.set (j, temp);
}
public ArrayList <Order> sortString (ArrayList <Order> myArray) {//sort the array based on strings
int insertPt;
int maxIndex;
for (int i = myArray.size()- 1; i >= 1; --i) {
insertPt = i;
maxIndex = 0;
for (int j = 1; j <= i; ++j) {
//find the index with the max value in this sub array
if(myArray.get(maxIndex).getCustomerID().compareTo(myArray.get(j).getCustomerID()) < 0) {
maxIndex = j;
}
}
//swap values
swap(myArray, insertPt, maxIndex);
}
return myArray;
}
public ArrayList <Order> sortDate (ArrayList <Order> myArray) {//sort the array based on date in increasing order
int insertPt;
int maxIndex;
for (int i = myArray.size() - 1; i >= 1; --i) {
insertPt = i;
maxIndex = 0;
for (int j = 0; j <= i; ++j) {
//find the index with the max value in this sub array
if (myArray.get(maxIndex).isLessThan(myArray.get(j))) {
maxIndex = j;
}
}
//swap values
swap (myArray, insertPt, maxIndex);
}
return myArray;
}
public void searchCustomerID(String CustomerID) {
//implement the binary search
if (orders.size() == 0) {
System.out.println("The office supply database is empty");
return;
}
int first = 0;
int end = orders.size() - 1;
int mid = (first + end) / 2;
while (first <= end) {
if (orders.get(mid).getCustomerID().compareTo(CustomerID) == 0) {
System.out.println("Found a match");
System.out.println(orders.get(mid).toString());
return;
}
else if (orders.get(mid).getCustomerID().compareTo(CustomerID) < 0) { //look at the right half
first = mid + 1;
mid = (first + end) / 2;
}
else if (orders.get(mid).getCustomerID().compareTo(CustomerID) > 0) { //look at the left half
end = mid - 1;
mid = (first + end) / 2;
}
}
System.out.println("No match found");
}
public boolean searchForDuplicate(Order aOrder) {
//loop through the library and find duplicates
//return true if duplicate found
//else return false
if(orders.size() == 0) {
return false;
}
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).equals(aOrder)) {
return true;
}
}
return false;
}
public void addOrder (Order order) {
orders.add(order);
}
public void delOrder (int orderIDToRemove) {
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).getOrderID() == orderIDToRemove) {
orders.remove(i);
return;
}
}
}
public void print() {//list the order
for (int i = 0; i < orders.size(); i++) {
System.out.println(orders.get(i).toString());
}
}
public static void main(String [] args)
{
Client client = new Client();
Scanner sc;
String str;
Order aOrder;
try {
File myFile = new File(args[0]);
sc = new Scanner(myFile);
while(sc.hasNextLine()){
str=sc.nextLine();
String[] tok = str.split(":");
aOrder = new Order(tok[0], Integer.parseInt(tok[1]),Integer.parseInt(tok[2]),
Integer.parseInt(tok[3]), Integer.parseInt(tok[4]),
Integer.parseInt(tok[5]), Integer.parseInt(tok[6]));
//check for duplicate records
if (!client.searchForDuplicate(aOrder)){
//create an Order object and load it on the array
client.orders.add(aOrder);
//System.out.println("No of order: " + myOrder.orders);
}
else{
System.out.println("Found a duplicate");
String out = "";
out += "=================================== ";
out += "The Order ID is " + aOrder.getOrderID() + " ";
out += "The Customer ID is " + aOrder.getCustomerID() + " ";
out += "The Product ID is " + aOrder.getProductID() + " ";
out += "The Date of Order is " + aOrder.getMonthOfOrder() + "/" + aOrder.getDateOfOrder() + "/" + aOrder.getYearOfOrder() + " ";
out += "The Order Amount is " + aOrder.getOrderAmount() + " ";
out += "=================================== ";
System.out.println(out);
}
}
sc.close();
}
catch(IOException ioe) {
System.out.println("The file can not be read");
}
//sort the array
client.sortString(client.orders);
// User Interactive Part
String option1;
int option2;
String option;
sc = new Scanner(System.in);
while(true)
{
System.out.println("Select an option:");
System.out.println("Select "A" to add an order");
System.out.println("Select "D" to delete an order");
System.out.println("Select "L" to print out all customer IDs in file");
option1 = sc.nextLine();
switch(option1){
case "A": System.out.println("Add Order ID");
option2 = sc.nextInt();
System.out.println("Add Customer Order ID");
sc.nextInt();
System.out.println("Add Product ID");
sc.nextInt();
System.out.println("Add Date of Order");
sc.nextLine();
System.out.println("Add Order Amount");
sc.nextInt();
System.out.println("Type O for one-time order or R for repeater order");
option = sc.nextLine();
if (option.equals("R")) {
System.out.println("Input Period");
sc.nextInt();
System.out.println("Input EndDate");
sc.nextInt();
}
Order newOrder = new Order(/* Put information here*/);
client.addOrder(newOrder);
break;
case "D": System.out.println("Type an Order ID to remove");
option2 = sc.nextInt();
client.delOrder(option2);
break;
case "L": client.print();
ArrayList<Order> tmpOrders = new ArrayList<Order>(client.orders);
tmpOrders = client.sortDate(tmpOrders);
String CustomerID = sc.nextLine();
for (Order order : tmpOrders) {
if (order.getCustomerID() == CustomerID)
System.out.println(order);
}
break;
}
}
}
}
//orders.txt
R,Orange Inc.,C2,10/22/2003,7,30,12/8/2003
R,Orange Inc.,C2,1/24/2004,1,30,2/5/2004
R,Orange Inc.,A1,10/10/2002,8,30,10/30/2002
O,HAL Industries,A2,10/13/2007,10
O,HAL Industries,A1,3/23/2003,8
O,Orange Inc.,C3,3/16/2007,8
O,HAL Industries,C3,5/17/2006,1
R,HAL Industries,C3,12/16/2008,8,7,2/7/2009
O,Orange Inc.,A2,5/6/2006,10
O,Macrohard Inc.,C3,1/20/2008,1
R,Giggle Industries,C3,12/3/2007,9,1,1/9/2008
O,HAL Industries,C1,4/5/2003,4
O,Macrohard Inc.,A1,2/27/2004,3
R,HAL Industries,C3,6/22/2009,6,7,8/20/2009
O,Orange Inc.,A1,5/8/2004,4
R,Peak Enterprises,A2,2/27/2006,6,7,4/16/2006
R,Peak Enterprises,A2,10/16/2008,5,7,11/26/2008
O,Macrohard Inc.,A2,2/1/2006,1
R,HAL Industries,A2,4/7/2006,10,7,4/15/2006
R,Peak Enterprises,C2,2/3/2005,2,7,2/7/2005
R,Giggle Industries,C3,9/4/2009,10,1,10/19/2009
R,Giggle Industries,A1,10/17/2002,7,1,12/15/2002
R,HAL Industries,C3,4/11/2007,4,7,5/3/2007
R,HAL Industries,C2,8/7/2004,9,7,8/22/2004
O,HAL Industries,A1,4/9/2001,2
O,HAL Industries,B1,6/14/2002,8
R,Giggle Industries,C3,7/2/2006,5,1,8/7/2006
R,Giggle Industries,A1,8/15/2003,10,1,9/26/2003
O,Macrohard Inc.,B1,7/19/2004,10
O,Macrohard Inc.,A2,12/10/2007,6
Explanation / Answer
package superclass;
public class Order {
private int orderID;
private String customerID;
private int productID;
private int dateOfOrder;
private int monthOfOrder;
private int yearOfOrder;
private int orderAmount;
static int cnt;
// default Constructor
public Order()
{
this.customerID = " No Customer ID";
this.productID = 0;
this.dateOfOrder = 0;
this.monthOfOrder = 0;
this.yearOfOrder = 0;
this.orderAmount = 0;
this.orderID = 0;
}
// non default Constructor
public Order(String customerID, int productID, int dateOfOrder,
int monthOfOrder, int yearOfOrder, int orderAmount, int orderID)
{
this.customerID = customerID;
this.productID = productID;
this.dateOfOrder = dateOfOrder;
this.monthOfOrder = monthOfOrder;
this.yearOfOrder = yearOfOrder;
this.orderAmount = orderAmount;
this.orderID = orderID;
}
//Accessor Methods
public String getCustomerID()
{
return this.customerID;
}
public int getProductID()
{
return this.productID;
}
public int getDateOfOrder()
{
return this.dateOfOrder;
}
public int getMonthOfOrder()
{
return this.monthOfOrder;
}
public int getYearOfOrder()
{
return this.yearOfOrder;
}
public int getOrderAmount()
{
return this.orderAmount;
}
public int getOrderID()
{
return this.orderID;
}
//Mutator Method
public void setOrderID(int OrderID)
{
this.orderID = OrderID;
}
public void setCustomerID(String CustomerID)
{
this.customerID = CustomerID;
}
public void setProductID(int ProductID)
{
this.productID = ProductID;
}
public void setDateOfOrder(int DateOfOrder)
{
this.dateOfOrder = DateOfOrder;
}
public void setMonthOfOrder(int MonthOfOrder)
{
this.monthOfOrder = MonthOfOrder;
}
public void setYearOfOrder(int YearOfOrder)
{
this.yearOfOrder = YearOfOrder;
}
public void setOrderAmount( int OrderAmount)
{
this.orderAmount = OrderAmount;
}
public boolean isLessThan(Order other)
{
if (getYearOfOrder() < other.getYearOfOrder()) {
return true;
}
else if (getYearOfOrder() > other.getYearOfOrder()) {
return false;
}
else if (getMonthOfOrder() < other.getMonthOfOrder()) {
return true;
}
else if (getMonthOfOrder() > other.getMonthOfOrder()) {
return false;
}
else if (getDateOfOrder() < other.getDateOfOrder()) {
return true;
}
else if (getDateOfOrder() > other.getDateOfOrder()) {
return false;
}
return true;
}
//ToString Method
public String toString() {
String word = "";
word += "The Order ID is " + this.getOrderID() + " ";
word += "the Customer ID is " + this.getCustomerID() + " ";
word += "the Product ID is " + this.getProductID() + " ";
word += "The Date of Order is " + getMonthOfOrder() + "/" + getDateOfOrder() + "/" + getYearOfOrder() + " ";
word += "The Order Amount is " + this.getOrderAmount() + " ";
return word;
}
}
// 2.
//reOrder class
package subclass;
import superclass.Order;
public class reOrder extends Order {
private int Period;
private int EndDate;
// default Constructor
public reOrder() {
this.Period = 0;
this.EndDate = 0;
}
// non-default Constructor
public reOrder(String customerID, int productID, int dateOfOrder, int monthOfOrder,
int yearOfOrder, int orderAmount, int orderID, int Period, int endDate) {
super (customerID, productID, dateOfOrder, monthOfOrder, yearOfOrder, orderAmount, orderID);
this.Period = Period;
this.EndDate = endDate;
}
//Accessor Method
public int getPeriod()
{
return this.Period;
}
public int getEndDate()
{
return this.EndDate;
}
//Mutator Method
public void setPeriod(int Period)
{
this.Period=Period;
}
public void setEndDate(int endDate)
{
this.EndDate = endDate;
}
//toString Method
public String toString() {
String word = new String();
word = super.toString();
word += " Period" + this.getPeriod() + " ";
word += " End Date" + this.getEndDate() + " ";
return word;
}
}
//3.
//Client class
package client;
import superclass.Order;
import subclass.reOrder;
import java.util.*;
import java.io.File;
import java.io.IOException;
public class Client {
Order [] order; // array of objects
int noRecords = 0; // no of records, not in the array
// add method to sort the array through increasing date order
ArrayList <Order> orders=new ArrayList<Order>();
public void swap (ArrayList <Order> orders, int i, int j) {
Order temp = orders.get(i);
orders.set (i, orders.get(j));
orders.set (j, temp);
}
public ArrayList <Order> sortString (ArrayList <Order> myArray) {//sort the array based on strings
int insertPt;
int maxIndex;
for (int i = myArray.size()- 1; i >= 1; --i) {
insertPt = i;
maxIndex = 0;
for (int j = 1; j <= i; ++j) {
//find the index with the max value in this sub array
if(myArray.get(maxIndex).getCustomerID().compareTo(myArray.get(j).getCustomerID()) < 0) {
maxIndex = j;
}
}
//swap values
swap(myArray, insertPt, maxIndex);
}
return myArray;
}
public ArrayList <Order> sortDate (ArrayList <Order> myArray) {//sort the array based on date in increasing order
int insertPt;
int maxIndex;
for (int i = myArray.size() - 1; i >= 1; --i) {
insertPt = i;
maxIndex = 0;
for (int j = 0; j <= i; ++j) {
//find the index with the max value in this sub array
if (myArray.get(maxIndex).isLessThan(myArray.get(j))) {
maxIndex = j;
}
}
//swap values
swap (myArray, insertPt, maxIndex);
}
return myArray;
}
public void searchCustomerID(String CustomerID) {
//implement the binary search
if (orders.size() == 0) {
System.out.println("The office supply database is empty");
return;
}
int first = 0;
int end = orders.size() - 1;
int mid = (first + end) / 2;
while (first <= end) {
if (orders.get(mid).getCustomerID().compareTo(CustomerID) == 0) {
System.out.println("Found a match");
System.out.println(orders.get(mid).toString());
return;
}
else if (orders.get(mid).getCustomerID().compareTo(CustomerID) < 0) { //look at the right half
first = mid + 1;
mid = (first + end) / 2;
}
else if (orders.get(mid).getCustomerID().compareTo(CustomerID) > 0) { //look at the left half
end = mid - 1;
mid = (first + end) / 2;
}
}
System.out.println("No match found");
}
public boolean searchForDuplicate(Order aOrder) {
//loop through the library and find duplicates
//return true if duplicate found
//else return false
System.out.println(aOrder.toString());
if(orders.size() == 0) {
return false;
}
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).equals(aOrder)) {
return true;
}
}
return false;
}
public void addOrder (Order order) {
orders.add(order);
}
public void delOrder (int orderIDToRemove) {
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).getOrderID() == orderIDToRemove) {
orders.remove(i);
return;
}
}
}
public void print() {//list the order
for (int i = 0; i < orders.size(); i++) {
System.out.println(orders.get(i).toString());
}
}
public static void main(String [] args)
{
Client client = new Client();
Scanner sc=null;
String str=null;
Order aOrder=null;
try {
File myFile = new File(args[0]);
sc = new Scanner(myFile);
while(sc.hasNextLine()){
str=sc.nextLine();
String[] tok = str.split(",");
aOrder = new Order(tok[0], Integer.parseInt(tok[1]),Integer.parseInt(tok[2]),
Integer.parseInt(tok[3]), Integer.parseInt(tok[4]),
Integer.parseInt(tok[5]), Integer.parseInt(tok[6]));
//check for duplicate records
if(client.orders.size()== 0){
client.orders.add(aOrder);
}
if (!client.searchForDuplicate(aOrder)){
//create an Order object and load it on the array
client.orders.add(aOrder);
//System.out.println("No of order: " + myOrder.orders);
}
else{
System.out.println("Found a duplicate");
String out = "";
out += "=================================== ";
out += "The Order ID is " + aOrder.getOrderID() + " ";
out += "The Customer ID is " + aOrder.getCustomerID() + " ";
out += "The Product ID is " + aOrder.getProductID() + " ";
out += "The Date of Order is " + aOrder.getMonthOfOrder() + "/" + aOrder.getDateOfOrder() + "/" + aOrder.getYearOfOrder() + " ";
out += "The Order Amount is " + aOrder.getOrderAmount() + " ";
out += "=================================== ";
System.out.println(out);
}
}
sc.close();
}
catch(IOException ioe) {
System.out.println("The file can not be read");
}
//sort the array
client.sortString(client.orders);
// User Interactive Part
String option1;
int option2;
String option;
sc = new Scanner(System.in);
while(true)
{
System.out.println("Select an option:");
System.out.println("Select "A" to add an order");
System.out.println("Select "D" to delete an order");
System.out.println("Select "L" to print out all customer IDs in file");
option1 = sc.nextLine();
switch(option1){
case "A": System.out.println("Add Order ID");
option2 = sc.nextInt();
System.out.println("Add Customer Order ID");
sc.nextInt();
System.out.println("Add Product ID");
sc.nextInt();
System.out.println("Add Date of Order");
sc.nextLine();
System.out.println("Add Order Amount");
sc.nextInt();
System.out.println("Type O for one-time order or R for repeater order");
option = sc.nextLine();
if (option.equals("R")) {
System.out.println("Input Period");
sc.nextInt();
System.out.println("Input EndDate");
sc.nextInt();
}
Order newOrder = new Order(/* Put information here*/);
client.addOrder(newOrder);
break;
case "D": System.out.println("Type an Order ID to remove");
option2 = sc.nextInt();
client.delOrder(option2);
break;
case "L": client.print();
ArrayList<Order> tmpOrders = new ArrayList<Order>(client.orders);
tmpOrders = client.sortDate(tmpOrders);
String CustomerID = sc.nextLine();
for (Order order : tmpOrders) {
if (order.getCustomerID() == CustomerID)
System.out.println(order);
}
break;
}
}
}
}
//text file
C1,1,20,7,2018,12,101
C2,2,21,4,2018,15,102
C3,1,22,3,2018,14,103
C4,2,25,5,2018,17,104
/*Output:-
The Order ID is 101
the Customer ID is C1
the Product ID is 1
The Date of Order is 7/20/2018
The Order Amount is 12
Found a duplicate
===================================
The Order ID is 101
The Customer ID is C1
The Product ID is 1
The Date of Order is 7/20/2018
The Order Amount is 12
===================================
The Order ID is 102
the Customer ID is C2
the Product ID is 2
The Date of Order is 4/21/2018
The Order Amount is 15
The Order ID is 103
the Customer ID is C3
the Product ID is 1
The Date of Order is 3/22/2018
The Order Amount is 14
The Order ID is 104
the Customer ID is C4
the Product ID is 2
The Date of Order is 5/25/2018
The Order Amount is 17
Select an option:
Select "A" to add an order
Select "D" to delete an order
Select "L" to print out all customer IDs in file
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.