Java Reference
In-Depth Information
return
reply.substring(Const.ERROR.length());
}
}
// return the price of a product given the code
public
double getPrice(
long
item)
throws
Exception{
String reply
#
sendMessage(Const.GET_PRICE
!
" "
!
item);
if
(reply.startsWith(Const.OK)
return
Double.parseDouble(reply.
substring(Const.OK.length()));
else
return
Double.parseDouble(reply.
substring(Const.ERROR.length()));
}
// returns the name of the customer given the code
public
String getCustomer(
long
code)
throws
Exception{
String reply
#
sendMessage(Const.GET_CUSTOMER
!
" "
!
code);
if
(reply.startsWith(Const.OK))
return
reply.substring(Const.OK.length());
else
return
reply.substring(Const.ERROR.length());
}
// communicate a new purchase for the current customer
public void
buy(
long
item,
long
quantity)
throws
Exception {
String reply
#
sendMessage(Const.BUY
!
" "
!
item
!
" "
!
quantity);
if
(reply.startsWith(Const.ERROR))
throw new
Exception(reply
.substring(Const.ERROR.length()));
}
// terminates the transaction
public void
endTransaction() throws Exception {
String reply
#
sendMessage(Const.END);
if
(reply.startsWith(Const.ERROR))
throw
new Exception(reply.
substring(Const.ERROR.length()));
}
// disconnect the terminal from the market server
public void
disconnect() {
try
{
out.write((Const.QUIT
!
"\n").getBytes(
out.flush();
}
catch
(Exception e){ }
socket
#
null
;