Skip to content

Instantly share code, notes, and snippets.

@yanil3500
Last active February 27, 2019 17:37
Show Gist options
  • Save yanil3500/ac8a6f9da8185e7d850602ff8e45be2d to your computer and use it in GitHub Desktop.
Save yanil3500/ac8a6f9da8185e7d850602ff8e45be2d to your computer and use it in GitHub Desktop.
Checkboxes
package com.company;
import java.util.Scanner;
//Uncomment the class definition.
/**
class Checkbox {
private boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void unCheck(){
isChecked = false;
}
public void check(){
isChecked = true;
}
}**/
public class Main {
public static void main(String[] args) {
boolean checkC1 = false;
boolean checkC2 = false;
//Creates two Checkbox objects
Checkbox c1 = new Checkbox();
Checkbox c2 = new Checkbox();
//Reads in command line arguments specifying whether or not the boxes should be checked.
if (args.length == 2) {
checkC1 = Boolean.parseBoolean(args[0]);
checkC2 = Boolean.parseBoolean(args[1]);
} else {
System.err.println("Usage: java Main " +
"<c1 true or false> <c2 true or false>");
System.err.println("Ensure the Checkbox class definition is not commented out.");
System.exit(1);
}
if (checkC1) {
c1.check();
}
if (checkC2) {
c2.check();
}
Scanner reader = new Scanner(System.in); //Reads input from keyboard
//covers case in which both boxes are checked;
//the user will need to deselect a box in order to move forward
while(c1.isChecked() && c2.isChecked()){
System.out.println("Selecting both boxes is not permitted. \nPlease deselect box 1 or box 2 (Enter 1 or 2): ");
int n = reader.nextInt();
if (n == 1) {
c1.unCheck();
} else if (n == 2){
c2.unCheck();
}
}
if (c1.isChecked()) {
performAction(1);
}
if (c2.isChecked()) {
performAction(2);
}
if (!c1.isChecked() && !c2.isChecked()) {
System.out.println("No boxes have been checked.");
}
}
public static void performAction(int n){
System.out.printf("c%d is checked", n);
System.out.println("perform action");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment