Advertisement
Parasect

Untitled

Oct 19th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Main
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         /*
  9.         Scanner in = new Scanner(System.in);
  10.         double t = 0;
  11.         Account a[] = new Account[20];
  12.         for (int i = 0; i<20; i++){
  13.             a[i] = new Account(in.nextLine(), (int)(Math.random()*1001.0));
  14.             t += a[i].getBalance();
  15.         }
  16.        double avg = t/20;
  17.         for (int i = 0; i<20; i++){
  18.             if(a[i].getBalance()>avg){
  19.                 System.out.println(a[i].toString());
  20.             }
  21.         }
  22.         */
  23.         Account b = new Account("yo",100);
  24.         Account c = new Account("k",300);
  25.         Account d = new Account("d", 200);
  26.         Account e = new Account("mo",50);
  27.         Account [] x = {b,c,d,e};
  28.         sortByBalance(x);
  29.         for (int i = 0; i<x.length;i++){
  30.             System.out.println(x[i]);
  31.         }
  32.     }
  33.     public static void robinHood(Account[] arr){
  34.         double max = Integer.MAX_VALUE;
  35.         double min = 0;
  36.         int num1 = 0;
  37.         int num2 = 0;
  38.         for(int i = 0; i<arr.length; i++){
  39.             if (arr[i].getBalance() <max){
  40.                max= arr[i].getBalance();
  41.                num1 = i;
  42.             }
  43.             if (arr[i].getBalance() >min){
  44.                 min = arr[i].getBalance();
  45.             num2=i;
  46.             }
  47.         }
  48.         arr[num1].transfer(arr[num2], arr[num1].getBalance()/2);
  49.         arr[num2].balance = arr[num2].getBalance()/2;
  50.     }
  51.     public static double takeCommission(Account[] arr, int percent){
  52.         double emla = 0;
  53.         for (int i = 0; i<arr.length;i++){
  54.             double z = arr[i].getBalance()/100*percent;
  55.             emla+=z;
  56.         }
  57.         return emla;
  58.     }
  59.     public static boolean check(Account[] arr, int accNum){
  60.         for (int i = 0; i<arr.length; i++){
  61.             if(arr[i].getAccNum()==accNum){
  62.                 return true;
  63.             }
  64.         }
  65.         return false;
  66.     }
  67.     public static void print(Account[] arr, Date date){
  68.        
  69.         for (int i = 0; i<arr.length;i++){
  70.         if(date.compareTo(arr[i].getOpenDate()) == -1){
  71.             System.out.println(arr[i].toString());
  72.         }    
  73.         }
  74.     }
  75.     public static int count(Account[] arr, int year){
  76.         int c = 0;
  77.         for(int i = 0; i<arr.length; i++){
  78.             if (arr[i].getOpenDate().getYear() == year){
  79.                 c++;
  80.             }
  81.         }
  82.         return c;
  83.     }
  84.     public static Account oldest(Account[] arr){
  85.         int y = Integer.MAX_VALUE;
  86.         int z = 0;
  87.         for(int i = 0; i<arr.length; i++){
  88.             if(arr[i].getOpenDate().getYear() < y){
  89.                 y = arr[i].getOpenDate().getYear();
  90.                 z = i;
  91.             }
  92.         }
  93.         return arr[z];
  94.     }
  95.     public static int[] check(Account[] arr, String name){
  96.         int []x = new int[arr.length];
  97.         for(int i = 0; i<arr.length; i++){
  98.                if(arr[i].getOwnerName().equals(name)){
  99.                    x[0] = i;
  100.                }
  101.         }
  102.        
  103.         return x;
  104.        
  105.     }
  106.     public static void sortByBalance(Account[] arr){
  107.         boolean isFinished = true;
  108.         while(isFinished){
  109.             isFinished = false;
  110.         for(int i = 0; i<arr.length-1;i++){
  111.         if(arr[i+1].getBalance()<arr[i].getBalance()){
  112.         Account temp = arr[i];
  113.         arr[i] = arr[i+1];
  114.         arr[i+1] = temp;
  115.         isFinished = true;
  116.         }
  117.         }
  118.         }
  119. }      
  120.     }
  121.  
  122. ----------------------------------
  123. public class Account
  124. {
  125.     private static int nextAccNum = 1000;
  126.     private String ownerName;
  127.     private int accNum;
  128.     public double balance;
  129.     private Date openDate;
  130.  
  131.     public Account(String name)
  132.     {
  133.         ownerName = name;
  134.         balance = 0;
  135.         accNum = nextAccNum++;
  136.         openDate = new Date(0, 0, 0);
  137.         openDate.setCurrent();
  138.     }
  139.  
  140.     public Account(String name, double balance)
  141.     {
  142.         ownerName = name;
  143.         this.balance = balance;
  144.         accNum = nextAccNum++;
  145.         openDate = new Date(0, 0, 0);
  146.         openDate.setCurrent();
  147.     }
  148.  
  149.     public Account(Account a)
  150.     {
  151.         this.ownerName = a.ownerName;
  152.         this.balance = a.balance;
  153.         this.accNum = a.accNum;
  154.         this.openDate = a.openDate;
  155.     }
  156.  
  157.     public void deposit(double x)
  158.     {
  159.         this.balance += x;
  160.     }
  161.  
  162.     public boolean withdraw(double x)
  163.     {
  164.         if((this.balance + x) < 0)
  165.         {
  166.             return false;
  167.         }
  168.         this.balance += x;
  169.         return true;
  170.     }
  171.  
  172.     public boolean transfer(Account a, double x)
  173.     {
  174.         if(x > this.balance)
  175.         {
  176.             return false;
  177.         }
  178.         a.deposit(x);
  179.         return true;
  180.     }
  181.  
  182.     public void setOwnerName(String name)
  183.     {
  184.         this.ownerName = name;
  185.     }
  186.  
  187.     public String getOwnerName()
  188.     {
  189.         return this.ownerName;
  190.     }
  191.  
  192.     public double getBalance()
  193.     {
  194.         return this.balance;
  195.     }
  196.  
  197.     public int getAccNum()
  198.     {
  199.         return this.accNum;
  200.     }
  201.  
  202.     public Date getOpenDate()
  203.     {
  204.         return openDate;
  205.     }
  206.     public String toString()
  207.     {
  208.        
  209.         return (ownerName +" "+ balance + " " + openDate.toString());
  210.     }
  211.  
  212.     public static int getAccsNum()
  213.     {
  214.         return nextAccNum-1000;
  215.     }
  216.    
  217.  
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement