diff --git a/ticket/Test.java b/ticket/Test.java index 1eb1dd1..1e1f531 100644 --- a/ticket/Test.java +++ b/ticket/Test.java @@ -10,6 +10,9 @@ public class Test { boolean continueBuy = true; Scanner sc = new Scanner(System.in); while (continueBuy) { + System.out.println("Ticket Seller: Five Yuan - " + ts.getFive() + ", Ten Yuan - " + ts.getTen() + + ", Twenty Yuan - " + ts.getTwenty() + ", Fifty Yuan - " + ts.getFifty() + ", Hundred Yuan - " + + ts.getHundred()); System.out.print("Please enter your name: "); name = sc.nextLine(); System.out.print("Please enter your payment: "); @@ -17,7 +20,7 @@ public class Test { Thread consumer = new Thread(ts, name + "-" + payment); consumer.start(); try { - consumer.join(); + Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/ticket/TicketSeller.java b/ticket/TicketSeller.java index 0c3a07b..0251652 100644 --- a/ticket/TicketSeller.java +++ b/ticket/TicketSeller.java @@ -44,15 +44,52 @@ public class TicketSeller implements Runnable { } break; case 50: - if (ten > 0 && five > 0) { - ten--; + if (twenty > 1 && five > 0) { + twenty -= 2; five--; fifty++; success = true; - } else if (five >= 3) { + } else if (ten > 1 && twenty > 0 && five > 0) { + ten -= 2; + twenty--; + five--; + fifty++; + success = true; + } else if (ten > 3 && five > 0) { + ten -= 4; + five--; + fifty++; + success = true; + } else if (twenty > 0 && ten > 0 && five > 2) { + twenty--; + ten--; five -= 3; fifty++; success = true; + } else if (ten > 2 && five > 2) { + ten -= 3; + five -= 3; + fifty++; + success = true; + } else if (twenty > 0 && five > 4) { + twenty--; + five -= 4; + fifty++; + success = true; + } else if (ten > 1 && five > 4) { + ten -= 2; + five -= 4; + fifty++; + success = true; + } else if (ten > 0 && five > 6) { + ten--; + five -= 6; + fifty++; + success = true; + } else if (five > 9) { + five -= 10; + fifty++; + success = true; } break; default: @@ -71,4 +108,44 @@ public class TicketSeller implements Runnable { } } } + + public int getFive() { + return five; + } + + public void setFive(int five) { + this.five = five; + } + + public int getTen() { + return ten; + } + + public void setTen(int ten) { + this.ten = ten; + } + + public int getTwenty() { + return twenty; + } + + public void setTwenty(int twenty) { + this.twenty = twenty; + } + + public int getFifty() { + return fifty; + } + + public void setFifty(int fifty) { + this.fifty = fifty; + } + + public int getHundred() { + return hundred; + } + + public void setHundred(int hundred) { + this.hundred = hundred; + } } diff --git a/ticket/combinations.py b/ticket/combinations.py new file mode 100644 index 0000000..dde6104 --- /dev/null +++ b/ticket/combinations.py @@ -0,0 +1,12 @@ +def find_combinations(): + combinations = [] + for i in range(10): + for j in range(5): + for k in range(3): + if 5 * i + 10 * j + 20 * k == 45: + combinations.append((i, j, k)) + return combinations + + +combinations = find_combinations() +print(f"The combinations of 5, 10, and 20 that sum up to 45 are: {combinations}")