/Users/19942/NetBeansProjects/Material Management/src/MaterialManagement_Class/MaterialPurchase.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package MaterialManagement_Class;
  7 import java.util.ArrayList;
  8 /**
  9  *
 10  * @author 19942
 11  */
 12 public class MaterialPurchase {
 13     private int row = 0;
 14     private String name = "";
 15     private String dimension;
 16     private int price;
 17     private int timeOfShipment;
 18     private int priority;
 19     private double score;
 20     private double number;
 21 
 22     public MaterialPurchase(){
 23 
 24     }
 25 
 26     public MaterialPurchase(int row, String name,  String dimension, int price, int timeOfShipment, int priority, double score, double number){
 27         this.row = row;
 28         this.name = name;
 29         this.dimension = dimension;
 30         this.price = price;
 31         this.timeOfShipment = timeOfShipment;
 32         this.priority = priority;
 33         this.score = score;
 34         this.number = number;
 35     }
 36 
 37     public void setRow(int row) {
 38         this.row = row;
 39     }
 40 
 41     public void setName(String name) {
 42         this.name = name;
 43     }
 44 
 45     public void setDimension(String dimension) {
 46         this.dimension = dimension;
 47     }
 48 
 49     public void setPrice(int price) {
 50         this.price = price;
 51     }
 52 
 53     public void setTimeOfShipment(int timeOfShipment) {
 54         this.timeOfShipment = timeOfShipment;
 55     }
 56     
 57     public void setPriority(int priority){
 58         this.priority = priority;
 59     }
 60     
 61     public void setScore(double score){
 62         this.score = score;
 63     }
 64     
 65     public void setNumber(double number){
 66         this.number = number;
 67     }
 68 
 69     public int getRow() {
 70         return row;
 71     }
 72 
 73     public String getName() {
 74         return name;
 75     }
 76 
 77     public String getDimension() {
 78         return dimension;
 79     }
 80     
 81     public int getPrice() {
 82         return price;
 83     }
 84 
 85     public int getTimeOfShipment() {
 86         return timeOfShipment;
 87     }
 88     
 89     public int getPriority(){
 90         return priority;
 91     }
 92     
 93     public double getScore(){
 94         return score;
 95     }
 96     
 97     public double getNumber(){
 98         return number;
 99     }
100     
101     public ArrayList<MaterialPurchase> sort(ArrayList<MaterialPurchase> purchaseList){
102         //Sorting the purchase list. 
103         int n = purchaseList.size();
104         boolean sorted = false;
105         while (!sorted) {
106             n--; 
107             sorted = true;
108             for (int i = 0; i < n; i++) {
109                 if (purchaseList.get(i).getScore() < purchaseList.get(i+1).getScore()) {
110                     MaterialPurchase temp = purchaseList.get(i);
111                     purchaseList.set(i, purchaseList.get(i+1));
112                     purchaseList.set(i+1, temp);
113                     sorted = false; //if the second variable is larger than the first variable, this means that the program is not sorted yet. As a reuslt, the sorted variable will be false. 
114                 }
115             }
116         }
117         return purchaseList;
118     }
119     
120     public int amountLeft(ArrayList<NewMaterial> newMaterial, String key){
121         //this will output the amount left for the selected material. 
122         for(int i = 0; i<newMaterial.size(); i++){
123             if(newMaterial.get(i).getName().equals(key))
124                 return newMaterial.get(i).getNumber();
125         }
126         return 9999;
127     }
128     
129     public ArrayList<MaterialPurchase> autoGenerate(ArrayList<MaterialPurchase> materialPurchase, ArrayList<NewMaterial> newMaterial, 
130     int wPriority, int wPrice,  int wTime, int wAmountLeft, int budget){
131         //this is an algorithems which will generate a list of recommended materials for purchase based on the weight of each factor. 
132         ArrayList<MaterialPurchase> purchaseList =  new ArrayList<MaterialPurchase>();
133         for(int i = 0; i<materialPurchase.size(); i++){
134             //give each factor a score. The adjustment is made by multiplying the weight by a constant
135             double sPriority = (double)wPriority/materialPurchase.get(i).getPriority()*3;
136             double sPrice = (double)wPrice/materialPurchase.get(i).getPrice()*70;
137             double sTime = (double)wTime/materialPurchase.get(i).getTimeOfShipment()*60;
138             double sAmountLeft = (double)wAmountLeft/amountLeft(newMaterial, materialPurchase.get(i).getName())*40;
139             materialPurchase.get(i).setScore(sPriority + sPrice + sTime + sAmountLeft);
140         }
141 
142         materialPurchase = sort(materialPurchase);//sort the material purchase list in the descending sequence based on the score.
143         for(int i = 0; i<10; i++){
144             purchaseList.add(materialPurchase.get(i));//the top 10 will be selected and recommended to the teachers. 
145         }
146         double totalScore= 0.0;
147         for(int i = 0; i<purchaseList.size(); i++){
148             totalScore += purchaseList.get(i).getScore();
149         }
150         double weight = budget/totalScore;
151         for(int i = 0; i<purchaseList.size(); i++){
152             purchaseList.get(i).setNumber((int)(weight*purchaseList.get(i).getScore()/purchaseList.get(i).getPrice()));
153         }//make a recommendation about how many materials should be purchased based on the budget. 
154         
155         return purchaseList;
156     }
157     
158     public MaterialPurchase[] bubbuleSort(MaterialPurchase [] purchaseList){
159         //Sorting the purchase list. 
160         int n = purchaseList.length;
161         boolean sorted = false;
162         while (!sorted) {
163             n--; 
164             sorted = true;
165             for (int i = 0; i < n; i++) {
166                 if (purchaseList[i].getScore() < purchaseList[i+1].getScore()) {
167                     MaterialPurchase temp = purchaseList[i];
168                     purchaseList[i] = purchaseList[i+1];
169                     purchaseList[i+1] = temp;
170                     sorted = false; //if the second variable is larger than the first variable, this means that the program is not sorted yet. As a reuslt, the sorted variable will be false. 
171                 }
172             }
173         }
174         return purchaseList;
175     }
176 
177 }
178 
179 
180