1. 程式人生 > >Java十六章以及十七章

Java十六章以及十七章

十六章第一題:實現使用者登入功能,在控制檯輸入使用者名稱和密碼,然後判斷輸入是否正確並輸入結果
答案:package lesson16;

public class No_1_1 {
public void yanzhen(String name,String pwd){

	if(name.equals("qwert")&&pwd.equals("qwert")){
		System.out.println("登入成功");
	}else{
		System.out.println("使用者名稱或密碼錯誤,登入失敗");
	}
}

}

package lesson16;

import java.util.Scanner;

public class No_1 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	No_1_1 a=new No_1_1();
	Scanner input=new Scanner(System.in);
	System.out.print("請輸入使用者名稱:");
	String b=input.next();
	System.out.print("請輸入密碼:");
	String c=input.next();
	a.yanzhen(b, c);

}

}

第二題:在控制檯輸入學生姓名,年齡,性別,和學校,然後模擬將該學生資訊儲存到資料庫中。
答案:package lesson16;

public class No_2_1 {
public void xinxi(String name,int year,String xingbie,String school){
System.out.println(“將該學生資訊成功習寫入資料庫”);
System.out.println(name+" “+year+”\t"+xingbie+" "+school);
}

}

package lesson16;

import java.util.Scanner;

public class No_2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	No_2_1 a=new No_2_1();
	Scanner input=new Scanner(System.in);
	System.out.print("請輸入學生姓名");
	String name=input.next();
	System.out.print("請輸入學生年齡");
	int year=input.nextInt();
	System.out.print("請輸入學生性別");
	String xingbie=input.next();
	System.out.print("請輸入學生學校");
	String school=input.next();
	a.xinxi(name, year, xingbie, school);

}

}

第三題:3.某公司對固定資產進行編號,規則如下:購買年份+產品型別(1為桌上型電腦,2為筆記本,3為其他,統一採用兩位數字表示,數字前加0)+3位隨機數。
答案:package lesson16;

public class No_3_1 {
public void bianhao(String year,int xuan,int suiji){
StringBuffer bianhao=new StringBuffer(year);
int a=0;
StringBuffer bianhao1=bianhao.append(a);
StringBuffer bianhao2=bianhao1.append(xuan);
StringBuffer bianhao3=bianhao2.append(suiji);
System.out.println(bianhao3);
}

}

package lesson16;

import java.util.Scanner;

public class No_3 {

public static void main(String[] args) {
	int max = 999; // 隨機範圍
	int min = 100;// 隨機範圍
	int suiji = (int) (Math.random() * (max - min)) + min;
	// TODO Auto-generated method stub
	No_3_1 a=new No_3_1();
	Scanner input=new Scanner(System.in);
	System.out.print("請輸入年份:");
	String year=input.next();
	System.out.print("請選擇產品型別1.桌上型電腦2.筆記本3.其他:");
	int xuan=input.nextInt();
	a.bianhao(year, xuan, suiji);

}

}

第四題:4.按照月/日/年的方法輸入一個日期(如8/8/2008),然後對字串進行拆分,輸出某天是哪年哪月哪日(如2008年8月8日)。

答案:package lesson16;

public class No_4_1 {
public void chaofen(String nianfen){
String a[]=nianfen.split("/");
System.out.println(a[2]+“年”+a[0]+“月”+a[1]+“日”);
}

}

package lesson16;

import java.util.Scanner;

public class No_4 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	No_4_1 a = new No_4_1();
	Scanner input = new Scanner(System.in);
	System.out.print("請輸入一個日期,格式為月/日/年");
	String year = input.next();
	a.chaofen(year);
	int i=0,k=2;
	
	

	  

}

}

十七章:吃貨聯盟訂餐系統
答案:package chihuo;

public class No_2 {
String[] dishNames={“紅燒帶魚”,“魚香肉絲”,“蔬菜”,“鮮湯”};
double[] prices=new double[]{38.0,20.0,10.0,15.0};
int[] praiseNums=new int[3];
package chihuo;

import java.util.Scanner;

public class shuzi {
public int shuzi(int xuan){
Scanner input=new Scanner(System.in);
int b = 0;
boolean a=true;
while (a) {
if(input.hasNextInt()==true){
b=input.nextInt();
a=false;
}

	}return b;
}

}

package chihuo;
import java.util.Scanner;
public class add {
String[] names = new String[4];
String[] dishMegs = new String[4];
int[] times = new int[4];
String[] addresses = new String[4];
int[] states = new int[4];
double[] sumPrice = new double[4];
No_2 a = new No_2();
public boolean add() {
Scanner input = new Scanner(System.in);
boolean isAdd = false;
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
isAdd = true;
System.out.println(“請輸入訂餐人姓名”);
String name = input.next();
System.out.println(“序號” + “\t” + “菜名” + “\t” + “單價” + “\t”
+ “點贊數”);
for (int j = 0; j < a.dishNames.length - 1; j++) {
String price = a.prices[j] + “元”;
String priaiseNum = (a.praiseNums[j]) > 0 ? a.praiseNums[j]
+ “贊” : “0”;
System.out.println((j + 1) + “\t” + a.dishNames[j] + “\t”
+ price + “\t” + priaiseNum);
}
System.out.println(“請選擇您要點的菜品編號:”);
int chooseDish = input.nextInt();
System.out.println(“請選擇您需要的份數:”);
int number = input.nextInt();
String dishMeg = a.dishNames[chooseDish - 1] + " " + number
+ “份”;
double sumPrices = a.prices[chooseDish - 1] * number;
double deliCharge = (sumPrices >= 50) ? 0 : 5;
System.out.print(“請輸入送餐時間,送餐時間是10點至20點間整點送餐:”);
int time = input.nextInt();
while (time < 10 || time > 20) {
System.out.println(“您的輸入有誤,請輸入10-20之間的整數!”);
time = input.nextInt();
}
System.out.print(“請輸入送餐地址:”);
String address = input.next();
System.out.println(“訂餐成功”);
System.out.println(“您訂的是” + dishMeg);
System.out.println(“送餐時間:” + time + “點”);
System.out.println(“餐費:” + sumPrices + “元,送餐費” + deliCharge
+ “元,總計:” + (sumPrices + deliCharge) + “元”);
names[i] = name;
dishMegs[i] = dishMeg;
times[i] = time;
addresses[i] = address;
sumPrice[i] = sumPrices + deliCharge;
break;
}
}
if (!isAdd) {
System.out.println(“對不起,您的餐袋已滿!”);
return false;
} else
return true;
}
public void display() {
System.out.println(“序號\t訂餐人\t餐品資訊\t\t送餐時間\t送餐地址\t\t總金額\t訂單狀態”);
for (int i = 0; i < names.length; i++) {
if (names[i] != null) {
String state = (states[i] == 0) ? “已預訂” : “已完成”;
String date = times[i] + “點”;
String sumPrices = sumPrice[i] + “元”;
System.out.println((i + 1) + “\t” + names[i] + “\t”
+ dishMegs[i] + “\t” + date + “\t” + addresses[i]
+ “\t” + sumPrices + “\t” + state);
}
}
}
public void sigh() {
boolean isSighFind = false;
Scanner input = new Scanner(System.in);
System.out.print(“請選擇要簽收的訂單號”);
int sighOrderId = input.nextInt();
for (int i = 0; i < names.length; i++) {
if (names[i] != null && states[i] == 0 && sighOrderId == i + 1) {
states[i] = 1;
System.out.println(“訂單簽收完成”);
isSighFind = true;
} else if (names[i] != null && states[i] == 1
&& sighOrderId == i + 1) {
System.out.println(“您選擇的訂單已簽收,不能再次簽收!”);
isSighFind = true;
}
if (!isSighFind) {
System.out.println(“您選擇的訂單不存在”);
}
}
}
public void delete() {
boolean delfind = false;
Scanner input = new Scanner(System.in);
System.out.println(“請輸入要刪除的訂單序號”);
int delid = input.nextInt();
for (int i = 0; i < names.length; i++) {
if (names[i] != null && states[i] == 1 && delid == i + 1) {
delfind = true;
for (int j = delid - 1; j < names.length - 1; j++) {
names[j] = names[j + 1];

				dishMegs[i] = dishMegs[j + 1];
				times[i] = times[j + 1];
				addresses[i] = addresses[j + 1];
				sumPrice[i] = sumPrice[j + 1];
			}
			int endIndex = names.length - 1;
			names[endIndex] = null;
			dishMegs[endIndex] = null;
			times[endIndex] = 0;
			addresses[endIndex] = null;
			sumPrice[endIndex] = 0;
			System.out.println("刪除訂單成功");
			break;

		} else if (names[i] != null && states[i] == 0 && delid == i + 1) {
			System.out.println("您選擇的訂單未簽收,不能刪除");
			delfind = true;
			break;
		}
	}
	if (!delfind) {
		System.out.println("您要刪除的訂單不存在");
	}
}
public void praise() {
	Scanner input = new Scanner(System.in);
	System.out.println("序號" + "\t" + "菜名" + "\t" + "單價");
	for (int i = 0; i < a.dishNames.length - 1; i++) {
		String price = a.prices[i] + "元";
		String priaiseNum = (a.praiseNums[i]) > 0 ? a.praiseNums[i] + "贊"
				: "0";
		System.out.println((i + 1) + "\t" + a.dishNames[i] + "\t" + price
				+ "\t" + priaiseNum);
	}
	System.out.println("請選擇您要點讚的菜品序號");
	int priaiseNum = input.nextInt();
	a.praiseNums[priaiseNum - 1]++;
	System.out.println("點贊成功");
}

}

package chihuo;

import java.util.Scanner;

public class OrderingSet {
public void starMenu() {
add add = new add();
shuzi c=new shuzi();
Scanner input = new Scanner(System.in);
int num = -1;

	boolean isExit = false;
	System.out.println("\n歡迎使用吃過聯盟訂餐系統");
	do {
		System.out.println("*********************");
		System.out.println("1.我要訂餐");
		System.out.println("2.檢視餐袋");
		System.out.println("3.簽收訂單");
		System.out.println("4.刪除訂單");
		System.out.println("5.我要點贊");
		System.out.println("6.退出系統");
		System.out.println("*********************");
		System.out.print("請選擇:");
		
		 int choose = input.nextInt();
		
		
		switch (choose) {
		case 1:
			System.out.println("我要訂餐");
			add.add();

			break;
		case 2:
			System.out.println("檢視餐袋");
			add.display();
			break;
		case 3:
			System.out.println("簽收訂單");
			add.sigh();
			break;
		case 4:
			System.out.println("刪除訂單");
			add.delete();
			break;
		case 5:
			System.out.println("我要點贊");
			add.praise();
			break;
		case 6:
			System.out.println("退出系統謝謝使用");
			isExit = true;
			break;
		default:
			System.out.print("輸入錯誤,");
			break;
		}
		if (!isExit) {
			System.out.print("輸入0返回");
			num = input.nextInt();
		} else {
			break;
		}
	
	}while (num == 0);
}

}

package chihuo;

import java.util.Scanner;

public class OrderingUtil {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	OrderingSet a=new OrderingSet();
	a.starMenu();
	

}

}