# Point of sale

• December 1st, 2012, 12:39 PM
kreek
Point of sale
Hi guys can u help me do this in java, i am new to java

command line Point of Sale system that can calculate discounts and totals. The system should be for selling books and it should contain the following products:
1. A man apart by author1, priced at \$20
2. Uncharted by author2, priced at \$40

If you purchase two copies of a book, you get an 8 % discount on those two. If you buy three copies or more, you get 20 % discount on all of them.

The system accepts input where you specify the number of items and the id of the book and then you press enter. When you are done you type “end” and press enter.

Example input:
5 1
1 2
end

Example output:
5 copies of A man apart: \$80.00
1 copy of Uncharted: \$40.00
Total price: \$120.00

Example input 2:
1 1
1 1
0 1
End

Example output 2:
2 copies of A man apart: \$36.80
Total price: \$36.80

Code java:

```package pos;   import java.util.Scanner;     public class Pos {     public static void main(String[] args) { System.out.print("Please enter the quantity along with a book book code (1,2, etc) " + "from the list.Please space you entry.\n "); System.out.print("1.A man apart by author1 - Price: \$ 20\n " + "2.Uncharted by author2 - Price: \$ 40\n Quantity and Book Code:\n");   int bkprice1 = 20; int bkprice2 = 40;   String book1 = "Hunger Games"; String book2 = "Clean Code by Robert C. Martin"; float Totalbk1 = 0; float Totalbk2 = 0; float Totalbk1L1 = 0; float Totalbk2L1 = 0; float Totalbk2L2 = 0; float Totalbk3 = 0; float Total; float Disc;   try (Scanner in = new Scanner(System.in)) {   int qty = in.nextInt(); int code = in.nextInt(); int qty2 = in.nextInt(); int code2 = in.nextInt(); String tt = in.next(); int qty3;       if (qty == 2 && code == 1) { Disc = (float) ((float) (qty*bkprice1)*(0.08)); Totalbk1 = (float) (qty*bkprice1) - Disc; System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   } else if (qty > 2 && code == 1) { Disc = (float) ((float) (qty*bkprice1)*(0.2)); Totalbk1 = (float) (qty*bkprice1) - Disc; System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   } else if (qty <= 1 && code == 1) { Totalbk1 = (float) (qty*bkprice1); System.out.println(qty+" copy of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   }   if (qty2 == 2 && code2 == 1) { Disc = (float) ((float) (qty2*bkprice1)*(0.08)); Totalbk1 = (float) (qty2*bkprice1) - Disc; System.out.println(qty2+" copies of "+book1+": GHC "+Totalbk1); Total = Totalbk1L1 + Totalbk1; System.out.println("Total price: "+Total); } else if (qty2 > 2 && code2 == 1) { Disc = (float) ((float) (qty2*bkprice1)*(0.2)); Totalbk1 = (float) (qty2*bkprice1) - Disc; System.out.println(qty2+" copies of "+book1+": GHC "+Totalbk1); Total = Totalbk1L1 + Totalbk1; System.out.println("Total price: "+Total); } else if (qty2 <= 1 && code2 == 1) { Totalbk1 = (float) (qty2*bkprice1); System.out.println(qty2+" copy of "+book1+": GHC "+Totalbk1); Total = Totalbk1L1 + Totalbk1; System.out.println("Total price: "+Total); }         if (qty == 2 && code == 2) { Disc = (float) ((float) (qty*bkprice2)*(0.08)); Totalbk2 = (float) (qty*bkprice2) - Disc; System.out.println(qty+" copies of "+book2+": GHC "+Totalbk2); Totalbk2L1 = Totalbk2;   } else if (qty > 2 && code == 2) { Disc = (float) ((float) (qty*bkprice2)*(0.2)); Totalbk2 = (float) (qty*bkprice2) - Disc; System.out.println(qty+" copies of "+book2+": GHC "+Totalbk2); Totalbk2L1 = Totalbk2;   } else if (qty <= 1 && code == 2) { Totalbk2 = (float) (qty*bkprice2); System.out.println(qty+" copy of "+book2+": GHC "+Totalbk2); Totalbk2L1 = Totalbk2;   }     if (qty2 == 2 && code2 == 2) { Disc = (float) ((float) (qty2*bkprice2)*(0.08)); Totalbk2 = (float) (qty2*bkprice2) - Disc; System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "+Total); } else if (qty2 > 2 && code2 == 2) { Disc = (float) ((float) (qty2*bkprice2)*(0.2)); Totalbk2 = (float) (qty2*bkprice2) - Disc; System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "+Total); } else if (qty2 <= 1 && code2 == 2) { Totalbk2 = (float) (qty2*bkprice2); System.out.println(qty2+" copy of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "+Total); }       if (qty == 2 && code == 1) { Disc = (float) ((float) (qty*bkprice1)*(0.08)); Totalbk1 = (float) (qty*bkprice1) - Disc; System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   } else if (qty > 2 && code == 1) { Disc = (float) ((float) (qty*bkprice1)*(0.2)); Totalbk1 = (float) (qty*bkprice1) - Disc; System.out.println(qty+" copies of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   } else if (qty <= 1 && code == 1) { Totalbk1 = (float) (qty*bkprice1); System.out.println(qty+" copy of "+book1+": GHC "+Totalbk1); Totalbk1L1 = Totalbk1;   }     if (qty2 == 2 && code2 == 2) { Disc = (float) ((float) (qty2*bkprice2)*(0.08)); Totalbk2 = (float) (qty2*bkprice2) - Disc; System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "+Total); } else if (qty2 > 2 && code2 == 2) { Disc = (float) ((float) (qty2*bkprice2)*(0.2)); Totalbk2 = (float) (qty2*bkprice2) - Disc; System.out.println(qty2+" copies of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "+Total); } else if (qty2 <= 1 && code2 == 2) { Totalbk2 = (float) (qty2*bkprice2); System.out.println(qty2+" copy of "+book2+": GHC "+Totalbk2); Total = Totalbk2L1 + Totalbk2; System.out.println("Total price: "Total); }       } } }```

Please let me know if my logic is correct. I want my code to be as succinct as possible.
• December 1st, 2012, 12:49 PM
Norm
Re: Point of sale
[code=java]
[/code]
to get highlighting and preserve formatting.
Be sure the code is properly indented.

Quote:

Please let me know if my logic is correct
Does the code generate the correct output? That would be one requirement.
• December 1st, 2012, 01:54 PM
kreek
Re: Point of sale
yes it does.I would really appreciate it if your could code another alternative to the method i have started using as per the question above.The are to possible outputs outlined above.Thanks so very much.
• December 1st, 2012, 02:01 PM
Norm
Re: Point of sale
Sorry, I don't do much coding here.

The posted code needs to be formatted. Nested statements should not start in the first column.
}s should not be in the same column.