韩顺平学JAVA:课程练习(零钱通项目——过程编程版)

        

        没有处理非法输入... 

import java.util.Scanner;
import java.io.IOException;
import java.text.SimpleDateFormat; 
import java.util.Date;

public class SmallChange {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		boolean sysOn = true;
		int balance = 0;
		String breakdown = "";
		do {
			clearScreen();
			System.out.println("-----------------SmallChange------------------\n"
								+ "\t 1 Breakdown\n"
								+ "\t 2 Proceeds\n"
								+ "\t 3 Expenditure\n"
								+ "\t 4 Exit\n"
								+ "-----------------SmallChange------------------");
			System.out.println("Please select the required operation(from 1 to 4):");
			int op = in.nextInt();
			switch(op) {
			case 1:
				clearScreen();
				System.out.println("------------------Breakdown-------------------");
				System.out.println(breakdown);
				System.out.println("------------------Breakdown-------------------");
				System.out.println("Press Enter to return...");
				in.nextLine();
				in.nextLine();
				break;
			case 2:
				clearScreen();
				System.out.println("-------------------Proceed--------------------");
				System.out.println("Please input the proceed:");
				int proceed = in.nextInt();
				balance += proceed;
				String proceedBD = "\nProceed:" + proceed + "\t" + getCurrentTime() + "\t" 
									+ "Balance:" + balance;
				breakdown += proceedBD;
				System.out.println("Operation sucessful!");
				System.out.println("Press Enter to return...");
				in.nextLine();
				in.nextLine();
				break;
			case 3:
				clearScreen();
				System.out.println("-------------------Expenditure--------------------" );
				System.out.println("Please input the expenditure item:");	
				String item = in.next(); 
				System.out.println("Please input the expenditure:");	
				int expenditure = in.nextInt();
				balance -= expenditure;
				String expenditureBD = "\n" + item + ":" + expenditure + "\t" + getCurrentTime() + "\t"
										+ "Balance:" + balance;
				breakdown += expenditureBD;
				System.out.println("Press Enter to return...");
				in.nextLine();
				in.nextLine();
				break;
			case 4:
				sysOn = false;
				System.out.println("System is exiting...");
			default:
				break;
			}
 
		} while(sysOn);
		
		in.close();
		System.out.println("Exit success!");
	}
	
	public static void clearScreen() {
	        try {
	            String operatingSystem = System.getProperty("os.name");
	
	            if (operatingSystem.contains("Windows")) {
	                ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
	                Process process = pb.inheritIO().start();
	                process.waitFor();
	            } else {
	                ProcessBuilder pb = new ProcessBuilder("clear");
	                Process process = pb.inheritIO().start();
	                process.waitFor();
	            }
	        } catch (IOException | InterruptedException e) {
	            e.printStackTrace();
	        }
	}
	
	public static String getCurrentTime() {
		Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
		return simpleDateFormat.format(date);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值