Created
August 21, 2018 07:24
-
-
Save luchtech/6b77f2af3e75bf310feb8059ec3bcfa8 to your computer and use it in GitHub Desktop.
Real-Life Processes (Constructors, Setters, Getters)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextArea; | |
public class PhotoCopier { | |
private double usedBlack, usedColored, blackCapacity, coloredCapacity, percentBlack, percentColored, blackPerPage, coloredPerPage, blackPrint, coloredPrint; | |
private int totalCopies; | |
private String brand; | |
private File f; | |
private FileWriter fw; | |
private FileReader fr; | |
private Scanner s; | |
private ArrayList<String> colorL; | |
private ArrayList<Integer> pagesL; | |
private ArrayList<Integer> copiesL; | |
private ArrayList<Integer> totalCopiesL; | |
private ArrayList<Double> priceL; | |
private ArrayList<Double> totalL; | |
//DEFAULT VALUES FOR PRINTER | |
public PhotoCopier(){ | |
blackCapacity = 250; | |
coloredCapacity = 250; | |
usedBlack = 0; | |
usedColored = 0; | |
brand = "Epson"; | |
blackPerPage = 0.1; | |
coloredPerPage = 0.1; | |
blackPrint = 2; | |
coloredPrint = 4; | |
} | |
void resetBlack(){ | |
blackCapacity = 250; | |
usedBlack = 0; | |
JOptionPane.showMessageDialog(null, "Black Cartridge replacement successful."); | |
} | |
void resetColored() { | |
coloredCapacity = 250; | |
usedColored = 0; | |
JOptionPane.showMessageDialog(null, "Colored Cartridge replacement successful."); | |
} | |
boolean isBlackEmpty(){ return usedBlack >= blackCapacity;} | |
boolean isColoredEmpty(){ return usedColored >= coloredCapacity;} | |
boolean isBlackFull(){ return usedBlack == 0;} | |
boolean isColoredFull(){ return usedColored == 0;} | |
String getBrand(){ return brand;} | |
double getBlackCap(){ return blackCapacity;} | |
double getColoredCap(){ return coloredCapacity;} | |
double getUsedBlack(){ return usedBlack;} | |
double getUsedColored(){ return usedColored;} | |
double getPercentBlack() { // Returns Percent Level for Black Ink | |
percentBlack=100-(usedBlack/blackCapacity*100); | |
return percentBlack; | |
} | |
double getPercentColored() { // Returns Percent Level for Colored Ink | |
percentColored=(coloredCapacity-usedColored)/coloredCapacity*100; | |
return percentColored; | |
} | |
void printNow(int pages, int copies, String color) throws FileNotFoundException, IOException { | |
f = new File("trans.txt"); | |
if(!f.exists()){ | |
f.createNewFile(); | |
} | |
fw = new FileWriter(f, true); | |
totalCopies=pages*copies; | |
switch(color) { | |
case "Black": | |
if(usedBlack+totalCopies*blackPerPage > blackCapacity) { | |
JOptionPane.showMessageDialog(null, "Copying cancelled. Not enough ink.", "Copying Failed", 1); | |
} | |
else { | |
usedBlack = usedBlack+totalCopies*blackPerPage; | |
JOptionPane.showMessageDialog(null, new JTextArea(String.format("Transaction Details:\n%d pages x %d copies = %,3d copies\n%,3d copies x P%.2f = P%,3.2f\n\nAmount Payable: P%,3.2f", pages, copies, totalCopies, totalCopies, blackPrint, totalCopies*blackPrint, totalCopies*blackPrint)), "Success", 1); | |
fw.write(String.format("%s#%d#%d#%d#%.2f#%.2f\n", color, pages, copies, totalCopies, blackPrint, (totalCopies*blackPrint))); | |
fw.close(); | |
} | |
break; | |
case "Colored": | |
if(usedColored+totalCopies*coloredPerPage > coloredCapacity) { | |
JOptionPane.showMessageDialog(null, "Copying cancelled. Not enough ink.", "Copying Failed", 1); | |
} | |
else { | |
usedColored = usedColored+totalCopies*coloredPerPage; | |
JOptionPane.showMessageDialog(null, new JTextArea(String.format("Transaction Details:\n%d pages x %d copies = %,3d copies\n%,3d copies x P%.2f = P%,3.2f\n\nAmount Payable: P%,3.2f", pages, copies, totalCopies, totalCopies, coloredPrint, totalCopies*coloredPrint, totalCopies*coloredPrint)), "Success", 1); | |
fw.write(String.format("%s#%d#%d#%d#%.2f#%.2f\n", color, pages, copies, totalCopies, coloredPrint, (totalCopies*coloredPrint))); | |
fw.close(); | |
} | |
break; | |
} | |
} | |
String getPrinterStatus() { | |
if(getPercentBlack() <= 0 && getPercentColored() <= 0) { | |
return "NOT OK"; | |
} | |
else return "OK"; | |
} | |
String printerHealth(){ | |
String hold = String.format("Brand\t: %s\nStatus\t: %s",getBrand(), getPrinterStatus()); | |
return hold; | |
} | |
String getInkStatus() { | |
String hold = ""; | |
if(getPercentBlack() > 0){ | |
hold+="*Black\t: AVAILABLE\n"; | |
} | |
else hold+="*Black\t: NOT AVAILABLE\n"; | |
if(getPercentColored() > 0){ | |
hold+="*Colored\t: AVAILABLE"; | |
} | |
else hold+="*Colored\t: NOT AVAILABLE"; | |
return hold; | |
} | |
String getInkLevels(){ // Returns Ink Levels for Black and Colored | |
return String.format("Black Left\t: %.0f ml / 250 ml (%.0f%%)\nColored Left\t: %.0f ml / 250 ml (%.0f%%)",(250-getUsedBlack()),getPercentBlack(),(250-getUsedColored()), getPercentColored()); | |
} | |
String getPaperCapacity(){ | |
return String.format("Black Left\t: %,3d pages\nColored Left\t: %,3d pages",(int)(getPercentBlack()*250*blackPerPage), (int)(getPercentColored()*250*coloredPerPage)); | |
} | |
String getTotalSales() throws IOException{ | |
colorL = new ArrayList<String>(); | |
pagesL = new ArrayList<Integer>(); | |
copiesL = new ArrayList<Integer>(); | |
totalCopiesL = new ArrayList<Integer>(); | |
priceL = new ArrayList<Double>(); | |
totalL = new ArrayList<Double>(); | |
f = new File("trans.txt"); | |
if(!f.exists()){ | |
f.createNewFile(); | |
} | |
fr = new FileReader(f); | |
s = new Scanner(fr); | |
String info[], hold = ""; | |
while(s.hasNext()){ | |
info = s.nextLine().split("#"); | |
colorL.add(info[0]); | |
pagesL.add(Valid.toInteger(info[1])); | |
copiesL.add(Valid.toInteger(info[2])); | |
totalCopiesL.add(Valid.toInteger(info[3])); | |
priceL.add(Valid.toDouble(info[4])); | |
totalL.add(Valid.toDouble(info[5])); | |
} | |
if(colorL.size() != 0){ | |
hold+="Color\tNo. of Pages\tNo. of Copies\tTotal Copies\tPrice per Page\tTotal Amount\n"; | |
for (int i = 0; i < colorL.size(); i++) { | |
hold+=String.format("%s\t%,3d\t%,3d\t%,3d\tP%.2f\tP%,3.2f\n", colorL.get(i),pagesL.get(i),copiesL.get(i),totalCopiesL.get(i),priceL.get(i),totalL.get(i)); | |
} | |
} | |
else hold = "No data to display."; | |
return hold; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.HeadlessException; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import javax.swing.ImageIcon; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextArea; | |
public class TestPhotoCopier { | |
static PhotoCopier p; | |
static ImageIcon ink = new ImageIcon("inkLogo.png"); | |
static ImageIcon copier = new ImageIcon("copierLogo.png"); | |
static int pages= 0, copies = 0; | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
p = new PhotoCopier(); | |
String choose = ""; | |
for (;;) | |
{ | |
choose = start(); | |
switch (choose) | |
{ | |
case "Photocopy": | |
print(); | |
break; | |
case "Ink Level": | |
inkLevel(); | |
break; | |
case "Replace Ink": | |
replaceInk(); | |
break; | |
case "All Sales": | |
allSales(); | |
break; | |
case "Terminate": | |
JOptionPane.showMessageDialog(null, "Terminating Program..."); | |
System.exit(0); | |
} | |
} | |
} | |
static String start(){ | |
String menu[] = {"Photocopy", "Ink Level", "Replace Ink", "All Sales", "Terminate"}; | |
String choose = ""; | |
try { | |
choose = JOptionPane.showInputDialog(null, new JTextArea(p.printerHealth()), "LuchMacWep Printer", 2, copier, menu, menu[0]).toString(); | |
} catch (Exception e) { | |
JOptionPane.showMessageDialog(null, "Goodbye!"); | |
System.exit(0); | |
} | |
return choose; | |
} | |
static void print(){ | |
String menu[] = {"Black", "Colored"}; | |
String choose = "", error = ""; | |
int pages = 0; | |
int copies = 0; | |
try { | |
choose = JOptionPane.showInputDialog(null, new JTextArea("Ink Status:\n"+p.getInkStatus()+"\n\nCopies Available:\n"+p.getPaperCapacity()), "Photocopy", 2, copier, menu, menu[0]).toString(); | |
switch (choose) { | |
case "Black": | |
if(p.isBlackEmpty()) { | |
JOptionPane.showMessageDialog(null, "Black is empty. Please replace Black Cartridge.", "No Ink", 2); | |
} | |
else { | |
pages = Valid.toInteger(JOptionPane.showInputDialog("Enter number of PAGES: ")); | |
copies = Valid.toInteger(JOptionPane.showInputDialog("Enter number of COPIES: ")); | |
if(pages <= 0){ | |
error+="*Invalid number of pages: "+pages+"\n"; | |
} | |
if(copies <= 0){ | |
error+="*Invalid number of copies: "+copies; | |
} | |
if(!error.equals("")){ | |
JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
else{ | |
p.printNow(pages, copies, "Black"); | |
} | |
} | |
break; | |
case "Colored": | |
if(p.isColoredEmpty()) { | |
JOptionPane.showMessageDialog(null, "RGB is empty. Please replace Colored Cartridge.", "No Ink", 2); | |
} | |
else { | |
pages = Valid.toInteger(JOptionPane.showInputDialog("Enter number of PAGES: ")); | |
copies = Valid.toInteger(JOptionPane.showInputDialog("Enter number of COPIES: ")); | |
if(pages <= 0){ | |
error+="*Invalid number of pages: "+pages+"\n"; | |
} | |
if(copies <= 0){ | |
error+="*Invalid number of copies: "+copies; | |
} | |
if(!error.equals("")){ | |
JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
else{ | |
p.printNow(pages, copies, "Colored"); | |
} | |
} | |
} | |
} catch (Exception e) { | |
start(); | |
} | |
} | |
static void inkLevel(){ | |
JOptionPane.showMessageDialog(null, new JTextArea(p.getInkLevels()), "Ink Level", 1, ink); | |
} | |
static void replaceInk() { | |
String menu[] = {"Black Cartridge", "Colored Cartridge"}; | |
String choose = ""; | |
int confirm = 0; | |
try { | |
choose = JOptionPane.showInputDialog(null, new JTextArea(p.getInkLevels()+"\n\nRemarks:\n"+p.getInkStatus()), "Replace Ink Cartridge", 2, ink, menu, menu[0]).toString(); | |
switch (choose) { | |
case "Black Cartridge": | |
if(!p.isBlackEmpty()) { | |
confirm = JOptionPane.showConfirmDialog(null, String.format("%,3.2f mL (%.0f%%) remaining in Black Cartridge.\nDo you want to continue replacing it?", (250-p.getUsedBlack()), p.getPercentBlack()), "Black Ink Reset", JOptionPane.YES_NO_OPTION,1, ink); | |
if(confirm == 0) p.resetBlack(); | |
else JOptionPane.showMessageDialog(null, "Ink replacement cancelled."); | |
} | |
else { | |
p.resetBlack(); | |
} | |
break; | |
case "Colored Cartridge": | |
if(!p.isColoredEmpty()) { | |
confirm = JOptionPane.showConfirmDialog(null, String.format("%,3.2f mL (%.0f%%) remaining in Black Cartridge.\nDo you want to continue replacing it?", (250-p.getUsedColored()), p.getPercentColored()), "Colored Ink Reset", JOptionPane.YES_NO_OPTION,1, ink); | |
if(confirm == 0) p.resetColored(); | |
else JOptionPane.showMessageDialog(null, "Ink replacement cancelled."); | |
} | |
else { | |
p.resetColored(); | |
} | |
} | |
} catch (Exception e) { | |
start(); | |
} | |
} | |
static void allSales() throws HeadlessException, IOException{ | |
JOptionPane.showMessageDialog(null, new JTextArea(p.getTotalSales())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Vector; | |
public class Valid { | |
/* | |
* This user-defined class is made by James Carlo Luchavez. | |
* This class is composed of the following Data Validating Methods: | |
* (a) "isDuplicate" returns the index of the given data from the array - Vector, ArrayList, Normal Array | |
* (b) "removeExcessSpace" returns a lowercase string with only one space from each word or element | |
* (c) "firstLetterUp" returns a string with first letters of each word capitalized (for names) | |
* (d) "toInteger" converts a string value to integer | |
* (e) "toDouble" converts a string value to double | |
* (f) "" | |
*/ | |
// Vector Array isDuplicate | |
static int isDuplicate(String s, Vector<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, Vector<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, Vector<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, Vector<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
// ArrayList isDuplicate | |
static int isDuplicate(String s, ArrayList<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, ArrayList<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, ArrayList<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, ArrayList<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
// Normal Array isDuplicate | |
static int isDuplicate(String s, String arr[]) { // String Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i].equals(s)) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, int arr[]) { // Integer Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, double arr[]) { // Double Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, char arr[]) { // Character Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
// Excess Whitespace Remover | |
static String removeExcessSpace(String s){ | |
if(s.equals("")) return s; | |
if(allSpace(s)) return ""; | |
String hold = ""; | |
char c; | |
for (int i = 0; i < s.length(); i++) { // REMOVES INAPPROPRIATE WHITESPACES | |
c = s.charAt(i); | |
if(Character.isLetter(c)) { | |
hold+=c; | |
} | |
else if(Character.isWhitespace(c) && hold.length() > 0 && !Character.isWhitespace(hold.charAt(hold.length()-1))) { | |
hold+=" "; | |
} | |
} | |
return hold; | |
} | |
// First Letter to Uppercase | |
static String firstLetterUp(String s) { | |
s = removeExcessSpace(s); | |
if(s.equals("")) return s; | |
String hold = ""; | |
String[] arr = s.split(" "); | |
for (int i = 0; i < arr.length; i++) { // CONVERTS FIRST LETTER FOR EACH WORD | |
arr[i] = arr[i].toUpperCase().charAt(0)+arr[i].toLowerCase().substring(1); | |
hold+=arr[i]; | |
if(i != arr.length-1) { | |
hold+=" "; | |
} | |
} | |
return hold; | |
} | |
// String to Integer | |
static int toInteger(String s) { | |
if(s == null) return -1; | |
String hold = ""; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
if(c == '-' && hold.equals("")){ | |
hold+=c; | |
} | |
} | |
if(hold.equals("")) return 0; | |
else return Integer.parseInt(hold); | |
} | |
// String to Double | |
static Double toDouble(String s) { | |
if(s == null) return (double) -1; | |
String hold = ""; | |
int ctr = 0; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
else if(c == '-' && hold.equals("")) { | |
hold+=c; | |
} | |
else if(c == '.' && ctr != 1) { | |
hold+=c; | |
ctr++; | |
} | |
} | |
if(hold.equals("")) return 0.0; | |
else return Double.parseDouble(hold); | |
} | |
// Checks if a string contains only whitespaces | |
static boolean allSpace(String s) { | |
if(s == null) return true; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(!Character.isWhitespace(c)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment