Advertisement
giangnhauqe

hendy

May 31st, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javaapplication1;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author t1406g_dtgiang
  14.  */
  15. public class hendy {
  16.  
  17.     private int ID;
  18.     private String date;
  19.     private double amount;
  20.     private String content;
  21.  
  22.     int getID() {
  23.         return ID;
  24.     }
  25.  
  26.     String getdate() {
  27.         return date;
  28.     }
  29.  
  30.     double getamount() {
  31.         return amount;
  32.     }
  33.  
  34.     String getcontent() {
  35.         return content;
  36.     }
  37.  
  38.     void setDate(String date) {
  39.         this.date = date;
  40.     }
  41.  
  42.     void display() {
  43.         System.out.printf("%-5d%-15s%-10.3f%-10s\n", ID, date, amount, content);
  44.  
  45.     }
  46.  
  47.     public hendy() {
  48.     }
  49.     public void setID( int ID){
  50.         this.ID=ID;
  51.     }
  52.     public void setContent( String content){
  53.         this.content=content;
  54.     }
  55.     public void setAmount(double amount){
  56.         this.amount=amount;
  57.     }
  58.  
  59.     public hendy(int ID, String date, double amount, String content) {
  60.         this.ID = ID;
  61.         this.date = date;
  62.         this.amount = amount;
  63.         this.content = content;
  64.     }
  65.  
  66.     public static void main(String[] args) {
  67.         Scanner sc = new Scanner(System.in);
  68.         expense ex = new expense();
  69.         int chose;
  70.         do {
  71.             System.out.println("1.add an expense");
  72.             System.out.println("2.display expense");
  73.             System.out.println("3.delete expense");
  74.             System.out.println("4.exit");
  75.             System.out.println("chose");
  76.             chose = sc.nextInt();
  77.             switch (chose) {
  78.                 case 1:
  79.                     ex.input();
  80.                     break;
  81.                 case 2:
  82.                     ex.display1();
  83.                     break;
  84.                 case 3:
  85.                     System.out.println("enter ID");
  86.                     int tim = sc.nextInt();
  87.                    
  88.                     ex.delete(tim);
  89.                     break;
  90.                 case 4:
  91.                     System.exit(4);
  92.                 default:
  93.                     System.out.println("Nhap lai");
  94.             }
  95.         } while (true);
  96.     }
  97. }
  98.  
  99. class expense {
  100.  
  101.     ArrayList ar = new ArrayList();
  102.     private Object exit;
  103.  
  104.     void input() {
  105.         int ID=1;
  106.         String date=new String();
  107.         double amount;
  108.         String content;
  109.         Scanner sc = new Scanner(System.in);
  110.         do {
  111.            
  112.             int check = 1;
  113.             System.out.println("ID " + ID);
  114.             while (check != 0) {
  115.                 System.out.println("DATE");
  116.                 date = sc.nextLine();
  117.                 if (checkdate(date) == 0) {
  118.                  check = 0;
  119.                 } else {
  120.                     if (date.equals("exit") == true) {
  121.                         return;
  122.                     } else {
  123.                          check = 1;
  124.                     }
  125.                 }
  126.             }
  127.             System.out.println("amount : ");
  128.             amount = sc.nextDouble();
  129.          
  130.            
  131.             System.out.println("content :");
  132.             content=sc.nextLine();
  133.             content = sc.nextLine();
  134.            
  135.             hendy h = new hendy(ID,date,amount,content);
  136.             ar.add(h);
  137.             ID++;
  138.  
  139.         } while (true);
  140.     }
  141.  
  142.     void display1() {
  143.         System.out.print("ID     DATE       MOUNT      CONTENT");
  144.         System.out.println("");
  145.         hendy h = new hendy();
  146.         for (int i = 0; i < ar.size(); i++) {
  147.             h = (hendy) ar.get(i);
  148.             h.display();
  149.             System.out.println("");
  150.         }
  151.     }
  152.  
  153.     int checkdate(String date) {
  154.         hendy h = new hendy();
  155.         if (date.matches("(0[0-9]|[12][0-9]|[3][01])/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/[0-9]{4}") == true) {
  156.             return 0;
  157.         } else {
  158.             return 1;
  159.         }
  160.     }
  161.     int searchID(int tim){
  162.         hendy h = new hendy();
  163.         for(int i=0; i<ar.size();i++){
  164.             h= (hendy) ar.get(i);
  165.             if( h.getID() == tim ){
  166.                 return i;
  167.             }
  168.         }
  169.         return -1;
  170.     }
  171.     void delete(int tim){
  172.         if(tim == -1)
  173.             System.out.println("k co ID ");
  174.         else{
  175.             ar.remove(searchID(tim));
  176.             System.out.println("ok");
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement