Skip to content

Commit 5de2f8b

Browse files
authored
Merge pull request #693 from abhishekkumar177/main
Accept it for Hacktoberfest
2 parents f406dfe + aed0c67 commit 5de2f8b

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-0
lines changed

Java/AtmInterface.java

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
//TASK 3 OIBSIP
2+
import java.util.Scanner;
3+
4+
class BankAccount {
5+
6+
String name;
7+
String userName;
8+
String password;
9+
String accountNo;
10+
float balance = 10000f;
11+
int transactions = 0;
12+
String transactionHistory = "";
13+
14+
// BankAccount(String name, String userName, String password, String accountNo) {
15+
// this.name = name;
16+
// this.userName = userName;
17+
// this.password = password;
18+
// this.accountNo = accountNo;
19+
// }
20+
21+
public void register() {
22+
Scanner sc = new Scanner(System.in);
23+
System.out.print("\nEnter Your Name - ");
24+
this.name = sc.nextLine();
25+
System.out.print("\nEnter Your Username - ");
26+
this.userName = sc.nextLine();
27+
System.out.print("\nEnter Your Password - ");
28+
this.password = sc.nextLine();
29+
System.out.print("\nEnter Your Account Number - ");
30+
this.accountNo = sc.nextLine();
31+
System.out.println("\nRegistration completed..kindly login");
32+
}
33+
34+
public boolean login() {
35+
boolean isLogin = false;
36+
Scanner sc = new Scanner(System.in);
37+
while ( !isLogin ) {
38+
System.out.print("\nEnter Your Username - ");
39+
String Username = sc.nextLine();
40+
if ( Username.equals(userName) ) {
41+
while ( !isLogin ) {
42+
System.out.print("\nEnter Your Password - ");
43+
String Password = sc.nextLine();
44+
if ( Password.equals(password) ) {
45+
System.out.print("\nLogin successful!!");
46+
isLogin = true;
47+
}
48+
else {
49+
System.out.println("\nIncorrect Password");
50+
}
51+
}
52+
}
53+
else {
54+
System.out.println("\nUsername not found");
55+
}
56+
}
57+
return isLogin;
58+
}
59+
60+
public void withdraw() {
61+
62+
System.out.print("\nEnter amount to withdraw - ");
63+
Scanner sc = new Scanner(System.in);
64+
float amount = sc.nextFloat();
65+
try {
66+
67+
if ( balance >= amount ) {
68+
transactions++;
69+
balance -= amount;
70+
System.out.println("\nWithdraw Successfully");
71+
String str = amount + " Rs Withdrawed\n";
72+
transactionHistory = transactionHistory.concat(str);
73+
74+
}
75+
else {
76+
System.out.println("\nInsufficient Balance");
77+
}
78+
79+
}
80+
catch ( Exception e) {
81+
}
82+
}
83+
84+
public void deposit() {
85+
86+
System.out.print("\nEnter amount to deposit - ");
87+
Scanner sc = new Scanner(System.in);
88+
float amount = sc.nextFloat();
89+
90+
try {
91+
if ( amount <= 100000f ) {
92+
transactions++;
93+
balance += amount;
94+
System.out.println("\nSuccessfully Deposited");
95+
String str = amount + " Rs deposited\n";
96+
transactionHistory = transactionHistory.concat(str);
97+
}
98+
else {
99+
System.out.println("\nSorry...Limit is 100000.00");
100+
}
101+
102+
}
103+
catch ( Exception e) {
104+
}
105+
}
106+
107+
public void transfer() {
108+
109+
Scanner sc = new Scanner(System.in);
110+
System.out.print("\nEnter Receipent's Name - ");
111+
String receipent = sc.nextLine();
112+
System.out.print("\nEnter amount to transfer - ");
113+
float amount = sc.nextFloat();
114+
115+
try {
116+
if ( balance >= amount ) {
117+
if ( amount <= 50000f ) {
118+
transactions++;
119+
balance -= amount;
120+
System.out.println("\nSuccessfully Transfered to " + receipent);
121+
String str = amount + " Rs transfered to " + receipent + "\n";
122+
transactionHistory = transactionHistory.concat(str);
123+
}
124+
else {
125+
System.out.println("\nSorry...Limit is 50000.00");
126+
}
127+
}
128+
else {
129+
System.out.println("\nInsufficient Balance");
130+
}
131+
}
132+
catch ( Exception e) {
133+
}
134+
}
135+
136+
public void checkBalance() {
137+
System.out.println("\n" + balance + " Rs");
138+
}
139+
140+
public void transHistory() {
141+
if ( transactions == 0 ) {
142+
System.out.println("\nEmpty");
143+
}
144+
else {
145+
System.out.println("\n" + transactionHistory);
146+
}
147+
}
148+
}
149+
150+
151+
public class AtmInterface {
152+
153+
154+
public static int takeIntegerInput(int limit) {
155+
int input = 0;
156+
boolean flag = false;
157+
158+
while ( !flag ) {
159+
try {
160+
Scanner sc = new Scanner(System.in);
161+
input = sc.nextInt();
162+
flag = true;
163+
164+
if ( flag && input > limit || input < 1 ) {
165+
System.out.println("Choose the number between 1 to " + limit);
166+
flag = false;
167+
}
168+
}
169+
catch ( Exception e ) {
170+
System.out.println("Enter only integer value");
171+
flag = false;
172+
}
173+
};
174+
return input;
175+
}
176+
177+
178+
public static void main(String[] args) {
179+
180+
System.out.println("\n**********WELCOME TO SBI ATM SYSTEM**********\n");
181+
System.out.println("1.Register \n2.Exit");
182+
System.out.print("Enter Your Choice - ");
183+
int choice = takeIntegerInput(2);
184+
185+
if ( choice == 1 ) {
186+
BankAccount b = new BankAccount();
187+
b.register();
188+
while(true) {
189+
System.out.println("\n1.Login \n2.Exit");
190+
System.out.print("Enter Your Choice - ");
191+
int ch = takeIntegerInput(2);
192+
if ( ch == 1 ) {
193+
if (b.login()) {
194+
System.out.println("\n\n**********WELCOME BACK " + b.name + " **********\n");
195+
boolean isFinished = false;
196+
while (!isFinished) {
197+
System.out.println("\n1.Withdraw \n2.Deposit \n3.Transfer \n4.Check Balance \n5.Transaction History \n6.Exit");
198+
System.out.print("\nEnter Your Choice - ");
199+
int c = takeIntegerInput(6);
200+
switch(c) {
201+
case 1:
202+
b.withdraw();
203+
break;
204+
case 2:
205+
b.deposit();
206+
break;
207+
case 3:
208+
b.transfer();
209+
break;
210+
case 4:
211+
b.checkBalance();
212+
break;
213+
case 5:
214+
b.transHistory();
215+
break;
216+
case 6:
217+
isFinished = true;
218+
break;
219+
}
220+
}
221+
}
222+
}
223+
else {
224+
System.exit(0);
225+
}
226+
}
227+
}
228+
else {
229+
System.exit(0);
230+
}
231+
232+
233+
234+
}
235+
}
236+

Java/NumberGuessingGame.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//NumberGuessingGame
2+
import java.util.Random;
3+
import javax.swing.JOptionPane;
4+
5+
public class NumberGuessingGame {
6+
public static void main(String[] args) {
7+
int lowerBound = 1;
8+
int upperBound = 100;
9+
int maxAttempts = 5;
10+
int score = 0;
11+
12+
JOptionPane.showMessageDialog(null, "Welcome to Guess the Number Game!");
13+
14+
boolean playAgain = true;
15+
while (playAgain) {
16+
int randomNumber = generateRandomNumber(lowerBound, upperBound);
17+
int attempts = 0;
18+
boolean guessedCorrectly = false;
19+
20+
while (attempts < maxAttempts && !guessedCorrectly) {
21+
int userGuess = getUserGuess(lowerBound, upperBound);
22+
attempts++;
23+
24+
if (userGuess == randomNumber) {
25+
JOptionPane.showMessageDialog(null, "Congratulations! You guessed the correct number.");
26+
score += maxAttempts - attempts + 1;
27+
guessedCorrectly = true;
28+
} else if (userGuess < randomNumber) {
29+
JOptionPane.showMessageDialog(null, "Too low! Try a higher number.");
30+
} else {
31+
JOptionPane.showMessageDialog(null, "Too high! Try a lower number.");
32+
}
33+
}
34+
35+
if (!guessedCorrectly) {
36+
JOptionPane.showMessageDialog(null, "Sorry, you did not guess the correct number. The number was: " + randomNumber);
37+
}
38+
39+
int option = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Play Again", JOptionPane.YES_NO_OPTION);
40+
playAgain = (option == JOptionPane.YES_OPTION);
41+
}
42+
43+
JOptionPane.showMessageDialog(null, "Game Over! Your total score is: " + score);
44+
JOptionPane.showMessageDialog(null, "Thank you for playing Guess the Number Game!");
45+
}
46+
47+
private static int generateRandomNumber(int lowerBound, int upperBound) {
48+
Random random = new Random();
49+
return random.nextInt(upperBound - lowerBound + 1) + lowerBound;
50+
}
51+
52+
private static int getUserGuess(int lowerBound, int upperBound) {
53+
String userInput = JOptionPane.showInputDialog(null, "Enter your guess (" + lowerBound + " - " + upperBound + "):");
54+
try {
55+
return Integer.parseInt(userInput);
56+
} catch (NumberFormatException e) {
57+
JOptionPane.showMessageDialog(null, "Invalid input! Please enter a valid number.");
58+
return getUserGuess(lowerBound, upperBound);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)