/////////////////////////////////////////////
Exceptions///////////////////////////////
import java.util.Scanner;
class Encrypter{
public static boolean isAlpha(String s) {
return s != null && s.matches("[a-zA-Z][a-zA-Z ]+[a-zA-Z]$");
}
public static String encryptMessage(String name){
String name1;
if(isAlpha(name)){
try{
Validator.validate(name);
//name = StringFormatter.reverseString("name")
char ch[]= name.toCharArray();
String rev="";
for(int i=ch.length-1;i>=0;i--){
rev+=ch[i];
}
name1 = rev;
name = name1.toLowerCase();
}
catch(Exception e){
System.out.println("Try again with valid message");
}
}
else{
System.out.println("InvalidMessageException: Try again with
valid message");
String name2 = "InvalidMessageException: Try again with valid
message";
return name2;
}
return name;
}
}
class Validator {
public static boolean validate(String message) {
return message.matches("[A-Za-z0-9 ]+");
}
}
public class Solution {
private static final Scanner INPUT_READER = new Scanner(System.in);
public static void main(String[] args) {
String message = INPUT_READER.nextLine();
try {
String encrypted_message = Encrypter.encryptMessage(message);
if(! encrypted_message.startsWith("InvalidMessageException"))
System.out.println(encrypted_message);
} catch (Exception e) {
System.out.println(e);
}
}
}
//////////////////////////Files//////////////////////////////
import java.io.*;
import java.util.Random;
import java.util.Scanner;
// Write your code below.
//
class EncryptDecryptFile {
public void writeDecryptionFile(String message) throws Exception{
String decryptFilename = Solution.filepath + "DecryptionFile.txt";
FileWriter writer = new FileWriter(decryptFilename);
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write(message);
buffer.close();
// System.out.println("Success");
}
public String readEncryptionFile() throws Exception{
String EncryptionFilename = Solution.filepath + "EncryptionFile.txt";
// // FileReader fr=new FileReader(EncryptionFilename);
// // BufferedReader br=new BufferedReader(fr);
// // int i;
// // while((i=br.read())!=-1){
// // System.out.print((char)i);
// // }
// // br.close();
// // fr.close();
// Scanner in = new Scanner(new FileReader(EncryptionFilename));
// StringBuilder sb = new StringBuilder();
// while(in.hasNext()) {
// sb.append(in.next());
// System.out.println(sb);
// }
// in.close();
// String outString = sb.toString();
// String content;
// content = new String(Files.readAllBytes(Paths.get("sample.txt")));
// return outString;
String content = null;
File file = new File(EncryptionFilename); // For example, foo.txt
FileReader reader = null;
try {
reader = new FileReader(file);
char[] chars = new char[(int) file.length()];
reader.read(chars);
content = new String(chars);
//System.out.println(content);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
reader.close();
}
}
return content;
}
}
public class Solution {
public static String filepath = System.getenv("OUTPUT_PATH").substring(0,
System.getenv("OUTPUT_PATH").lastIndexOf("\\") + 1);
private static String generateString()
{
char[] chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
StringBuilder generatedString = new StringBuilder(20);
Random random = new Random();
for (int i = 0; i < 40; i++) {
char c = chars[random.nextInt(chars.length)];
generatedString.append(c);
}
return generatedString.toString();
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String message = sc.nextLine();
try{
EncryptDecryptFile f = new EncryptDecryptFile ();
String encryptFilename = Solution.filepath + "EncryptionFile.txt";
String generatedString = generateString();
BufferedWriter writer = new BufferedWriter(new
FileWriter(encryptFilename));
writer.write(generatedString);
writer.close();
if(f.readEncryptionFile().equals(generatedString))
{
f.writeDecryptionFile(message);
String decryptFilename = Solution.filepath + "DecryptionFile.txt";
BufferedReader reader = new BufferedReader(new
FileReader(decryptFilename));
String messageFromFile = reader.readLine();
reader.close();
System.out.println(messageFromFile);
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
import java.io.*;
import java.util.*;
class Register {
private static Register register = null;
static String fruit;
static double price;
/*
* Complete the 'getTotalBill' function below.
*
* The function is expected to return a STRING.
* The function accepts MAP itemDetails as parameter.
*/
public Register(String fruit,double price)
{
this.fruit = fruit;
this.price = price;
}
public static Register getInstance( ) {
register = new Register(fruit, price);
return register;
}
public String getTotalBill(Map<String,Double> itemDetails) {
// Write your code here
/* List<Register> list1=new ArrayList<Register>();
List<Register> list2=new ArrayList<Register>();
List<Register> list3=new ArrayList<Register>();
List<Register> list4=new ArrayList<Register>(); */
Map<String,Double> myItems=new HashMap<>();
myItems.put("apple", 2.0);
myItems.put("grape", 1.0);
myItems.put("orange", 1.5);
myItems.put("mango", 1.2);
// Register r[] = new Register[4];
// r[0] = new Register("apple",2);
// r[1] = new Register("orange",1.5);
// r[2] = new Register("mango",1.2);
// r[3] = new Register("grapes",1.0);
// System.out.print(r[0].fruit+" "+r[0].price);
// System.out.print(r[1].fruit+" "+r[1].price);
// System.out.print(r[2].fruit+" "+r[2].price);
// System.out.print(r[3].fruit+" "+r[3].price);
/* List<Register> list[] = new List[4];
list1.add(r1);
list2.add(r2);
list3.add(r3);
list4.add(r4);
list[0] = list1;
list[1] = list2;
list[2] = list3;
list[3] = list4;
for(int a = 0;a<4;a++)
{
System.out.print(list[0].get(0).fruit);
}*/
String fruits[] = new String[itemDetails.size()];
int i = 0,j = 0;
double sum1 = 0;
for (String fruit1 : itemDetails.keySet())
{
fruits[i] = fruit1;
i++;
}
// for(int q = 0;q<i;q++)
// {
// System.out.println(fruits[q]+" ");
// }
double quantities[] = new double[itemDetails.size()];
for (double quantity : itemDetails.values())
{
quantities[j] = quantity;
j++;
}
// for(int q = 0;q<i;q++)
// {
// System.out.println(quantities[q] + " ");
// }
for(int k = 0;k<i;k++)
{
//for(int p = 0;p<4;p++)
//{
// if(fruits[k]==myItems.get(fruits[k]));
//{
sum1 = sum1 + (myItems.get(fruits[k])* quantities[k] );
// System.out.println(myItems.get(fruits[k]));
// System.out.println(fruits[k]);
// }
// }
String s = String.valueOf(sum1);
return s;
}
public class Solution {
public static void main(String[] args) throws IOException {
Scanner readInput = new Scanner(System.in);
String[] input=readInput.nextLine().split(" ");
Map<String,Double> myItems=new HashMap<String,Double>();
for(int i=0;i<input.length;i+=2){
myItems.put(input[i],Double.parseDouble(input[i+1]));
}
Register regObj = Register.getInstance();
System.out.println(regObj.getTotalBill(myItems));
readInput.close();
}
}