-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSellerClient.java
More file actions
130 lines (116 loc) · 4.6 KB
/
SellerClient.java
File metadata and controls
130 lines (116 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package AuctioningSystem;
/*
Code: Calculator client calculatorClient.java
Date: 10th October 2000
Simple client program that remotely calls a set of arithmetic
methods available on the remote calculatorimpl object
*/
import java.util.*;
import java.rmi.Naming; //Import the rmi naming - so you can lookup remote object
import java.rmi.RemoteException; //Import the RemoteException class so you can catch it
import java.net.MalformedURLException; //Import the MalformedURLException class so you can catch it
import java.rmi.NotBoundException; //Import the NotBoundException class so you can catch it
import java.util.Scanner;
import java.util.Random;
public class SellerClient extends Client {
private static SellerInterface s;
private static final String[] commandsArray = new String[]{"create", "close auction", "quit"};
private static User user;
public SellerClient(){
super();
s = (SellerInterface) frontEndInstance;
int id = askForID();
boolean authenticated = authenticateServer(s,id); //authenticates server
if(authenticated){
user = authenticateUser(s,id); //if the server autheticates
if(user == null){
System.out.println("User Authentication failed");
controls(3);
}
} else{
System.out.println("Server Authentication failed");
controls(3);
}
}
public static void main(String[] args) {
try {
// Create the reference to the remote object through the remiregistry
new SellerClient();
listen();
}
// Catch the exceptions that may occur - rubbish URL, Remote exception
// Not bound exception or the arithmetic exception that may occur in
// one of the methods creates an arithmetic error (e.g. divide by zero)
catch (Exception e) {
System.out.println(e);
}
}
//Waits for user to input another command
public static void listen(){
int action = ask(commandsArray);
controls(action);
}
//Handles controls when a command input is given
public static void controls(int action){
try{
switch(action){
case 1 : Item item = create();
int id = s.newAuction(item);
System.out.println("Started auction.");
System.out.println("Auction ID is: " + id);
break;
case 2: close();
break; //close
case 3: System.out.println("Quitting client....");
break;
default: System.out.println("Sorry, you have entered an incorrect command.");
break;
}
if(action != 3){
listen();
}
}
catch(Exception e){
System.out.println(e);
}
}
//Runs the closing protocol. Asks for ID and if the user is sure. If yes, then a request is sent to the server to have the auction closed.
public static void close(){
System.out.println("Please enter the ID of the auction you would like to close: ");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
System.out.println("Are you sure you'd like to close this auction? (Y or N)");
scanner = new Scanner(System.in);
String yorn = scanner.nextLine();
yorn.toLowerCase();
if(yorn.equals("y")){
try{
String message = s.closeAuction(id,user.getID());
System.out.println(message);
} catch(Exception e){
System.out.println(e);
}
}
else if(yorn.equals("n")){
System.out.println("The auction has NOT been closed and is still active.");
}
else{
System.out.println("Sorry didn't quite get that, this auction will not be closed");
}
}
public static Item create(){
System.out.println("Let's create an auction");
System.out.println("------------------------");
System.out.println("Please describe the item: ");
Scanner scanner = new Scanner(System.in);
String description = scanner.nextLine();
System.out.println("Starting price: ");
scanner = new Scanner(System.in);
Float sPrice = scanner.nextFloat();
System.out.println("Reserve price: ");
scanner = new Scanner(System.in);
Float rPrice = scanner.nextFloat();
Item item = new Item(user, sPrice, rPrice, description);
return item;
}
}