Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class LibraryBooks2{
- public static void main(String[] args){
- System.out.println("This program calculates the profits and orders of books");
- System.out.println("Enter \"quit\" at any time to quit ");
- userInteraction();
- //math();
- //printInfo();
- }
- public static void userInteraction(){
- q1(); //new book
- q2(); //book id
- q3(); //book cost
- q4(); //# of copies
- q5(); //# of students in class
- q6(); //book required
- q7(); //book new to school
- }
- public static void q1(){ //new book
- Scanner scanner = new Scanner(System.in);
- String qNum = "q1";
- System.out.println("Do you want to add a book? ( y / n )");
- ynValidation(scanner.nextLine(),qNum);
- }
- public static void q2(){ //book id
- Scanner scanner = new Scanner(System.in);
- String qNum = "q2";
- System.out.println("What is the book ID? ( y / n )");
- String bookID=scanner.nextLine();
- System.out.println("You entered: " +bookID + "\nIs this correct? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static void q3(){ //book cost
- Scanner scanner = new Scanner(System.in);
- String qNum = "3";
- System.out.println("What is the cost of the book?");
- double validNum = numValidation(qNum);
- System.out.println("You entered: $" +validNum + ". Is this correct? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static void q4(){ //# of copies in storage
- Scanner scanner = new Scanner(System.in);
- String qNum = "4";
- System.out.println("How many copies of this book are currently in storage?");
- double validNum = numValidation(qNum);
- int validInt = (int) validNum;
- System.out.println("Confirm there are "+validInt+" copies in storage? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static void q5(){ //# of students in class
- Scanner scanner = new Scanner(System.in);
- String qNum = "5";
- System.out.println("How many students are enroled in class?");
- double validNum = numValidation(qNum);
- int validInt = (int) validNum;
- System.out.println("Confirm there are "+validInt+" students in class? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static void q6(){ //book required?
- Scanner scanner = new Scanner(System.in);
- String qNum = "6";
- System.out.println("Is this book required for class? ( y / n )");
- //double validNum = numValidation(qNum);
- //int validInt = (int) validNum;
- //System.out.println("Confirm there are "+validInt+"copies in storage? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static void q7(){ //new book to school
- Scanner scanner = new Scanner(System.in);
- String qNum = "7";
- System.out.println("Is this book new to school? ( y / n )");
- //double validNum = numValidation(qNum);
- //int validInt = (int) validNum;
- //System.out.println("Confirm there are "+validInt+"copies in storage? ( y / n )");
- ynValidation(scanner.nextLine(), qNum);
- }
- public static double numValidation(String x){
- Scanner scanner = new Scanner(System.in);
- double y=0;
- if(x.equals("3")){
- try{
- y = scanner.nextDouble();
- }catch(InputMismatchException exception)
- {
- System.out.println("Invalid input, please input a number");
- q3();
- }
- }
- if(x.equals("4")){
- try{
- y = scanner.nextDouble();
- }catch(InputMismatchException exception)
- {
- System.out.println("Invalid input, please input a number");
- q4();
- }
- if(!(y % 1==0)){
- System.out.println("Invalid input, please input a WHOLE number");
- q4();
- }
- }
- if(x.equals("5")){
- try{
- y = scanner.nextDouble();
- }catch(InputMismatchException exception)
- {
- System.out.println("Invalid input, please input a number");
- q5();
- }
- if(!(y % 1==0)){
- System.out.println("Invalid input, please input a WHOLE number");
- q5();
- }
- }
- return y;
- //
- // if(x <= 0){
- // System.out.println("please enter a valid number");
- // }
- //
- }
- public static String ynValidation(String x, String y){
- if(x.equals("quit")||x.equals("QUIT")){
- System.out.println("Program exiting...");
- System.exit(0);
- }
- if(x.equals("n")|| x.equals("N")){
- if(y.equals("q1")){
- q1();
- }
- if(y.equals("q2")){
- q2();
- }
- if(y.equals("q3")){
- q3();
- }
- //System.out.println("Program exiting...");
- //System.exit(0);
- }
- if(!(x.equals("y") || x.equals("Y"))){
- System.out.println("invalid choice -- please try again or enter \"quit\" to exit");
- if(y.equals("q1")){
- q1();
- }
- if(y.equals("q2")){
- q2();
- }
- if(y.equals("q3")){
- q3();
- }
- if(y.equals("q4")){
- q4();
- }
- if(y.equals("q5")){
- q5();
- }
- if(y.equals("q6")){
- q6();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement