Skip to content

Instantly share code, notes, and snippets.

@luchtech
Created August 21, 2018 07:17
Show Gist options
  • Save luchtech/39ebfed59ebd250b2e6deb1221d33c5e to your computer and use it in GitHub Desktop.
Save luchtech/39ebfed59ebd250b2e6deb1221d33c5e to your computer and use it in GitHub Desktop.
SMS (Constructors, Setters, Getters)
public class SMS
{
private String inbox[];
private int smsCount, capacity, balance;
SMS(){
capacity = 10;
inbox = new String[capacity];
smsCount = 0;
}
SMS(int capacity){
setCapacity(capacity);
inbox = new String[this.capacity];
smsCount = 0;
}
//Observers
boolean isFull(){return (smsCount == capacity);}
boolean isEmpty(){return (smsCount == 0);}
int getLoadBalance(){return balance;}
int getTotalSMS(){return smsCount;}
public int getCapacity(){return capacity;}
String viewSMS(){
if(isEmpty()) return "Inbox is empty.";
else{
String hold = "Index\tMessage\n";
for(int i =0; i < smsCount; i++)
hold+=i+"\t"+inbox[i]+"\n";
return hold;
}
}
//Transformers
void setCapacity(int capacity){
this.capacity = capacity;
}
void setLoad(int amount){
balance+=amount;
}
void setSMS(String sms){
if(balance > 0 && !isFull()){
inbox[smsCount]=sms;
smsCount++;
balance--;
}
}
void clear(){
smsCount = 0;
}
String searchSMS(int index){
if(index == -1) return "Invalid index.";
if(index >= 0 && index < smsCount)
return "[Index "+index+"]\t"+inbox[index];
else return "No message found.";
}
String deleteSMS(int index){
if(index == -1) return "Invalid index.";
if (smsCount == 0) return "Inbox is empty.";
if(index >= 0 && index < smsCount){
for(int i = index; i < smsCount; i++)
inbox[i] = inbox[i+1];
smsCount--;
return "SMS successfully deleted.";
}
else return "No message found.";
}
}
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class TestSMS {
public static void main(String[] args) {
SMS txt = null;
String menu[] = {"Send SMS", "View Inbox", "Search SMS", "Delete SMS", "Clear Messages", "Add Load Wallet"};
String hold = "", message = "";
Object choice;
int response, capacity, index, amount;
response = JOptionPane.showConfirmDialog(null, "Do you want to set Inbox Capacity?", "Inbox Capacity", JOptionPane.YES_NO_OPTION, 3, null);
if(response != 0)
txt = new SMS();
else {
capacity = Valid.toInteger(JOptionPane.showInputDialog("How many messages do you want to store?"));
if(capacity > 0) {
txt = new SMS(capacity);
hold = "Capacity successfully set.\nYou may now store up to "+capacity+" messages.";
}
else {
txt = new SMS();
hold = "Invalid capacity: "+capacity+"\nCapacity was set to 10 messages by default.";
}
JOptionPane.showMessageDialog(null, new JTextArea(hold), "Result", 1);
}
for(;;) {
hold = "Inbox\t: "+txt.getTotalSMS()+" / "+txt.getCapacity()+"\nLoad Balance\t: "+txt.getLoadBalance()+"\n\nChoose:";
message = "";
choice = JOptionPane.showInputDialog(null, new JTextArea(hold), "Hi", 3, null, menu, menu[0]);
if(choice == null) {
JOptionPane.showMessageDialog(null, "Thanks for testing the program!", "Goodbye!", 1);
System.exit(0);
}
switch(choice.toString()) {
case "Send SMS":
if(txt.isFull()) hold = "Inbox is full.";
else if(txt.getLoadBalance() == 0) hold = "Insufficient balance.";
else{
message = JOptionPane.showInputDialog("Enter message:");
if(Valid.allSpace(message)) hold = "Blank input.";
else {
txt.setSMS(message);
hold = "Message successfully added to Inbox.";
}
}
break;
case "View Inbox":
hold = txt.viewSMS();
break;
case "Search SMS":
if(txt.isEmpty()) hold = "No messages yet.";
else {
index = Valid.toInteger(JOptionPane.showInputDialog("Enter index from 0 to "+(txt.getTotalSMS()-1)+":"));
hold = txt.searchSMS(index);
}
break;
case "Delete SMS":
if(txt.isEmpty()) hold = "No messages yet.";
else {
index = Valid.toInteger(JOptionPane.showInputDialog("Enter index from 0 to "+(txt.getTotalSMS()-1)+":"));
hold = txt.deleteSMS(index);
}
break;
case "Clear Messages":
if(txt.getTotalSMS() == 0) hold = "Inbox is empty.";
else {
txt.clear();
hold = "All messages have been deleted.";
}
break;
case "Add Load Wallet":
amount = Valid.toInteger(JOptionPane.showInputDialog("Current Balance: "+txt.getLoadBalance()+"\nEnter amount to add:"));
if(amount <= 0) hold = "Invalid amount.";
else {
txt.setLoad(amount);
hold = amount+" has been loaded successfully.\n\nCurrent Balance: "+txt.getLoadBalance();
}
break;
}
JOptionPane.showMessageDialog(null, new JTextArea(hold), "Result", 1);
}
}
}
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