Advertisement
Guest User

sashko

a guest
Dec 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.54 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class pr1 {
  4. public static void main(String[] args) {
  5.  
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. String line = sc.nextLine();
  9. Map<String, List<String>> bandMembers = new LinkedHashMap<>();
  10. Map<String, Integer> bandTime = new HashMap<>();
  11.  
  12. int totalTime = 0;
  13.  
  14. while(!line.equals("start of concert")){
  15.  
  16. String[] tokens = line.split("; ");
  17.  
  18. if(tokens[0].equals("Add")){
  19. String bandName = tokens[1];
  20. String[] members = tokens[2].split(", ");
  21.  
  22. if(!bandMembers.containsKey(bandName)) {
  23. bandMembers.put(bandName, new ArrayList<>());
  24. }
  25.  
  26. for (int i = 0; i < members.length; i++) {
  27. if(!bandMembers.get(bandName).contains(members[i])){
  28. bandMembers.get(bandName).add(members[i]);
  29. }
  30. }
  31.  
  32. }else{
  33. String bandName = tokens[1];
  34. int time = Integer.parseInt(tokens[2]);
  35. bandTime.putIfAbsent(bandName, 0);
  36. bandTime.put(bandName, bandTime.get(bandName) + time);
  37. totalTime += time;
  38. }
  39.  
  40. line = sc.nextLine();
  41. }
  42.  
  43. String band = sc.nextLine();
  44.  
  45. System.out.println("Total time: " + totalTime);
  46. bandTime.entrySet().stream()
  47. .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
  48. .thenComparing(Map.Entry.comparingByKey()))
  49. .forEach(e -> System.out.printf("%s -> %d%n", e.getKey(), e.getValue()));
  50.  
  51. System.out.println(band);
  52. bandMembers.get(band).stream()
  53. .forEach(e -> System.out.println("=> " + e));
  54. }
  55. }
  56.  
  57. ======================================================================================================================
  58.  
  59.  
  60.  
  61.  
  62. import java.util.Scanner;
  63.  
  64. public class pr2 {
  65. public static void main(String[] args) {
  66.  
  67. Scanner sc = new Scanner(System.in);
  68.  
  69. String line = sc.nextLine();
  70.  
  71.  
  72. while(!line.equals("end")){
  73.  
  74. String result = "";
  75.  
  76. String[] tokens = line.split(":");
  77. String artist = tokens[0];
  78. String song = tokens[1];
  79. int key = artist.length();
  80.  
  81. if(artistIsValid(artist) && songIsValid(song)){
  82.  
  83. for (int i = 0; i < artist.length(); i++) {
  84. char symbol = (char)(artist.charAt(i) + key);
  85.  
  86. if((char)(artist.charAt(i) + key) > 'z') {
  87. symbol = (char)((char)(symbol - 'z') + '`');
  88. result += symbol;
  89. }else if ((char)(artist.charAt(i) + key) > 'Z'
  90. && (char)(artist.charAt(i) + key) < 'a') {
  91. symbol = (char)((char)(symbol - 'Z') + '@');
  92. result += symbol;
  93. }else {
  94. if (artist.charAt(i) == '\'' || artist.charAt(i) == ' ') {
  95. result += artist.charAt(i);
  96. } else {
  97. result += symbol;
  98. }
  99. }
  100.  
  101. }
  102.  
  103. result += "@";
  104.  
  105. for (int i = 0; i < song.length(); i++) {
  106. char symbol = (char) (song.charAt(i) + key);
  107.  
  108. if((char)(song.charAt(i) + key) > 'z') {
  109. symbol = (char)((char)(symbol - 'z') + '`');
  110. result += symbol;
  111. }else if ((char)(song.charAt(i) + key) > 'Z') {
  112. symbol = (char)((char)(symbol - 'Z') + '@');
  113. result += symbol;
  114. }else {
  115.  
  116. if (song.charAt(i) == ' ') {
  117. result += song.charAt(i);
  118. } else {
  119. result += symbol;
  120. }
  121. }
  122. }
  123.  
  124. System.out.println("Successful encryption: " + result);
  125. }else{
  126. System.out.println("Invalid input!");
  127. }
  128.  
  129. line = sc.nextLine();
  130. }
  131.  
  132.  
  133. }
  134.  
  135. private static boolean songIsValid(String song) {
  136. for (int i = 0; i < song.length(); i++) {
  137. if(Character.isLowerCase(song.charAt(i)) && song.charAt(i) != ' '){
  138. return false;
  139. }
  140. }
  141.  
  142. return true;
  143. }
  144.  
  145. private static boolean artistIsValid(String artist) {
  146. if(Character.isLowerCase(artist.charAt(0))){
  147. return false;
  148. }
  149.  
  150. for (int i = 1; i < artist.length(); i++) {
  151. if(Character.isUpperCase(artist.charAt(i))){
  152. return false;
  153. }
  154.  
  155. if(!Character.isLetter(artist.charAt(i)) && artist.charAt(i) != '\''
  156. && artist.charAt(i) != ' '){
  157. return false;
  158. }
  159. }
  160.  
  161. return true;
  162. }
  163. }
  164.  
  165.  
  166. =======================================================================================================================
  167.  
  168.  
  169.  
  170.  
  171. package softuni.bandregister.entities;
  172.  
  173. import javax.persistence.*;
  174.  
  175.  
  176. @Entity
  177. @Table(name = "bands")
  178. public class Band {
  179. private Integer id;
  180. private String name;
  181. private String members;
  182. private Double honorarium;
  183. private String genre;
  184.  
  185. public Band(String name, String members, Double honorarium, String genre) {
  186. this.name = name;
  187. this.members = members;
  188. this.honorarium = honorarium;
  189. this.genre = genre;
  190. }
  191.  
  192. public Band() {
  193. }
  194.  
  195. @Id
  196. @GeneratedValue(strategy = GenerationType.IDENTITY)
  197. public Integer getId() {
  198. return id;
  199. }
  200.  
  201. public void setId(Integer id) {
  202. this.id = id;
  203. }
  204.  
  205. @Column(nullable = false)
  206. public String getName() {
  207. return name;
  208. }
  209.  
  210. public void setName(String name) {
  211. this.name = name;
  212. }
  213.  
  214. @Column(nullable = false)
  215. public String getMembers() {
  216. return members;
  217. }
  218.  
  219. public void setMembers(String members) {
  220. this.members = members;
  221. }
  222. @Column(nullable = false)
  223. public Double getHonorarium() {
  224. return honorarium;
  225. }
  226.  
  227. public void setHonorarium(Double honorarium) {
  228. this.honorarium = honorarium;
  229. }
  230. @Column(nullable = false)
  231. public String getGenre() {
  232. return genre;
  233. }
  234.  
  235. public void setGenre(String genre) {
  236. this.genre = genre;
  237. }
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. package softuni.bandregister.bindingModels;
  251.  
  252. public class BandBindingModel {
  253. private String name;
  254. private String members;
  255. private Double honorarium;
  256. private String genre;
  257.  
  258. public BandBindingModel(){}
  259.  
  260. public String getName() {
  261. return name;
  262. }
  263.  
  264. public void setName(String name) {
  265. this.name = name;
  266. }
  267.  
  268. public String getMembers() {
  269. return members;
  270. }
  271.  
  272. public void setMembers(String members) {
  273. this.members = members;
  274. }
  275.  
  276. public Double getHonorarium() {
  277. return honorarium;
  278. }
  279.  
  280. public void setHonorarium(Double honorarium) {
  281. this.honorarium = honorarium;
  282. }
  283.  
  284. public String getGenre() {
  285. return genre;
  286. }
  287.  
  288. public void setGenre(String genre) {
  289. this.genre = genre;
  290. }
  291. }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305. package softuni.bandregister.controllers;
  306.  
  307. import org.springframework.beans.factory.annotation.Autowired;
  308. import org.springframework.stereotype.Controller;
  309. import org.springframework.web.bind.annotation.GetMapping;
  310. import org.springframework.web.bind.annotation.PathVariable;
  311. import org.springframework.web.bind.annotation.PostMapping;
  312. import org.springframework.web.servlet.ModelAndView;
  313. import softuni.bandregister.bindingModels.BandBindingModel;
  314. import softuni.bandregister.entities.Band;
  315. import softuni.bandregister.repositories.BandRepository;
  316.  
  317. import java.util.ArrayList;
  318. import java.util.List;
  319.  
  320. @Controller
  321. public class BandController {
  322. private final BandRepository bandRepository;
  323.  
  324. @Autowired
  325. public BandController(BandRepository bandRepository) {
  326. this.bandRepository = bandRepository;
  327. }
  328.  
  329.  
  330. @GetMapping("/")
  331. public ModelAndView index(ModelAndView modelAndView) {
  332. List<Band> bands = bandRepository.findAll();
  333. modelAndView.setViewName("base-layout");
  334. modelAndView.addObject("view", "band/index");
  335. modelAndView.addObject("bands", bands);
  336. return modelAndView;
  337. }
  338.  
  339. @GetMapping("/create")
  340. public ModelAndView create(ModelAndView modelAndView) {
  341. modelAndView.setViewName("base-layout");
  342. modelAndView.addObject("view", "band/create");
  343. return modelAndView;
  344. }
  345.  
  346. @PostMapping("/create")
  347. public String create(Band band) {
  348. bandRepository.saveAndFlush(band);
  349. return "redirect:/";
  350. }
  351.  
  352. @GetMapping("/edit/{id}")
  353. public ModelAndView edit(ModelAndView modelAndView,
  354. @PathVariable(value = "id") Integer id) {
  355. Band bandToEdit = bandRepository.getOne(id);
  356. modelAndView.setViewName("base-layout");
  357. modelAndView.addObject("view", "band/edit");
  358. modelAndView.addObject("band", bandToEdit);
  359. return modelAndView;
  360. }
  361.  
  362. @PostMapping("/edit/{id}")
  363. public String edit(@PathVariable(value = "id") Integer id, BandBindingModel bandBindingModel) {
  364. Band bandFromDb = bandRepository.getOne(id);
  365. bandFromDb.setName(bandBindingModel.getName());
  366. bandFromDb.setMembers(bandBindingModel.getMembers());
  367. bandFromDb.setHonorarium(bandBindingModel.getHonorarium());
  368. bandFromDb.setGenre(bandBindingModel.getGenre());
  369. bandRepository.saveAndFlush(bandFromDb);
  370.  
  371.  
  372. return "redirect:/";
  373. }
  374.  
  375. @GetMapping("/delete/{id}")
  376. public ModelAndView delete(ModelAndView modelAndView,
  377. @PathVariable(value = "id") Integer id) {
  378. Band bandToDelete = bandRepository.getOne(id);
  379. modelAndView.setViewName("base-layout");
  380. modelAndView.addObject("view", "band/delete");
  381. modelAndView.addObject("band", bandToDelete);
  382. return modelAndView;
  383. }
  384.  
  385. @PostMapping("/delete/{id}")
  386. public String delete(Band band) {
  387. this.bandRepository.delete(band);
  388. return "redirect:/";
  389. }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement