Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication1;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- *
- * @author t1406g_dtgiang
- */
- public class hendy {
- private int ID;
- private String date;
- private double amount;
- private String content;
- int getID() {
- return ID;
- }
- String getdate() {
- return date;
- }
- double getamount() {
- return amount;
- }
- String getcontent() {
- return content;
- }
- void setDate(String date) {
- this.date = date;
- }
- void display() {
- System.out.printf("%-5d%-15s%-10.3f%-10s\n", ID, date, amount, content);
- }
- public hendy() {
- }
- public void setID( int ID){
- this.ID=ID;
- }
- public void setContent( String content){
- this.content=content;
- }
- public void setAmount(double amount){
- this.amount=amount;
- }
- public hendy(int ID, String date, double amount, String content) {
- this.ID = ID;
- this.date = date;
- this.amount = amount;
- this.content = content;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- expense ex = new expense();
- int chose;
- do {
- System.out.println("1.add an expense");
- System.out.println("2.display expense");
- System.out.println("3.delete expense");
- System.out.println("4.exit");
- System.out.println("chose");
- chose = sc.nextInt();
- switch (chose) {
- case 1:
- ex.input();
- break;
- case 2:
- ex.display1();
- break;
- case 3:
- System.out.println("enter ID");
- int tim = sc.nextInt();
- ex.delete(tim);
- break;
- case 4:
- System.exit(4);
- default:
- System.out.println("Nhap lai");
- }
- } while (true);
- }
- }
- class expense {
- ArrayList ar = new ArrayList();
- private Object exit;
- void input() {
- int ID=1;
- String date=new String();
- double amount;
- String content;
- Scanner sc = new Scanner(System.in);
- do {
- int check = 1;
- System.out.println("ID " + ID);
- while (check != 0) {
- System.out.println("DATE");
- date = sc.nextLine();
- if (checkdate(date) == 0) {
- check = 0;
- } else {
- if (date.equals("exit") == true) {
- return;
- } else {
- check = 1;
- }
- }
- }
- System.out.println("amount : ");
- amount = sc.nextDouble();
- System.out.println("content :");
- content=sc.nextLine();
- content = sc.nextLine();
- hendy h = new hendy(ID,date,amount,content);
- ar.add(h);
- ID++;
- } while (true);
- }
- void display1() {
- System.out.print("ID DATE MOUNT CONTENT");
- System.out.println("");
- hendy h = new hendy();
- for (int i = 0; i < ar.size(); i++) {
- h = (hendy) ar.get(i);
- h.display();
- System.out.println("");
- }
- }
- int checkdate(String date) {
- hendy h = new hendy();
- 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) {
- return 0;
- } else {
- return 1;
- }
- }
- int searchID(int tim){
- hendy h = new hendy();
- for(int i=0; i<ar.size();i++){
- h= (hendy) ar.get(i);
- if( h.getID() == tim ){
- return i;
- }
- }
- return -1;
- }
- void delete(int tim){
- if(tim == -1)
- System.out.println("k co ID ");
- else{
- ar.remove(searchID(tim));
- System.out.println("ok");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement