Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class PhoneShop {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> input = Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("End")){
- String phone = command.split(" - ")[1];
- switch (command.split(" - ")[0]){
- case "Add":
- //String addPhone = command.split(" - ")[1];
- if (!input.contains(phone)){
- input.add(phone);
- }
- break;
- case "Remove":
- if (input.contains(phone)) {
- input.remove(phone);
- }
- break;
- case "Last":
- if (input.contains(phone)){
- input.remove(phone);
- input.add(phone);
- }
- break;
- default:
- String phh = command.split(" ")[3];
- String oldPhone = phh.split(":")[0];
- String newPhone = phh.split(":")[1];
- int index = 0;
- if (input.contains(oldPhone)){
- for (int i = 0; i < input.size(); i++){
- if (input.get(i).equals(oldPhone)){
- index = i;
- }
- }
- input.add(index + 1, newPhone);
- }
- //System.out.println(input.toString().replaceAll("[\\[\\],]", "").replaceAll(" ", ", "));
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println(input.toString().replaceAll("[\\[\\],]", "").replaceAll(" ", ", "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment